0

Possible Duplicate:
realloc and malloc functions

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *p;
  p = malloc(6);
  p = realloc(p, 10);
  if (p == NULL)
  {
    printf("error"); // when does p point to null consider i have enough space in prgrm
                     //memory area but not in memory where realloc is trying to search 
                     //for the memory, I dont know how to explain that try to undrstnd
   exit(1);
   }
}

Take this example for the code, suppose the total memory is 10 bytes and 2 bytes is used by the declaration of pointer to type int and ohter 6 bytes by malloc function the remaining 2 bytes is occupied by other programs, now when i run realloc function to extend the memory that pointer is pointing to, it will search for 10 bytes in memory and when it is not available it allocates 10 bytes of memory from heap area and copies contents of malloc and paste it in new allocated memory area in heap area and then delete the memory stored in malloc right?

Does realloc() return a NULL pointer because the memory is not available? No right!? It does go to heap area for the memory allocation right? It does not return a NULL pointer right?

Listen to me: | 01 | 02 | 03 | 04 | 05 | 06 | 07 |08 |09 | 10 |

consider this as memory blocks: assume that 01 to 06 has been used by malloc() func, 07 and 08 are free and last 2 blocks i,e 09 and 10 are being used by memory of other programs. Now when i call realloc(p,10) i need 10 bytes but there are only 2 free bytes, so what does realloc do? return a NULL pointer or allocate memory form the heap area and copy the contents of 01 to 06 blocks of memory to that memory in the heap area, please let me know.

Community
  • 1
  • 1
john
  • 53
  • 1
  • 6
  • The only way `realloc()` can indicate an error (of any kind) is to return NULL – Andrew Brock Aug 25 '12 at 13:56
  • http://linux.die.net/man/3/realloc + http://msdn.microsoft.com/en-us/library/xbebcx7d.aspx – perilbrain Aug 25 '12 at 14:00
  • Listen to me: | 01 | 02 | 03 | 04 | 05 | 06 | 07 |08 |09 | 10 | consider this as memory blocks: assume that 01 to 06 has been used by malloc() func, 07 and 08 are free and last 2 blocks i,e 09 and 10 are being used by memory of other programs. Now when i call realloc(p,10) i need 10 bytes but there are only 2 free bytes, so what does realloc do? return a NULL pointer or allocate memory form the heap area and copy the contents of 01 to 06 blocks of memory to that memory in the heap area, please let me know. – john Aug 25 '12 at 14:09
  • 1
    The memory allocation routines return NULL when they cannot find enough memory for the request — either from what's already in the heap area under their control (but currently unused) or by requesting more space from the O/S to add to the heap area under their control. – Jonathan Leffler Aug 25 '12 at 14:10
  • How is this question different to your last one 20 minutes ago? – halex Aug 25 '12 at 14:20
  • Perhaps you might want to think why people are reading the question differently to what you are asking and clarify it – mmmmmm Aug 25 '12 at 14:26

3 Answers3

2

RETURN VALUE

...

The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. If size was equal to 0, either NULL or a pointer suitable to be passed to free() is returned. If realloc() fails the original block is left untouched; it is not freed or moved.

sourcejedi
  • 3,051
  • 2
  • 24
  • 42
  • I ask something and you people answer something irrelevant to the question and that something i already know all of you most of the time answers do not make and sense to the question – john Aug 25 '12 at 14:16
  • 1
    It answers the question - if realloc fails it returns NULL - as there are only 2 free bytes it can't find 10 – mmmmmm Aug 25 '12 at 14:19
  • 1
    We're trying to make sense of the question. Note "returns a pointer to the newly allocated memory, which... may be different from ptr". I.e. the object will be moved (copied) if necessary ("[The contents of the object shall remain unchanged...](http://pubs.opengroup.org/onlinepubs/009695399/functions/realloc.html)"). I think that answers your question. – sourcejedi Aug 25 '12 at 14:25
2

malloc

  1. This will allocate the memory block if it is available, or else it will return NULL.

realloc

  1. If the size passed is greated than the existing block then this will tries to expand the existing memory, if it succeeds in expanding it will return the same pointer.
  2. Or else if its failed to exapand then it will allocate the new memory block and it will copy the old data from the old memory block to new memory block. Then it will free the old block and it will return the new blocks address.
  3. If allocating new memory block failed then it will simply return NULL, without freeing old memory block.
  4. If size passed is zero to the realloc function, then it will just free the old memory block and return NULL. realloc(ptr, 0) is equivalent to free(ptr).
  5. If the size passed to the realloc function is lesser than the old memory block`s size, then it will shrink the memory.

Answer to your scenario

Listen to me: | 01 | 02 | 03 | 04 | 05 | 06 | 07 |08 |09 | 10 |

consider this as memory blocks: assume that 01 to 06 has been used by malloc() 
func, 07 and 08 are free and last 2 blocks i,e 09 and 10 are being used by 
memory of other programs. Now when i call realloc(p,10) i need 10 bytes but 
there are only 2 free bytes, so what does realloc do? return a NULL pointer 
or allocate memory form the heap area and copy the contents of 01 to 06 
blocks of memory to that memory in the heap area, please let me know.

Yes it will copy content of 01 to 06 from old memory block to new memory block and it will free the old memory block and then it will return the address of new memory block.

rashok
  • 12,790
  • 16
  • 88
  • 100
0

The way malloc is implemented is by definition system or implementation dependent. (A silly, but standard conforming, implementation of malloc would always fail by returning NULL; most real implementations are better than this).

Read first the behavioral specifications of malloc, realloc, free (Posix standard).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547