Solved

Memory allocation usin malloc

Posted on 2012-03-19
5
486 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

617 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