2

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.

2
  • 4
    Are you compiling with optimisations enabled? Commented Apr 28, 2011 at 19:29
  • 1
    Yes, when with -O3 removed, the problem is gone. Interested in why. Like which value is the real address of memory, with optimization enabled... Commented Apr 28, 2011 at 21:00

1 Answer 1

5

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.