How does realloc() work?
Asked Answered
S

3

5

Assuming that I have allocated memory using malloc(), if I do in my code:

char *newline = realloc ( oldline , newsize );
// Assuming oldline is the pointer to which the starting address
// of the memory which malloc() has returned, is assigned and,
// say, newsize is integer having 100 value.

Now my questions regarding it are:-

  1. Is it necessary that newline will point to same address as oldline, which holds the previous initial address?
  2. Is it so that in this case, oldline will be freed(implicitly) and newline will take the charge of memory addressing?
  3. After the above code and after the work has been done, what should I do to free memory

    free(newline);
    

    or

    free(oldline);
    

    or both?

Solder answered 29/3, 2014 at 18:29 Comment(4)
On a unix clone, open a terminal window and type man realloc and all your questions will be answered. On other OS's you need to figure out where the documentation is.Lennox
Or have a look on the internet: en.cppreference.com/w/c/memory/reallocMelodimelodia
Required reading for any student of C: C99 with Technical corrigenda TC1, TC2, and TC3 includedBreed
@Breed these are of C99?Solder
C
9

It depend if realloc was successful or not. If realloc is successful then:

  1. No ! For example, if there is not enough contiguous memory after oldline then newline will be different from oldline.

  2. Yes

  3. free(newline); since oldline has been freed if necessary. After a realloc oldline should be considered as invalid pointer.

If it is not successful. Then, you use oldline as if nothing happened, in particular you should free it.

Conch answered 29/3, 2014 at 18:33 Comment(4)
"After a successful realloc...". If realloc returns NULL, you do have to free oldline.Egad
if realloc returns NULL and size is not 0 and oldline is not 0, you have to free oldline.Breed
@Deduplicator: The standard guarantees that free(NULL) is a no-op. Furthermore, if you ask for 0 bytes and realloc (or malloc for that matter) returns a non-NULL value, you do have to free it. So the only valid test is "if oldline is not 0", but you don't have to test that because, as above, if oldline is 0, free(oldline) is harmless.Egad
The question was must free, not can free. And i don't see where anything you said contradicts my comment.Breed
W
1

1) No.. in fact newline is not used at all (other than to store the results), why would you ask that?

2) Yes

3) Only the first.

Wader answered 29/3, 2014 at 18:32 Comment(1)
for 1) newline may or may not points to same as of oldline.Solder
B
1

C standard for realloc(old, size):

  • size == 0
    • realloc might free old and return 0
    • alternatively realloc behaves as for size != 0 but cannot return 0
  • size != 0
    • realloc might return 0. old is not touched
    • if the block pointed to by old is >= size, realloc might return old
    • alternatively realloc allocates a block >= size, copies all bytes from old up to size, frees old and returns this new block

You are responsible for whichever block persisted through this algorithm / was allocated. Working those out:

1 No. Though it can be.

2 Yes, If size == 0 or returned != 0

3a free oldline, if returned 0 and size != 0

3b free newline, if returned != 0

Breed answered 29/3, 2014 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.