Richard
Richard

Reputation: 15562

evaluate variable/function in gdb

I have the following code,

...
char* seg = mmap(0, ...)
printf("seg=%x\n", seg);
...

The program prints with seg=b7ffd000. While in gdb (for the same execution), when using p/x seg, it prints $2 = 0x0. I am confused here. Isn't it the same var seg? why are the values different.

PS: in mmap, the first argument is the preferably address of mapped memory and the return value is the actual address of mapped memory.

Upvotes: 2

Views: 684

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272477

Now that you've answered my question in the comments, I can answer!

The value that you see as the result of the printf is the real address. You are seeing 0 as the value of seg in the debugger because when optimizations are enabled, the compiler is free to do all sorts of weird things (which generally makes step-by-step debugging tricky). The "observable" behaviour should always be correct, though (assuming you're not relying on any undefined behaviour).

Upvotes: 5

Related Questions