Click here to Skip to main content
15,885,908 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello
I am using a structure like
C++
struct abc
{
	int		x;		
	int		y;
};

struct abcArray
{
	int iCount;
	abc **test;
};

Further Use this like(memory allocation):
C++
abcArray xyz;
xyz.test = new abc*;
for(int i = 0 ; i < 10 ; i++)
   xyz.test[i] = new xyz;


For deallocation:
C++
for(int i = 0 ; i < 10 ; i++)
{
   delete[] xyz.test[i];
   xyz.test[i] = NULL;
}


When I check for memory leaks,it says leak at
C++
xyz.test = new abc*;


Can anybody suggest me how I should deallocate to avoid this leak?
Posted
Updated 30-Jun-11 22:57pm
v2
Comments
Richard MacCutchan 2-Jul-11 4:11am    
From AndreFratelli:
Since you are already using structs, you could also go old school and implement a linked list yourself. STL is probably a better option, though.

abcArray xyz;
xyz.test = new abc*;
for(int i = 0 ; i < 10 ; i++)
   xyz.test[i] = new xyz;

You have allocated a pointer to a single abc structure and then proceeded to access the following 9 cells which do not belong to you. You should allocate like:
xyz.test = new abc*[10];
 
Share this answer
 
Comments
NipunG 1-Jul-11 5:25am    
But I am not sure how many cells need to be allocated...It is to be allocated as and when required.I used 10 just for reference.Randomly I have to allocate cell one by one.
If I do new abc*[10]; way, the I always need to create a temporary array to add new set of data.
Richard MacCutchan 1-Jul-11 5:48am    
If you don't know how many cells to allocate then you cannot do it with an array, as C++ arrays are fixed. Take a look at the STL collection classes and use one that allows this feature; the vector class would probably do it.
NipunG 1-Jul-11 10:03am    
I found out the reason for memory leak and that is not related to this piece of code which I have posted.But I want to understand what is the problem with this piece of code.
Richard MacCutchan 1-Jul-11 12:10pm    
I have explained what is wrong with your code; you allocate a single pointer and then use it as if you had allocated a 10 element array of pointers. You must either allocate the number you need at the start, or use a STL class that can grow dynamically.
AndreFratelli 2-Jul-11 1:45am    
Since you are already using structs, you could also go old school and implement a linked list yourself. STL is probably a better option, though.

Moved this comment so OP gets it.
What Richard said is correct... but there's a variety of solutions available:
CArray (MFC)[^]
vector (STL)[^]
Linked list (there's several implementations out there)[^]
 
Share this answer
 
First, you allocate xyz.test[i] using new, but then you deallocate it using delete[]. The two don't go together! Use delete on singular allocated objects and delete[] only on allocated arrays!

Also, you allocate xyz.test, but fail to deallocate it - this is causing the leak.

You said:
But I am not sure how many cells need to be allocated...It is to be allocated as and when required.I used 10 just for reference.Randomly I have to allocate cell one by one.

This means you can not store your structs in a simple C array. Instead you need a linked list. You should follow the last link posted by Albert Holguin if you don't want to implement your own.

If you are programming in C only (no C++ constructs, no templates), then check out this article about an Implementation of a single linked list in C. It has some very good explanations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900