?
Solved

How to free a dynamically allocated multidimensional array??

Posted on 2006-03-24
8
Medium Priority
?
311 Views
Last Modified: 2010-04-15
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??
0
Comment
Question by:josabraham
6 Comments
 
LVL 3

Accepted Solution

by:
leisner earned 200 total points
ID: 16287066
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16288159
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
 
LVL 6

Expert Comment

by:Prashant Sabnekar
ID: 16355024
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
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
LVL 1

Expert Comment

by:abalonia
ID: 16494120
for (int i = 0; i < nrows; i++) {
  free(array2[i]);
}
free(array2);
0
 
LVL 1

Expert Comment

by:Ashwini_pandey
ID: 16550981
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16555676
>>int (* arr)[NCOL] = (int (*)[NCOL])malloc(NROWS*NCOL*sizeof(int));
Nice! I've never seen that technique before!

Paul
0

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question