Home » Interview Questions » Interviews » Story Details
Printable Version

A Question A Day: Freeing memory allocated to n-dim array using free

by Kamal Rawat on Sep 05, 2011

How to free a memory on heap allocated using malloc to an N-Dim array
Comments: 0    Views: 1945

MCN Professionals | Interview Question of the day

 

Date: 5th Sept; 2011

Difficulty Level: ***  

Category: Programming

To receive one interview question in your mailbox daily, please subscribe to "A Question A Day" at MCN Professionals home page.

 

 

Today's Question:

Yesterday we learned about how to allocate memory on heap for an n-dim array using Malloc.

Today let us see how to deallocate the memory on heap using free. It is Simple if we are deallocating normal pointers or pointers pointing to a one-dimensional array.

How will deallocate memory of an n-dim array on heap?

 

Solution:

Remember the way constructors & destructor are called in C++? The process of deallocating a memory is reverse of the way we allocate it.

For a one-dim array it is simply a call to function free and then assigning NULL to the pointer.


free(p);

p = NULL;


The assignment to NULL is very important. It may otherwise make p a dangling pointer (a pointer which holds the address of memory location that is freed to the compiler). You should also note that some other pointer should not become dangling in this process. For example consider the below case:


int *p = (int*)malloc(sizeof(int));

int *q = p;


If we free the p pointer like

free(p);

p = NULL;

Though we deallocate p properly, we make q dangling in the process.. Description: )

To deallocate a 2-dim array, you have to deallocate each element of  one dimension array (the array pointing to the arrays of ints) before deallocating the pointer pointing to the 2-dim array. Else you will loose the address of smaller arrays.


for(int i=0; i<m; i++)

free(p[i]);

free(p)

p = NULL;


Note that we didn't assign NULL to individual p[i] elements. This is because they will be deallocated in the very next statement. You may do that if you want to on the cost of one necessary operation.


Lets take the last example of deallocating a 3-dim array:


for(int i =0; i<A; i++)

for(int j=0; j<B; j++)

free(p[i][j]);

 

for(int i=0; i<A; i++)

free(p[i]);

 

free(p)

p = NULL;

 

Interview Questions Archive:

                  To see all the questions in the category, click here...

------------------------------------------------------------------------------------------------------------


Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName