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?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
OldSchool
  • 2,123
  • 4
  • 23
  • 45
  • 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. – user3386109 Mar 29 '14 at 18:33
  • 1
    Or have a look on the internet: http://en.cppreference.com/w/c/memory/realloc – Excelcius Mar 29 '14 at 18:34
  • Required reading for any student of C: [C99 with Technical corrigenda TC1, TC2, and TC3 included](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf) – Deduplicator Mar 29 '14 at 18:57
  • @Deduplicator these are of C99? – OldSchool Mar 29 '14 at 18:58

3 Answers3

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.

hivert
  • 10,579
  • 3
  • 31
  • 56
  • "After a *successful* realloc...". If realloc returns NULL, you do have to free oldline. – rici Mar 29 '14 at 18:35
  • if realloc returns NULL _and size is not 0 and oldline is not 0_, you have to free oldline. – Deduplicator Mar 29 '14 at 18:40
  • @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. – rici Mar 29 '14 at 19:01
  • The question was _must free_, not _can free_. And i don't see where anything you said contradicts my comment. – Deduplicator Mar 29 '14 at 19:05
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.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
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

Deduplicator
  • 44,692
  • 7
  • 66
  • 118