• C

How to free a dynamically allocated multidimensional array??

Hi I allocate the array2  in the following way.

 int **array2 = malloc(nrows * sizeof(int *));
                array2[0] = malloc(nrows * ncolumns * sizeof(int));
                for(i = 1; i < nrows; i++)
                        array2[i] = array2[0] + i * ncolumns;



How to free the allocated memory  "array2" at the end??
josabrahamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leisnerCommented:
free(array2[0]);
free(array2);

Looking at it quickly, it appears to work but I think its uncoventional...

When allocating a multidimensional array, just doing 1 malloc is fine (i.e. rows * columns * size).
Or allocating a vector of row points and allocating each row independently...

The allocates a row vector and fills in each row one at a time into an array....maybe there's a reason
you want to do it this way....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulCaswellCommented:
Hi leisner,

Take a look at a discussion about this at:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21787694.html

It is an effective method that has its advantages.

Paul
0
Prashant SabnekarAVPCommented:
int** i;

//Initialization
i=new int*[10]; //u can use malloc also
for(int index=0;index<10;index++)
i[index]=new int[10];

//Deletion
for(int index=0;index<10;index++)
delete [] i[index];

Regards
Prashant Sabnekar
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

abaloniaCommented:
for (int i = 0; i < nrows; i++) {
  free(array2[i]);
}
free(array2);
0
Ashwini_pandeyCommented:
To free the allocated  memory you should use
free(array2[0]);
free(array2);

But a two dimensional can be declared as in a concise form
int (* arr)[NCOL] = (int (*)[NCOL])malloc(NROWS*NCOL*sizeof(int));

this can be delete as free(arr);
0
PaulCaswellCommented:
>>int (* arr)[NCOL] = (int (*)[NCOL])malloc(NROWS*NCOL*sizeof(int));
Nice! I've never seen that technique before!

Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.