Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use realloc in a function in C

Tags:

arrays

c

malloc

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.

like image 567
Legendre Avatar asked Dec 06 '12 16:12

Legendre


People also ask

Can you realloc in a function?

The realloc() function gives a handy way to grow or shrink an array: If oldp is the result of an earlier call to malloc() such as old = malloc(oldsize); then newp = realloc(oldp, newsize); Allocates newsize bytes of memory, Copies the contents of *oldp to it (up to the lesser of oldsize and newsize)

How do you use realloc in C?

Use of realloc() in C The function realloc is used to resize the memory block which is allocated by malloc or calloc before. Here, pointer − The pointer which is pointing the previously allocated memory block by malloc or calloc. size − The new size of memory block.

What does realloc () function do?

In the C Programming Language, the realloc function is used to resize a block of memory that was previously allocated. The realloc function allocates a block of memory (which be can make it larger or smaller in size than the original) and copies the contents of the old block to the new block of memory, if necessary.

How do I allocate memory with realloc?

Use of realloc() Size of dynamically allocated memory can be changed by using realloc(). As per the C99 standard: void * realloc ( void *ptr, size_t size); realloc deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size.


2 Answers

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); 
like image 199
simonc Avatar answered Sep 22 '22 11:09

simonc


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); 
like image 20
Ramy Al Zuhouri Avatar answered Sep 21 '22 11:09

Ramy Al Zuhouri