LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-23-2005, 04:23 AM   #1
hddd
LQ Newbie
 
Registered: May 2005
Posts: 1

Rep: Reputation: 0
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.
 
Old 05-23-2005, 04:38 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
does two malloc statements return same address? hetzme Programming 5 08-16-2005 11:22 AM
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 02:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 02:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 07:40 AM
malloc() vijeesh_ep Programming 4 08-25-2004 04:50 PM


All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration