Thread: malloc and realloc

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    16

    malloc and realloc

    Hi All,

    Could anyone tell me the difference between memory allocation scheme used by malloc and realloc. I would like to know how realloc reasssigns the memory to a pointer variable. In other words, what will happen if the availabe memory is not contiguous?

    What will be the situation with malloc if the available memory is not contiguous?

    Finally, is the memory allocated by calloc is contiguous always?


    Please answer to this.

    Thanks in advance.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    malloc will assign the amount of memory that you need like void *malloc(size_t n) and returns NULL if there is not enought mem for the req. void * malloc is a great way to have a pointer to the array when you dont know what type the pointer should be. Where realloc (which I think is still just used in boreal C and not in ANSI) will in real time re reallocate memory(increasing and decreasing), thus as the program is running you can increase the size of the memory set aside but you could run into issues if you went to increase the memory to a point where memory is allready in use and thus you could cause A. Program to crash, B. delete values to other arrays, C. Unpredictable results. From the lectures I have sat in, it would be better to use pointers to set aside memory.
    Last edited by KoG Metalgod; 01-05-2007 at 08:13 AM.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by KoG Metalgod
    Where realloc (which I think is still just used in boreal C and not in ANSI) will in real time re reallocate memory(increasing and decreasing), thus as the program is running you can increase the size of the memory set aside.
    realloc is an ANSI C function, and as best I recall, always has been.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by KoG Metalgod
    thus as the program is running you can increase the size of the memory set aside but you could run into issues if you went to increase the memory to a point where memory is allready in use and thus you could cause A. Program to crash, B. delete values to other arrays, C. Unpredictable results. From the lectures I have sat in, it would be better to use pointers to set aside memory.
    __________________
    if the current regeon cannot be increased according to request, realloca will allocate the required amount of the memory in another plaace, copy the source data there and free the source.
    if the new regeon cannot be allocated the realloc returns NULL exactly as malloc does.
    So the program can crash only if it does not check the return value of the realloc (but the same issue is with malloc)

    It is impossible to get the memory overrun due to inproper reallocation. Only due to not checking bouds of the allocated memory. But it also the same issue as by using malloc. So I'm not really understand what you intended to say here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, realloc..wat on earth!
    By zesty in forum C Programming
    Replies: 3
    Last Post: 12-21-2007, 01:42 PM
  2. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  3. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM