Thread: Dynamic memory allocation...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    1

    Dynamic memory allocation...

    Hi all,
    When i was just searching for "How does free know how many bytes to free ", i came to know some facts abt dynamic memory allocation. But i need some help from u guys to clear my doubts.

    1.The memory is allocated in chunks of 8 bytes , But why?
    2.And i came to know that the memory manger stores the number of bytes allocated in the address right before the starting loation address of the block.
    Here is the code which i found in net
    Code:
    int *ch;
    	int i;
    	for(i=1;i<7;i++)
    	{
    		ch=(int *)malloc(sizeof(int)*i);
    		printf("%d --- %d\n",i,ch[-1]);// i -- the number of intergers, ch[-1]   
    // is the value of address loaction right before the starting address 
    // loaction of the block.
    		free(ch);
    	}
    The output of the above code is :
    i --- ch[-1]
    1 --- 17
    2 --- 17
    3 --- 17
    4 --- 25
    5 --- 25
    6 --- 33


    My doubt is when i allocated 1,2,3 bytes of int in ch[-1] it contains 17 and when i allocted 4,5 bytes of int in ch[-1] it contains 25 and so on.

    Just i dont understand the abt output .
    Plz clear my doubts.Thanks in advance.


    OS -- FC3 2.6.12-prep
    Machine -- 32 bit X86.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So much for facts.

    > .The memory is allocated in chunks of 8 bytes , But why?
    It might be some other number, it doesn't have to be 8.
    Some reasons for aligning allocations:
    1. To ensure that all requests are aligned to the alignment requirements of the most restrictive data type supported by your machine. An int for example is often limited to being on an even address (or a quad address). Doubles are perhaps even more restrictive.
    2. To resist the effects of memory fragmentation from allocating and freeing many small blocks of memory.

    > And i came to know that the memory manger stores the number of bytes allocated
    You can't know this for certain.
    Sure, you figured out how YOUR current implementation does it, but not everyone does it the same way. If you rely on this in your code, then you'll come unstuck pretty quickly.

    > My doubt is when i allocated 1,2,3 bytes of int in ch[-1] it contains 17 and when i allocted 4,5 bytes of int in ch[-1] it contains 25
    Who knows, maybe it relates to the number of times you called malloc rather than the number of bytes you requested.
    There's no way to tell what is going on from such a limited series of tests.

    In fact, there is no guarantee that trying to access ch[-1] wouldn't cause your program to crash with a segfault.

    All that you need to know is that malloc() stores the size somewhere private, and free() knows how to get at that information. There may be a non-standard API function that allows you to get at this information, but there is NO way in standard C for you to do it.

    > ch=(int *)malloc(sizeof(int)*i);
    Read the FAQ on why casting malloc in C is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM