By Santiago

2019-12-02 22:13:34 8 Comments

First of all I want to apologize if this is a repeated question. But I could not find any answer explaining how to do this correctly with a variable sized array.

Going straight to the code, I've got this:

void f(int[][2]);

int main(void)
  int (*obsF)[2] = malloc(sizeof(int)); //I reserve some memory so I can pass 
  f(obsF);                              //the array to f, since I don't know
  free(obsF);                           //its size until later working with f. 

void f(int obsF[][2])
  obsF = realloc(obsF, sizeof(int[5][2])); //The 5 is just as an example.
  // do stuff with obsF                    //I won't know this number until
}                                          //later in f.

Testing this with valgrind threw that that free(obsF); is an invalid free().

Now, if I do the same but everything in the main function like this:

int main(void)
  int (*obsF)[2] = malloc(sizeof(int));
  obsF = realloc(obsF, sizeof(int[5][2]));

Valgrind tests passed successfully with no errors.

I would like to know why this is happening and how to realloc my 2D array inside function and be able to free it from main correctly.

Any other better approach or solution is well received, as long as I can use obsF as a 2D array inside f and modify it as I wish and then use it in main.



@user114332 2019-12-02 22:21:56

C passes arguments by value, not by reference. When you pass the obsF pointer to f and reallocate it, you change the value of the obsF parameter in the f function, but the variable in the main function (which determined the parameter's initial value) still holds the old pointer, which has been invalidated by the reallocation.

To fix this, you can pass a pointer to the obsF variable to f, and dereference that pointer to access the variable:

void f(int **obsF)
  *obsF = realloc(*obsF, sizeof(int[5][2]));
  // do stuff with obsF

And then call f as:

int main(void)
  int *obsF = malloc(sizeof(int));

This way, the value of the obsF variable inside main will be updated after the call to f and the correct pointer will be freed.

@Santiago 2019-12-02 22:31:03

How would be the correct way to write the prototype of f? Because I cannot get it to work.

@user114332 2019-12-02 22:35:14

I changed the example to use a plain int * instead of int (*)[][]; it should compile now.

@Santiago 2019-12-02 22:51:00

I did not see that. Thank you very much. I can then cast the array to a 2D array, and free the old one, because it's what my program needs to handle. You helped me to solve my problem.-

Related Questions

Sponsored Content

18 Answered Questions

[SOLVED] With arrays, why is it the case that a[5] == 5[a]?

11 Answered Questions

[SOLVED] How to Sort Multi-dimensional Array by Value?

2 Answered Questions

2 Answered Questions

[SOLVED] invalid realloc/realloc returns NULL

  • 2013-11-16 01:09:46
  • Vladimir
  • 2706 View
  • 2 Score
  • 2 Answer
  • Tags:   c structure realloc

2 Answered Questions

[SOLVED] Using realloc to double an arrays size during runtime. Is my code correct?

  • 2017-05-28 05:25:29
  • Ivan Lendl
  • 727 View
  • 0 Score
  • 2 Answer
  • Tags:   c realloc

1 Answered Questions

[SOLVED] How to use realloc (strange behavior)

  • 2016-10-12 05:16:18
  • Aleksei Udalov
  • 46 View
  • 0 Score
  • 1 Answer
  • Tags:   c realloc

2 Answered Questions

[SOLVED] Initially mallocate 0 elements to later reallocate and measure size

2 Answered Questions

[SOLVED] Correct way to calculate the size for malloc() and realloc()?

  • 2014-04-14 05:15:53
  • jc72
  • 272 View
  • 3 Score
  • 2 Answer
  • Tags:   c malloc realloc

3 Answered Questions

[SOLVED] Reallocing an array of strings

Sponsored Content