Solved

Memory allocation usin malloc

Posted on 2012-03-19
5
469 Views
Last Modified: 2012-08-13
Hi Experts,

I am experimenting with how memory is allocated on the heap.  It appears, when I do malloc back to back, the first one returns an address in one page, and the second one returns an address in another page(4096 bytes page size).  I was expecting( most likely incorrectly) that it will be in the same page, but would be separated by the allocation table data.  Does every malloc goes and fetch addresses from different pages?
Thank you.


        int i, j;

	int * firstArray = malloc(sizeof(int)*20);
	int *secondArray = malloc(sizeof(int)*10);
	for( j = 0; j < 5; j++)
	{
		secondArray[j] = 11;
	}
	for( i = 0; i < 25; i++)
	{
		firstArray[i] = 10;
	}

Open in new window

0
Comment
Question by:ambuli
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 37740673
Yup, the 'usual' algorithms for allocating on the heap will either a) rely on undelying OS code or b) offer a scheme where it is most worthwile to fragment the heap as least as possible. So, in your case, the allocator code might have found regions that are better suitable to place your request in other than two consecutive pieces of memory (yet that might happen as well). As a bottom line, assumptions about what heap allocators return are futile at best, better not worry about that in the first place.
0
 

Author Comment

by:ambuli
ID: 37740712
sure:-) i was curious as to why it is allocated on a new page. thank you. one more question. I believe the allocation info is usually placed in the block before or after the allocated block. how much of memory is used for this data and what information is stored there. is there any reference doc for this.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 37740720
This is completely implementation dependant. I was about to mention that such a 'context record' could have been in the way between you and your contiguous allocations, but if it was me to create a (sub-)allocator, I'd keep that separated from the actual memory pool I was to supervise about. So, I guess my best advice is to make no guesses about what your underlying heap does nor how it is implemented.
0
 

Author Comment

by:ambuli
ID: 37740742
thanks.  i was studying about heap corruptions and that is why i was interested in.
0
 
LVL 86

Expert Comment

by:jkr
ID: 37740768
As a 'real life' example - I had to design an allocator for a UDF2 file system for real time video recording, and *speed* therefore was the primary  factor - so, find the first slot that satisfies your requirements and move on... (in hindsight, I see much room for optimization ;o)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now