38

Building on what I learned here: Manipulating dynamic array through functions in C.

void test(int data[])
{
    data[0] = 1;    
}    

int main(void)
{    
    int *data = malloc(4 * sizeof *data);

    test(data);

    return 0;
}

This works fine. However, I am also trying to using realloc in a function.

void increase(int data[])
{
    data = realloc(data, 5 * sizeof *data);    
}

This complies but the program crashes when run.

Question

How should I be using realloc in a function?

I understand that I should assign the result of realloc to a variable and check if it is NULL first. This is just a simplified example.

Community
  • 1
  • 1
Legendre
  • 3,108
  • 7
  • 31
  • 46
  • 17
    I can't believe you didn't cast the return value of `malloc()` and you also correctly used the `sizeof()` operator... Awesome! +1. –  Dec 06 '12 at 16:49
  • Welcome. Believe or not, every time I encounter a question tagged `c`, I am (and must be) prepared for clutter like `char *p = (char *)malloc(len * sizeof(char);`, which is wrong at 3 places at least (exercise: figure out where). –  Dec 06 '12 at 17:01
  • 8
    @H2CO3 - At the risk of sounding like a fool: 1. the cast before malloc is not necessary, 2. sizeof *p is equivalent to sizeof (char) and more useful when changing types, 3. its missing a closing bracket ")"? – Legendre Dec 07 '12 at 14:38
  • 4
    Oh so it's 4! Yes, I missed the close bracket indeed (0th error). First one: tick, second one: and since `sizeof(char)` is always 1, it's just clutter and decreases readability, third one: if you use `sizeof()`, you should really use `sizeof(*variable)` instead of `sizeof(type)`, because the latter **will** break when you change the type of `*p`. –  Dec 07 '12 at 14:41
  • Ah, I'm relatively new to programming and didn't realize that it is standard for sizeof(char) to be 1. Learned something today. Thanks for the interesting exercise! +1 – Legendre Dec 07 '12 at 14:44
  • @lundin I am opening this question because this also has `realloc`... – Antti Haapala -- Слава Україні Nov 01 '19 at 18:06

3 Answers3

38

You want to modify the value of an int* (your array) so need to pass a pointer to it into your increase function:

void increase(int** data)
{
    *data = realloc(*data, 5 * sizeof int);
}

Calling code would then look like:

int *data = malloc(4 * sizeof *data);
/* do stuff with data */
increase(&data);
/* more stuff */
free(data);
simonc
  • 41,632
  • 12
  • 85
  • 103
  • 3
    Beware memory leak! As mentioned in here https://stackoverflow.com/a/44071325/1201614 you need to use a temp pointer to check realloc return value – luca Mar 03 '20 at 10:52
8

Keep in mind the difference between a pointer and an array.
An array is a chuck of memory in the stack, and that's all.If you have an array:

int arr[100];

Then arr is an address of memory, but also &arr is an adress of memory, and that address of memory is constant, not stored in any location.So you cannot say arr=NULL, since arr is not a variable that points to something.It's just a symbolic address: the address of where the array starts.Instead a pointer has it's own memory and can point to memory addresses.

It's enough that you change int[] to int*.
Also, variables are passed by copy so you need to pass an int** to the function.

About how using realloc, all the didactic examples include this:

  1. Use realloc;
  2. Check if it's NULL.In this case use perror and exit the program;
  3. If it's not NULL use the memory allocated;
  4. Free the memory when you don't need it anymore.

So that would be a nice example:

int* chuck= (int*) realloc (NULL, 10*sizeof(int)); // Acts like malloc,
              // casting is optional but I'd suggest it for readability
assert(chuck);
for(unsigned int i=0; i<10; i++)
{
    chunk[i]=i*10;
    printf("%d",chunk[i]);
}
free(chunk);
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
6

Both code are very problematic, if you use the same pointer to send and receive from realloc, if it fails, you will lose your pointer to free it later.

you should do some thing like this :

{ ... ...

more = realloc(area , size);
if( more == NULL )
    free(area);
else
    area=more;

... ...

}

user8036942
  • 61
  • 1
  • 1
  • 4
    you `free(area)` on failure. Instead of freeing it, can I keep using `area` if realloc fails? – Alex Jul 02 '20 at 07:52