Thread: Malloc for extending the size....!

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    Malloc for extending the size....!

    Code:
    {
    char (*a)[100];
    
    a = malloc (2 * 100);
    a[1] = "Hi ";
    a[2] = "Hello";
    
    /*need to put more records to a, so go for resize...!*/
    
    a = malloc (4 * 100);
    
    }
    I understand malloc allocates memory and doesn't reset the contents, but
    here the question is

    1. Can malloc be used for extending size without loosing the existing contents
    (as realloc)....?

    2. Does elements "a[0], a[1]" still be in the same address and "a[3], a[4]" to
    the new address...!
    or
    Entire "a" ("a[1 to 4]") will be moved to some other new contiguous
    memory...?

    Thanks for any suggestions.....!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by patil.vishwa View Post
    Code:
    {
    char (*a)[100];
    
    a = malloc (2 * 100);
    a[1] = "Hi ";
    a[2] = "Hello";
    
    /*need to put more records to a, so go for resize...!*/
    
    a = malloc (4 * 100);
    
    }
    The red part of your code is incorrect. You are assigning an array of chars (e.g. "Hi") into another array. I doubt this code compiles. You should be using strcpy.
    I understand malloc allocates memory and doesn't reset the contents, but
    here the question is

    1. Can malloc be used for extending size without loosing the existing contents
    (as realloc)....?
    That is exactly what realloc is for - you can't do the same with malloc - that is WHY there is a realloc, if malloc did the same thing, you wouldn't need a realloc function, right?

    The "doesn't reset the contents" is more correct to mean "doesn't do anything to the contents of your new memory". It gives you a "new" piece of memory, but since it may have been used before, it is by no means guaranteed have any particular content. It may contain all zeros, but it may also contain any other random garbages (strings, addresses to pieces of memory, floating point data, or any mixture thereof - whatever happened to be in the memory when it was freed). Usually "fresh" memory from the OS will be filled with zero (or some other defined value) so that you can't get information from other applications by just allocating large portions of memory and reading the content.
    2. Does elements "a[0], a[1]" still be in the same address and "a[3], a[4]" to
    the new address...!
    or
    Entire "a" ("a[1 to 4]") will be moved to some other new contiguous
    memory...?
    No. You have just allocated a new area of memory that is 400 bytes. That is a different chunk of memory than your original 200 bytes. If you use "printf("%p", (void *) a)", then you can see what the actual address is - and it will be different for the first malloc and the second malloc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    is it sure that realloc stretches the memory size always??
    i heard that it depends on the memory block size that has been assigned... am i right??? if whatever i have understood is right then tell me how the MMU keeps track of that????

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, it doesn't always stretch the memory. What would happen if you do this:
    Code:
    a = malloc(100);
    b  = malloc(200);
    a = realloc(a, 10000);
    b may very well have the next 200 bytes immediately after a (in fact it is very likely to be the case).

    So realloc will either stretch the existing memory allocation, or create a new one. There is no way around that.

    MMU, for this particular purpose is irrelevant, the key here is that the underlaying code inside malloc/realloc and friends that know if the memory immediately after the memory you actually have allocated is free or not. If that memory is available, then the current block can be extended. If it's not free, then it can't be extended, so a new block of memory has to be allocated.

    There is a side-issue here, and that is that small amounts of extending the memory may use up "spare" space that malloc left free because it rounds the size up to some suitable value (e.g. you ask for 2 bytes, but malloc gives you back a piece of memory that is 16 bytes long, so realloc up to 16 will use the extra space which is just sitting there ready to be used).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Thanks for the clarification....!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. char problem
    By eXistenZ in forum Windows Programming
    Replies: 36
    Last Post: 02-21-2005, 06:32 AM