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 ?

6
  • Passing a NULL doesn't imply that the address 0 will be used. Commented Mar 11, 2016 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 ? Commented Mar 11, 2016 at 11:12
  • 3
    You are making a lot of assumptions that aren't true. Commented Mar 11, 2016 at 11:13
  • The documentation is pretty clear about this. Commented Mar 11, 2016 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. Commented Mar 11, 2016 at 11:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

1 Comment

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.
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;
}

3 Comments

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.
#2 is not true. realloc(not_null_pointer, 0) may return a valid (not-NULL) pointer. It is not like free().
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].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.