tags:

views:

66

answers:

2

Possible Duplicate:
Is <value optimized out> in gdb a problem?

Hi,

I am still learning advanced c++ programming. I am getting segmentation fault in my C++ code. While using gdb, the point of failure is during de-allocation with one of the parameters to the de-allocator(malloc) is a value that is optimized out(av=value optimized out). My question is, does this condition(value optmized out) leads to a segmentation fault in the de-allocator? The reason why I ask this is since I am able to print the values, but gdb is not able to read them for some reason.

Thanks.

+4  A: 

No, the value optimized out simply means that the optimizer passed the parameter through a register, which means gdb cannot see its value. If you compile with -O0 you'll see the value in gdb.

The error is likely yours. E.g. freeing an address that's already been freed, or freeing an address that was never allocated.

On a side note, for C++ it is recommended to use new, delete, new[] and delete[].

You need to provide more context.

Staffan
@Staffan: generally correct, but when something is "optimized out", it doesn't always mean that it was passed through a register. Sometimes, the value has been factored out to the point where that particular value doesn't exist in any practical sense.
Evan Teran
@Evan Thanks...I noticed this condition(value optimized out) for the first time in gdb, and esp with every seg fault..so just wanted to understand whether its me or the environment..looks like its me :-)
@Staffan Thanks...I noticed this condition(value optimized out) for the first time in gdb, and esp with every seg fault..so just wanted to understand whether its me or the environment..looks like its me :-)
+1  A: 

Values that are optimized out can't introduce a segfault. It means that the argument to malloc isn't used after the call, so rather than storing it in the caller's stack frame it was just passed directly to malloc using a register, so gdb can't show the memory address (it's not stored in memory)

Michael Mrozek
Reading a value that's optimized out can't cause a segfault, but if the value is a pointer, dereferencing it could cause one.
Karmastan
@kar If it were dereferenced somewhere in the code it wouldn't have been optimized out
Michael Mrozek
In my experience, "optimized out" just means that the exact value the debugger wants to display doesn't exist in memory at that exact instant. It could be that the value isn't live anymore, so it doesn't exist. It is also possible that the value exists only in a register. I have seen this happen when using gcc and gdb.
Karmastan
@Michael..thanks.It was useful.