Lasagna Master Segentation Fault

Hello,

I am getting a segmentation fault when I click the “Run Test” button on the website for the Lasagna Master exercise. I can test the code locally on my computer and all the tests pass.

### We received the following error when we ran your code:

make[2]: *** [CMakeFiles/test_lasagna-master.dir/build.make:70: CMakeFiles/test_lasagna-master] Segmentation fault (core dumped)
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test_lasagna-master.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Testing the code locally.

$ make
[ 25%] Building CXX object CMakeFiles/lasagna-master.dir/lasagna_master_test.cpp.o
[ 50%] Building CXX object CMakeFiles/lasagna-master.dir/lasagna_master.cpp.o
[ 75%] Building CXX object CMakeFiles/lasagna-master.dir/test/tests-main.cpp.o
[100%] Linking CXX executable lasagna-master
[100%] Built target lasagna-master
===============================================================================
All tests passed (1 assertion in 1 test case)

[100%] Built target test_lasagna-master

Here is the code!

#include "lasagna_master.h"
#include <vector>
#include <algorithm>

namespace lasagna_master {
    int preparationTime(std::vector<std::string> layers, int minutes) {
        return layers.size() * minutes;
    }

    amount quantities(const std::vector<std::string> items) {
        amount a = {0, 0.0};
        for (auto& i : items) {
            if (i == "noodles") {
                a.noodles += 50;
            }
             if (i == "sauce") {
                a.sauce += 0.2;
            }
        }
        return amount{a.noodles, a.sauce};
    }

    void addSecretIngredient(std::vector<std::string>& myList, const std::vector<std::string> friendsList) {
        myList[myList.size() - 1] = friendsList.back();
    }

    std::vector<double> scaleRecipe(std::vector<double> portions, int scaleTo) {
        std::vector<double> newPortions = portions;
        for (const auto& i : portions) {
            newPortions[i] = i * scaleTo;
        }
        return newPortions;
    }

    void addSecretIngredient(std::vector<std::string>& myList, std::string secret) {
         myList[myList.size() - 1] = secret;
    }
}  // namespace lasagna_master

Header file.

#pragma once
#include <string>
#include <vector>

namespace lasagna_master {

    struct amount {
        int noodles;
        double sauce;
    };
    
    int preparationTime(std::vector<std::string> layers, int time=2);
    amount quantities(std::vector<std::string>);
    void addSecretIngredient(std::vector<std::string>& myList, const std::vector<std::string> friendsList);
    std::vector<double> scaleRecipe(std::vector<double>, int scaleTo);
    void addSecretIngredient(std::vector<std::string>& myList, std::string secret="?");
        
}  // namespace lasagna_master

Are you running all of the tests? It seems like you only run one of them.

You can move the line to the next place to see your success locally: cpp/exercises/shared/.docs/tests.md at dea073d118807ca4bf0c5f6b476bad9343abfcda · exercism/cpp · GitHub

Segfaults mostly happen, when you try to access something that is not there.
Imagine an empty array for example. What happens if you access the element at position “-1”?

1 Like

Hi Vaeng!

I was not running all the tests. I did not know that. Looking at the output of the make command I saw the text All test passed.

===============================================================================
All tests passed (1 assertions in 1 test cases)

That lead me to believe that all of the tests passed but they did not.
However, in the parenthesis now I see (1 assertion in 1 test cases) .
I did not know I needed to move the #ifdefined line down.

Now I can keep working on the exercise.

Thanks!