An odd segmentation fault!!

You can find the files here
https://github.com/KAY-YAK/c-

The error is in occurring when the function remove() is called from within pop_back() in Menu.cpp from main which is inside menu.
Last edited on
since this is a "unix/linux programming", compile this with gcc -g, run in gdb, reach the segfault, type "bt" (for backtrace) and it will tell you what's wrong. You can then type "frame 2" for example to go up two stack frames to examine local variables there with "print" and the source code with "list", etc

Even if someone has the time to build and run your program, it appears to be interactive and you didn't explain what inputs led to the failure.

Running it with input "1" and "1" again leads to a segfault in strlen in Text::set , because t.text is a null pointer. That is called from operator=, called from Menu::insert where option_list[2].text is null, but only you can tell why that is.

A few notes on the code:
1. the biggest problem is the use of new/delete. They are not needed here at all. Use vectors, lists, stacks, strings etc. You actually #include <string>, but never use any strings!
2. atoi never throws, all those try/catch do nothing (stoi is the one that throws.. or you could just let the stream do the job it's meant to do)
3. push_back should take const char*, otherwise this is invalid C++ (gcc refuses to compile with -pedantic-errors) since you're calling it with string literals which are arrays of const char.
4. "this->" is redundant and looks silly
Last edited on
Topic archived. No new replies allowed.