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;
}
}
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 printfs 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.
Take care of these few problems and you are good to go.
Your main doesn't get back avet which actually points to something. Return the vet back to your main vet somehow.
int *vet = NULL; //better bet for realloc() in this case
vet= memoria (vet, num);
Change the prototype accordingly to return a pointer and do remember to return vet in the definition.
int* memoria(int*, int);
Now to realloc(). Accoring to ISO C
If ptr is a
null pointer,realloc()shall be equivalent tomalloc()for the specified size.If ptr does not match a pointer returned earlier by calloc(),
malloc(), orrealloc()or if the space has previously been deallocated by a call tofree()orrealloc(), the behavior is undefined.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With