Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » malloc() & address allocation
Closed Thread
Old 03-21-2005, 08:56 AM   #1 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 3

santechz

Default malloc() & address allocation

Hi there.
I know that malloc() takes its memory chunks from the heap. assuming that there are two consecutive malloc calls, one immedietly after the other, one would assume that the chunks would be allocated nearby like blocks in an array,

what i mean is
//assume sizeof(char)=1
char * p1 = (char*)malloc(sizeof(char)); //now let p1 =1460 (address)
char * p2 = (char*)malloc(sizeof(char));

now p2 should be =1461 (or atleast 1462 for even address boundary), right? but instead, my Turbo Compiler gives the allocated address as 1468 (i.e. an 8 byte gap between the addresses consecutive calls).

experimenting a lot, there always seems to be either a 4 byte or 8 byte gap between the consecutively malloc()'d allocated addresses. , so if we allocated a 3 int array using malloc(3*sizeof(int)), and then one more malloc, the first malloc ()returned address would be 1460, and the second malloc will return 1468.

I searched in the net and found out that ANSI doesnt specify how the memory must be allocated between malloc calls, it is specific to single malloc calls.

could anyone tell me why there is a gap, ?? I do hope u got my doubt....

regards
santechz is offline  
Old 03-21-2005, 11:48 AM   #2 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Try declaring BOTH pointers, then calling malloc twice. My first thought is that the gap between is the pointer p2.
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 03-21-2005, 12:01 PM   #3 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 3

santechz

Default

this is how i visualize my question


char * p1 = (char*)malloc(sizeof(char)); //sizeof(char)=1 byte
char * p2 = (char*)malloc(sizeof(char));

now i get p1 = 1460
then p2 must be 1461 (or 1462 for adjustment) if there were no allocation to heap in between these two statements, bcoz this would be the easiest to implement the heap management....

but i get p2 = 1468. in other words, there are 7 bytes which I believe I can use. Why did malloc return 1468 and not 1461(/1462)? Can i really use these 7 bytes. will the heaper (?) allocate these 7 bytes (even when i havent freed p1 and p2) when i request a ..say malloc(4 * sizeof(char))?

regards
santechz is offline  
Old 03-21-2005, 02:17 PM   #4 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

Try keeping the code you have, then returning the address of p2 after (not the address of the memory p2 points to). That will anwser a lot of questions.
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 03-23-2005, 09:35 AM   #5 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

If a char is 1byte, this is 8 bits.

1468 - 1460 = 8

It would make sense if the numbers were bits rather than bytes, since diferent data types use a different number of bits
fitzjj is offline  
Old 03-24-2005, 05:14 AM   #6 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 3

santechz

Default

Hey!
malloc() returns the address of a requested byte(s). you can only request for bytes. Please do try the snippet and see it for your self:
char *ptr1 = (char *) malloc(6*sizeof(char)); //assume 1char=1byte
char *ptr2=(char*) malloc(sizeof(char));//

now there will be either 8 byte difference between ptr1 (not &(ptr1)) and ptr2. i.e. ptr2-ptr1=8. and the "gap" between the 1'st requested(6 bytes) and the successive request will be 2 bytes....
santechz is offline  
Old 04-12-2005, 07:02 AM   #7 (permalink)
 
Newb Techie

Join Date: Apr 2005

Posts: 10

amit jain

Default

nice question .. I will work on it
amit jain is offline  
 
Closed Thread

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On