Realloc into a function

c, function, initialization, realloc

Solution

The way you have this written:

int *vet, num=10;

memoria (vet, num);

The changes you make inside of `memoria` do not get sent back to `main`. To understand this, just for fun change the value of `num` instead of `memoria` and then check it back in `main`. The value in main will still be `10`. The variables `vet` and `num` are passed by value so that they keep their original values in `main`.

The two most common ways around this are to pass the address of `vet` in so `memoria` can modify it, or return a new value for `vet`.

The first form looks like this:

memoria( & vet, num ) ;

void memoria (int **vet, int num)
{
  * vet= realloc( * vet, ... ) ;

Or, instead you can change the return type of `memoria`,

vet= memoria( vet, num ) ;

int * memoria( int * vet, int num)
{
  ...
  return vet ;
}

They both have their pros and cons. The second form is probably easier for people who aren't pointerheads to follow.

malloc/realloc fluke

The reason your last example works is easy to see if you add in `printf`s to `main` and `memoria` to show the value of `vet`. If it can, `realloc()` puts the new memory at the same location as the old pointer. In your simple test case, the allocator has an easy time doing so, and so the memory stays at the same location. If the code was more complex, and the call to `realloc()` moved the pointer, you'd be seeing a crash in `main` after.

Problem

My question is about `realloc`. The following code works correctly (with no warning): ``` #include <stdio.h> #include <stdlib.h> int main () { int num=10; int *vet; int i; for (i=0; i<num; i++) { /* allocate memory of vet to contains (i+1) int */ vet = (int*) realloc ( vet, (i+1) * sizeof(int) ); /* write numbers in the allocated memory */ vet[i] = 321 + i; } /* print test, if all works I must see: | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | */ printf ("| "); for (i=0; i<num; i++) printf ("%d | ", vet[i]); printf ("\n"); return 0; } ``` But the same program with a function doesn't work! And the compiler return the following warning: ``` In function ‘main’: 14:10: warning: ‘vet’ is used uninitialized in this function [-Wuninitialized] ``` The code is: ``` #include <stdio.h> #include <stdlib.h> void memoria (int *, int); int main () { int *vet, num=10; memoria (vet, num); /* print test, if all works I must see: | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | */ int i; printf ("| "); for (i=0; i<num; i++) printf ("%d | ", vet[i]); printf ("\n"); return 0; } void memoria (int *vet, int num) { int i; for (i=0; i<num; i++) { /* allocate memory of vet to contains (i+1) int */ vet = (int*) realloc ( vet, (i+1) * sizeof(int) ); /* write numbers in the allocated memory */ vet[i] = 321 + i; } } ``` Someone can say me why? Thank you very much! Oh, and the same code with a 'random' malloc in main works (with the function)... ``` #include <stdio.h> #include <stdlib.h> void memoria (int *, int); int main () { int *vet, num=10; /* ADDED MALLOC */ vet = (int*) malloc (1); memoria (vet, num); /* print test, if all works I must see: | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | */ int i; printf ("| "); for (i=0; i<num; i++) printf ("%d | ", vet[i]); printf ("\n"); return 0; } void memoria (int *vet, int num) { int i; for (i=0; i<num; i++) { /* allocate memory of vet to contains (i+1) int */ vet = (int*) realloc ( vet, (i+1) * sizeof(int) ); /* write numbers in the allocated memory */ vet[i] = 321 + i; } } ```

Original source