I have a struct that is declared on the stack. Here is what the struct looks like:
struct MyStruct {
int integer;
std::vector<bool > booleanVector;
};
When I step through the following function with gdb, printing the value of s.integer and s.booleanVector.size(), the statements seem to have no effect.
MyStruct getMyStruct()
{
MyStruct s;
s.integer = 3;
s.booleanVector.resize( s.integer );
return s;
}
However, when I insert print statements such as std::cout << s.integer << std::endl; the output shows that the values of s.integer and s.booleanVector.size() have been properly changed.
I discovered that the problem seems to be associated with the struct being returned by the function in which it is declared. The problem with gdb displaying invalid information about the struct does not occur if it is not being returned by the function in which it is declared.
This is a simplified example that exhibits a problem I had while attempting to debug a project. This problem with gdb was distracting me from an actual error in my code and making it difficult to find (originally I thought the problem might be a subtle bug in my code, which is why I posted it here). Here is a complete small program that exhibits this behaviour on my system.
I guess my question is if this is just a bug with gdb or does this behaviour occur as a result of how gcc handles structs / classes that are to be returned from a function.
Note: Using gdb 6.3 with gcc 4.2 in Mac OSX 10.6 (Snow Leopard)
This debugging issue is fixed by upgrading to a more recent version of gcc. When the code is compiled with gcc 4.7, gdb displays correct information about the struct.