Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, sorry I'm late to the party. This is my first stackoverflow answer. :)</p> <p>As has been pointed out, when realloc() is called, you can potentially change the pointer to the memory being reallocated. When this happens, the argument "string" becomes invalid. Even if you reassign it, the change goes out of scope once the function ends.</p> <p>To answer the OP, realloc() returns a pointer to the newly-reallocated memory. The return value needs to be stored somewhere. Generally, you would do this:</p> <pre><code>data *foo = malloc(SIZE * sizeof(data)); data *bar = realloc(foo, NEWSIZE * sizeof(data)); /* Test bar for safety before blowing away foo */ if (bar != NULL) { foo = bar; bar = NULL; } else { fprintf(stderr, "Crap. Memory error.\n"); free(foo); exit(-1); } </code></pre> <p>As TyBoer points out, you guys can't change the value of the pointer being passed in as the input to this function. You can assign whatever you want, but the change will go out of scope at the end of the function. In the following block, "input" may or may not be an invalid pointer once the function completes:</p> <pre><code>void foobar(char *input, int newlength) { /* Here, I ignore my own advice to save space. Check your return values! */ input = realloc(input, newlength * sizeof(char)); } </code></pre> <p>Mark tries to work around this by returning the new pointer as the output of the function. If you do that, the onus is on the caller to never again use the pointer he used for input. If it matches the return value, then you have two pointers to the same spot and only need to call free() on one of them. If they don't match, the input pointer now points to memory that may or may not be owned by the process. Dereferencing it could cause a segmentation fault.</p> <p>You could use a double pointer for the input, like this:</p> <pre><code>void foobar(char **input, int newlength) { *input = realloc(*input, newlength * sizeof(char)); } </code></pre> <p>If the caller has a duplicate of the input pointer somewhere, that duplicate still might be invalid now.</p> <p>I think the cleanest solution here is to avoid using realloc() when trying to modify the function caller's input. Just malloc() a new buffer, return that, and let the caller decide whether or not to free the old text. This has the added benefit of letting the caller keep the original string!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload