Segmentation fault

Needed some help with a c++ program with segmentation fault. Unfortunately Can't post the code here. Anyone willing to find out the mistake? and post the answer here if you want to.
Hi,

So you made your last post here:

http://www.cplusplus.com/forum/unices/175212/


And provided the output from gdb - so not enough context for anyone to be able to help.

Now, you post again, this time with no context whatsoever - how were you expecting to get help from anyone?

Why can't you show even a small piece of code?

It seems you are having trouble allocating a string.
I can mail the code to someone. I know it sounds stupid but if anyone is willing to help then let me know.
Hi,

Well I don't understand why you can't post even a few lines of code. You don't have to post the whole thing (in case you are worried about someone plagiarising your code ), just the relevant part plus anything related to it - as in declarations and definitions of the things involved in the problem line of code.

Anyway, seen as you are already using the debugger - why don't you go with that. Read up on how to use it, (or use a GUI one inside an IDE). Some useful commands are bt (backtrace) and step to step through the code 1 line at time. print to print values of expressions including your variables.

I have to go out now (I might not be back for another 6 hours), see how you go.

Edit:

It seems you are having a problem with a string, look at the reference material regarding what you are trying to do there. There is lots of simple reference material at the top left of this page. There is also cppreference.com for an authoritative technical of standard complying C++.
Last edited on
Ok I am back a lot earlier than I thought.

Your link 404'd for me.

How did you get on with my other suggestions?

Are you using an IDE? and which compiler? I might be able to help you with learning to debug, I have 4 different IDE's on my system.
Last edited on
OK, I have your code.

I am doing it on a Linux system.


Yes, I gathered that, but are you using an IDE , or are you just using the shell? Which compiler are you using - g++ probably?

How did you go with my other suggestions?

Have you managed to track down where the segfault is happening?

Edit:

What arguments are you sending the program - filenames etc?
Last edited on
Yes g++. I need to download some extra stuff on top of gdb to find out where the segfault is occuring exactly but I am not the adminstrator. Arguments are two different files:

http://www.megafileupload.com/hPZ2/ibm10.are
http://www.megafileupload.com/hPZ3/ibm10.net

These actually describe a circuit. I have to read and store them.
Last edited on
I need to download some extra stuff on top of gdb to find out where the segfault is occuring


if you run the program through gdb, wait for it to segfault, then do a bt - back trace. This will give some clues as to what is happening. Use print to show the values of the variables concerned.

One big observation about your code: You need to break it up into functions. And it needs comments and/or better variable names so one can decipher what is going on.

I will load you code onto my system and see what I can discover.

I get this:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid

Program received signal SIGABRT, Aborted.
0x0000003178632625 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.1.x86_64 libgcc-4.4.7-16.el6.x86_64 libstdc++-4.4.7-16.el6.x86_64


.net file is the argv[1], the .are file is argv[2]. The above files I provided are too big. Here are two other files where the program successfully runs:
http://www.megafileupload.com/5fy0/EACG21.are
http://www.megafileupload.com/5fy1/EACG21.net

Just pass them as command line arguments. .net file first then the .are file
Ok, I compiled with the Warnings -Wall - Wextra -pedantic turned on - you should always use these as a minimum.

These warnings were shown:

../Circuit/main.cpp:59:15: warning: variable length arrays are a C99 feature [-Wvla-extension]
net* Cell_list[number_of_cells][15]; //Random size
              ^
../Circuit/main.cpp:60:15: warning: variable length arrays are a C99 feature [-Wvla-extension]
cell* Net_list[(number_of_nets)+1][15]; //Random size
              ^
../Circuit/main.cpp:31:15: warning: unused parameter 'argc' [-Wunused-parameter]
int main (int argc,char *argv[])
              ^
3 warnings generated.


59
60
net* Cell_list[number_of_cells][15]; //Random size
cell* Net_list[(number_of_nets)+1][15]; //Random size 


So you are trying to create a variable length array on lines 59 & 60. Either allocate the memory dynamically or use some other STL containers. Or just make that variable a const and known (at compile time) size, seen as your comment indicated you wanted a random size.

Please note: if this fixes the problem, It doesn't mean the code is as good as it could be - you should still go back and split it into functions, and use some better variable names. You did have plenty of comments, but ideally someone who doesn't know the technical details of what you are doing, should still be able to have a fair idea of what is happening. Good code should tell a story. Avoid having using namespace std; read up about why that is bad, and what to do instead. Ideally your code should be in it's own namespace as well.

Ok, so the main lesson here is to set the compiler warnings to their highest.

Hope all goes well :+) Regards
What is the easiest way to go around that problem? I am pretty much burned out right now. Allocate dynamically using new and delete? Can you type that part of the code for me :S

Thanks for the help btw
Last edited on
Hi,

Another thing, try to avoid using pointers if you can, use references instead. And use the STL containers as much as you can - they have plenty of safeguards in them.

You can also make use of assert this will test if a value is what it should be, and the program will end immediately if it isn't, forcing you to fix the problem. They aren't in the release version of the code. This way, you can test your code with different data to see that it works.
I used 1 billion as its value but the problem still persists
Allocate dynamically using new and delete?


I wouldn't recommend doing that - it has problems of it's own. Ideally, you can just use an appropriate STL container, and avoid all these hassles.

The containers implicitly allocate their memory on the heap - you don't have to worry about it. They also have safeguards in that one cannot overrun the end of the container, they have things such as size() functions, one can use range based for loops instead of iterators over the whole container.

One other thing, avoid magic numbers like 15 in your code, make them a const variable, and use that variable name in the code.

Unfortunately, one cannot have a container of references, but there is std::reference_wrapper

http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper

So with your 2D array, it seems it has a const 15 columns, but the number of rows varies. So you could have a std::vector of std::array (size 15) , of type of the std::reference_wrapper above.

std::array can have a variable as the size of it to start with, but one can't change that afterwards.

Good Luck !!
I used 1 billion as its value but the problem still persists


Was that for an assert? In what context?

Anyway, try to make use of the containers in my last post, and split into functions, and go from there.

I have to head out again soon, I think you have quite a bit to do - I look forward to seeing your new code :+)
I am too burned out right now to write any sort of code.
Ah well, sleep on it then ZZZZzzzzzz.......
Topic archived. No new replies allowed.