0

I'm using OS X 10.10.5 now, and I'm doing an experiment about malloc and heap like below:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p;
    p = malloc(sizeof(int));
    *p = 100;
    printf("the malloc p pointer addr is %x\n", p);
    getchar();
}

the out put is the malloc p pointer addr is c3404ba0;
I think address 0xc3404ba0 is allocated by maclloc as part of the process heap.

Then I use the vmmap tool to get the memory map, the result is:
enter image description here

We can see that the 0xc3404ba0 is not in the heap area, but it's realy the pointer to the allocated memory. What's wrong with that?


It fix now, when I use %p instead of %x, the output is the malloc p pointer addr is 0x7fe131404ba0
and the vmmap output is:
enter image description here
thanks to @Alex Lop.

bazysong
  • 379
  • 2
  • 14
  • 4
    Looks like you are running on 64bit OS. If your program was compiled in 64bit mode then using `%x` to print the pointer will cut off the upper 32 bits. Try the same with `%p` instead and post the results. – Alex Lop. Mar 30 '16 at 07:40
  • using the wrong format specifier invokes **undefined behavior** http://stackoverflow.com/q/33648596/995714 http://stackoverflow.com/q/4231891/995714 http://stackoverflow.com/q/14504148/995714 http://stackoverflow.com/q/16864552/995714 – phuclv Mar 30 '16 at 08:01

1 Answers1

4

As I wrote in the comment, looks like you are running on 64bit OS. If your program was compiled in 64bit mode then using %x to print the pointer will cut off the upper 32 bits (since %x is used for integers). Try the same with %p instead and post the results.

My guess would be that 0xXXXXXXXXc3404ba0 belongs to that range:

enter image description here

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45