Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management during function call

I was writing a code that involves handling a 2D array of dimensions [101]X[101] in C. However I am constrained in terms of memory being used at a given point of time.

void manipulate(int grid_recv[101][101])
{
     //Something
}

void main()
{
   int grid[101][101];
   manipulate(grid);
}

So lets say I create an array grid[101][101] in my main() and then pass it for manipulation to another function. Now does the function manipulate() copy the entire matrix grid into grid_recv i.e by this sort of passing am I using twice the amount of memory ( i.e twice the size of grid)?

like image 666
Dubby Avatar asked Apr 13 '14 12:04

Dubby


1 Answers

No. In C, arrays cannot be passed as parameters to functions.

What you actually do is creating a pointer pointing the array. So the extra memory you use it only the size of that pointer created.

like image 82
chrk Avatar answered Oct 02 '22 16:10

chrk