Is it a proper free memory code?

H

huohaodian

Hi,

I have a variable char ** test;

initialized as
test = new char*[6];
for (int i=0;i<6;i++)
{
test = new char[MAX_PATH];
}

and try to free memory as

for (int j=0;i<6;j++)
{
delete [] test[j]; // free space
test = NULL; // remove pointer
}
delete []test;


Thank in advance.
 
R

red floyd

Hi,

I have a variable char ** test;

initialized as
test = new char*[6];
for (int i=0;i<6;i++)
{
test = new char[MAX_PATH];
}

and try to free memory as

for (int j=0;i<6;j++)
{
delete [] test[j]; // free space
test = NULL; // remove pointer
}
delete []test;


As far as I can see, yes, the code should function as you think.

However... You should prefer to use a std::vector of std:;string.

That way you don't even have to deal with memory management, or worry
about buffer overrun.
 
D

Default User

Hi,

I have a variable char ** test;

initialized as
test = new char*[6];
for (int i=0;i<6;i++)
{
test = new char[MAX_PATH];
}

and try to free memory as

for (int j=0;i<6;j++)
{
delete [] test[j]; // free space
test = NULL; // remove pointer


The line above doesn't look right. I suspect you wanted j rather than
i.




Brian
 
M

Micah Cowan

Default User said:
Hi,

I have a variable char ** test;

initialized as
test = new char*[6];
for (int i=0;i<6;i++)
{
test = new char[MAX_PATH];
}

and try to free memory as

for (int j=0;i<6;j++)
{
delete [] test[j]; // free space
test = NULL; // remove pointer


The line above doesn't look right. I suspect you wanted j rather than
i.


Also in the middle clause of the "for" line.
 
R

red floyd

red said:
Hi,

I have a variable char ** test;

initialized as
test = new char*[6];
for (int i=0;i<6;i++)
{
test = new char[MAX_PATH];
}

and try to free memory as

for (int j=0;i<6;j++)
{
delete [] test[j]; // free space
test = NULL; // remove pointer
}
delete []test;


As far as I can see, yes, the code should function as you think.

However... You should prefer to use a std::vector of std:;string.

That way you don't even have to deal with memory management, or worry
about buffer overrun.


Modulo typos, of course (test[j], not test)
 
D

Default User

Micah said:
Default User said:
Hi,

I have a variable char ** test;

initialized as
test = new char*[6];
for (int i=0;i<6;i++)
{
test = new char[MAX_PATH];
}

and try to free memory as

for (int j=0;i<6;j++)
{
delete [] test[j]; // free space
test = NULL; // remove pointer


The line above doesn't look right. I suspect you wanted j rather
than i.


Also in the middle clause of the "for" line.


Yep. Cut and paste, with a var name switch. Unless he skipped some
code, i shouldn't even be declared at that point. I suspect he's using
VC6.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top