Thanks in advance for any help.

I am running a fairly good-sized MPI-C++ code, and I am getting a frustratingly unreproducible segfault. I am also having trouble with gdb (I think because of my MPI implementation), but I think the problem may have something to do with the fact that my argc variable (I have three command-line arguments) is constantly changing, according to gdb, at different places in the code. It is sometimes a huge number, sometimes negative, and sometimes I get 'argc=Cannot access . . . .'

This is wrong, isn't it? argc should be constant as the code runs, and should be equal to the number of arguments I'm passing over the command line, correct? If so, any idea about what is changing it?

Just a 'yes' or 'no' answer to the first of those two questions will do for now. I appreciate the help.

>This is wrong, isn't it?
If the value of argc really is changing (sometimes debuggers can be misleading) then looking for overflow would be my first task. It's entirely possible for memory overflow to spill into other variables and corrupt your data.

Put a data access breakpoint on the address of argc, then wait for something to happen.

Right. So if argc is changing, overflow is probably the reason. Maybe if I spend a few more hours staring at it, I can figure out where!

Thanks, all.


Thanks for the suggestions so far -- I set a watchpoint at argc in gdb. The strange thing is -- I never hit the watchpoint, but at some point close to the beginning of the program I start to get a message at each step -- 'Cannot access 0x0' or something similar. The message disappears if I remove the watchpoint.

In the meantime, my code is still crashing unpredictably.

Well where are you in the code are you when it starts to produce those messages?

It seems to me it's already going wrong by that time.

You know, having found some sources of crashes not at all related to the arguments, I am now thinking that this has something to do with the debugger (gdb). Depending on further analysis, I may re-open (if I can do that), but for the moment I'll mark this as solved. Thanks to you both for the help.