Why does a pointer have to be assigned for realloc to work without altering the first value in the memory block?

后端 未结 2 1219
野趣味
野趣味 2021-01-26 22:43
int *ptr;
...
realloc(ptr,++count*sizeof(int));
      or
ptr=realloc(ptr,++count*sizeof(int));

I noticed if I use option number one more than once, the

相关标签:
2条回答
  • 2021-01-26 23:17

    The actual implementation of realloc is implementation-defined, but typically realloc tries to get more memory in two steps. First, it tries to see whether it can expand the allocated block without having to relocate it in memory. If that succeeds, then realloc doesn't have to move anything in memory and the old pointer is valid.

    However, there might not be space to expand the allocated block to the new size. For example, you could imagine that there's another block of allocated memory right after the currently-allocated block, so it would be impossible to expand the boundary forward. In that case, realloc has to allocate a brand-new piece of memory to store the larger block, and the old pointer will no longer be valid. This is why you should write

    ptr = realloc(ptr, newSize);
    

    instead of

    realloc(ptr, newSize);
    

    Of course, there are other reasons why realloc might move memory around. The memory allocator might have specific size requirements for blocks in different parts of memory, or it might try to optimize performance by compacting memory as it goes. In either case, realloc might move memory around without notice, so you may need to change where ptr points.

    Hope this helps!

    0 讨论(0)
  • 2021-01-26 23:32

    The pointer returned by realloc may or may not point to the same address as the pointer you passed to it.

    Typically if you're expanding a memory block, there may not be enough room to expand it in place. In that case realloc allocates a new chunk of memory, copies the contents of the old object to the newly allocated chunk, and returns a pointer to the new chunk. If you don't assign the result to a pointer object, you've just lost it (the old chunk is deallocated, and you don't know where the new one is).

    realloc can even return a pointer to a newly allocated block of memory if it shrinks the allocated block. There might be good reasons to allocate smaller chunks from a different region of memory.

    You also need to be aware that realloc can fail. If it does, it returns a null pointer -- and the old pointer is still valid (just not pointing to a block of memory of the size you wanted). (In principle, realloc can fail even if the new size is smaller than the old one.) So unless your response to an allocation failure is to abort the program, or at least the part of it that uses the pointer, you should assign the result to a different pointer object:

    int *ptr = ...;
    int *new_ptr;
    new_ptr = realloc(ptr, new_size);
    if (new_ptr == NULL) {
        /* ptr still points to the old block of memory; you can try something else */
    }
    else {
        ptr = new_ptr; /* Now you can forget the old pointer value */
    }
    

    If realloc succeeds, the pointer value it returns is valid, and the pointer you passed to it is invalid.

    If realloc fails, it returns a null pointer, and the pointer you passed to it is still valid.

    0 讨论(0)
提交回复
热议问题