2

In this code snippet ,realloc is equivalent to malloc but I am unable to get the logic .

int *ptr=(int*) realloc(NULL,10*sizeof(int));

why does it creates a new block , since NULL is a macro defined in stdio.h as 0 so it implies that it points to the base address 0 which in most machines is system area , so how can realloc start allocating a memory from base address 0 for 10 integers , why is this not a segmentation fault ?

user123
  • 271
  • 1
  • 10
  • Passing a NULL doesn't imply that the address 0 will be used. – 2501 Mar 11 '16 at 11:10
  • But it is a macro whose value is 0 , and first argument of realloc is a pointer so when we are passing 0 to a pointer variable , it implies that 0 is an address isn't it ? – user123 Mar 11 '16 at 11:12
  • 3
    You are making a lot of assumptions that aren't true. – 2501 Mar 11 '16 at 11:13
  • The [documentation](http://en.cppreference.com/w/c/memory/realloc) is pretty clear about this. – Jabberwocky Mar 11 '16 at 11:13
  • 1
    So is Google. Googling your exact title gives: 'About 78,500 results', with your question as the first entry, and its answer as the second. If you had not asked on SO first, your answer would have been at the top. – Martin James Mar 11 '16 at 11:16
  • Detail: `NULL` is _a_ null pointer constant. `NULL==0` is always true. `NULL` might _not_ have the same bit pattern as `0` or of `(void*)0`. It is common that `NULL`, and `(void*)0` both have the same bit pattern of all zero bits. "NULL is a macro defined in stdio.h as 0" is true on some platforms, but that is not defined by the C spec. Other possibilities have and can occur. – chux - Reinstate Monica Mar 11 '16 at 15:21

4 Answers4

3

so how can realloc start allocating a memory from base address 0

From realloc manual:

In case that ptr is a null pointer, the function behaves like malloc, assigning a new block of size bytes and returning a pointer to its beginning.

So in case the previous pointer is NULL, it doesn't mean that realloc has to start allocating from base address 0. In fact, it will behave like malloc, allocating a new block of memory.

artm
  • 17,291
  • 6
  • 38
  • 54
3

To clarify: it means the function realloc checks if its first argument is 0 (a NULL pointer) and if yes, behaves like malloc or simply calls malloc.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
2

NULL is a macro defined in stdio.h as 0 so it implies that it points to the base address 0

That's not true. NULL is the macro for the null pointer, which doesn't necessarily point to base address 0. See C FAQ.


To answer your main question, realloc behaves like malloc when the first argument is the null pointer.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    To be picky, NULL is the macro for _a_ null pointer _constant_. A pointer which gets assigned a null pointer constant, becomes a null pointer. So there is nothing called "_the_ null pointer". For more info, [see this](http://stackoverflow.com/a/32136460/584518). – Lundin Mar 11 '16 at 12:03
0

Yet another answer, All have explained beautifully but misses some good observation.

The following behaviour is taken from Linux man. You might like read doc for your platform. realloc(void *ptr, size_t size) has been to have a bad design, trying to perform many task, for instance

1) if ptr == NULL, it behaves like malloc(size)

2) if ptr != NULL && size == 0, it behaves like free(ptr)

More details at man 3 realloc.

So, it is preferable to have a wrapper for realloc especially for usage like ptr = realloc(ptr, size); which leads to leak.

void* my_realloc(void *oldPtr, size_t newSize)
{
    void *newPtr = NULL;
    if ( (newPtr = realloc( oldPtr, newSize) ) == NULL) 
    {
        free( oldPtr);
        return NULL;
    }
    return newPtr;
}
dlmeetei
  • 9,905
  • 3
  • 31
  • 38
  • `realloc(some_pointer, 0)` returns `NULL` or a non-`NULL` pointer. IMO, a weakness in `realloc()` specification. `my_realloc()` does the same - no improvement there. Better if `realloc(some_pointer, 0)` returned non-`NULL` so a return value of `NULL` _only_ occured with an error. – chux - Reinstate Monica Mar 11 '16 at 15:35
  • #2 is not true. `realloc(not_null_pointer, 0)` may return a valid (not-`NULL`) pointer. It is not like `free()`. – chux - Reinstate Monica Mar 11 '16 at 15:37
  • linux does not define C, The C standard defines C. "If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object." §7.22.2.1 Post is not tagged [linux]. – chux - Reinstate Monica Mar 11 '16 at 15:49