Solved

Memory allocation usin malloc

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Guacamole cut and paste issue 3 74
CentOS 7 Installation 7 73
Hacked File Timestamps 4 62
nested if statement in excel help 4 27
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

777 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