typedefstruct{
int *nodeIndex;
int *linkIndex;
int c_start;
int c_end;
} typeDMA;
// i use the following code snippet to new it and delete it:
typeDMA *gtDMA;
gtDMA = new typeDMA[6]; // new the structure itself
for(int i=0; i<6; i++)
{
gtDMA[i].nodeIndex = newint[100]; // new node index
gtDMA[i].linkIndex = newint[100]; // new link index
}
// use the sturcture to do sth
// release the sturecture
for(int i=0; i<6; i++)
{
delete []gtDMA[i].nodeIndex;
delete []gtDMA[i].linkIndex;
}
delete []gtDMA;
My question is: did i use the sturecture correctly? will it cause any memory leak? How can i know whether memory is leaking?
You did it correctly. But if you are thinking, that keeping track of this stuff is cumbersome, you are not alone. Many people thought so and that's why they invented e.g. the Standard Template Library.
typedefstruct{
vector<int> nodeIndex;
vector<int> linkIndex;
int c_start;
int c_end;
} typeDMA;
// i use the following code snippet to new it and delete it:
vector<typeDMA> gtDMA(6);
for(int i=0; i<6; i++)
{
gtDMA[i].nodeIndex = vector<int>(100); // new node index
gtDMA[i].linkIndex = vector<int>(100); // new link index
}
// use the sturcture to do sth
// no release necessary. Everything is destructed automatically
// if you need to manually reset things, do this:
gtDMA.clear(); // reset the vector to 0 elements and freeing all other
For your second question: There are a couple of non-portable tools for debugging memory problems, but all are quite cumbersome to use too.