Solved

Memory allocation usin malloc

Posted on 2012-03-19
5
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
expand ext4 on centos 6 5 53
C++ Code Issue 4 37
Ubuntu don’t allow SU command in terminal 7 63
SMTP log file for IMSVA 5 29
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

756 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