LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why malloc() return different pointer value? (https://www.linuxquestions.org/questions/programming-9/why-malloc-return-different-pointer-value-326152/)

hddd 05-23-2005 03:23 AM

why malloc() return different pointer value?
 
Such a program:

#include <stdio.h>
main()
{
void* p;
p=malloc(16);
printf("%p\n",p);
}

Compiled on linux then run it. The result will different for every times and the difference may be some pages.

On other unix system, such as solaris or HP-UX, the result for every running are same.

I think that unix process would have same mermory space mechanism, but why I got this result on linux??

Thanks for any help.

trevelluk 05-23-2005 03:38 AM

malloc () makes absolutely no guarantee that you'll get the same address each time. Maybe on some systems you will often get the same value, but I very much doubt that this is guaranteed. In general, you shouldn't actually be examining the return value from malloc () (all you need to know is that you've got a memory location to use, you shouldn't care where it is), except to check that it isn't NULL.


All times are GMT -5. The time now is 01:51 PM.