Free Allocated Memory Error (delete)

A

alan

Dear all,

I have a problem on the caption subject.
I read many referece books, it remind me that if "new" is used, then
"delete" should be use to release memory.

Here is my testing problem.

char *func()
{
char *str = new char[10];
strcpy(str, "testing");
return str;
}

void main()
{
char *str = func();
cout<<str<<endl;
delete str; // <--------Error is prompted in here
}

When I used "delete" keyword, error message is prompted out.
If I do not use "delete" keyword, it works fine.
But I have a query, the allocated memory cannot be released. If I have
100 or 1000 or more allocated memory (same as this situtation), my
program memory demand will be very large?? is it true?
How can I solve it ? or How can I release it ? and How can I impove
the program?

Please help. I am a newbie in C++.

Thank you very much.
Alan
 
D

Dan Cernat

alan said:
Dear all,

I have a problem on the caption subject.
I read many referece books, it remind me that if "new" is used, then
"delete" should be use to release memory.

Here is my testing problem.

char *func()
{
char *str = new char[10];
strcpy(str, "testing");
return str;
}

void main()
{
char *str = func();
cout<<str<<endl;
delete str; // <--------Error is prompted in here

delete[] str;
I would say at a first glance
}

When I used "delete" keyword, error message is prompted out.
If I do not use "delete" keyword, it works fine.
But I have a query, the allocated memory cannot be released. If I have
100 or 1000 or more allocated memory (same as this situtation), my
program memory demand will be very large?? is it true?
How can I solve it ? or How can I release it ? and How can I impove
the program?

Please help. I am a newbie in C++.

Thank you very much.
Alan

dan
 
A

Artie Gold

alan said:
Dear all,

I have a problem on the caption subject.
I read many referece books, it remind me that if "new" is used, then
"delete" should be use to release memory.

Here is my testing problem.

char *func()
{
char *str = new char[10];
strcpy(str, "testing");
return str;
}

void main() int main()
{
char *str = func();
cout<<str<<endl;
delete str; // <--------Error is prompted in here
}

When I used "delete" keyword, error message is prompted out.
If I do not use "delete" keyword, it works fine.
But I have a query, the allocated memory cannot be released. If I have
100 or 1000 or more allocated memory (same as this situtation), my
program memory demand will be very large?? is it true?
How can I solve it ? or How can I release it ? and How can I impove
the program?

Please help. I am a newbie in C++.
Since the memory was allocated as an aggregate (i.e. using the []
syntax), you need:

delete [] str;

HTH,
--ag
 
S

Sharad Kala

alan said:
Dear all,

I have a problem on the caption subject.
I read many referece books, it remind me that if "new" is used, then
"delete" should be use to release memory.

Here is my testing problem.

char *func()
{
char *str = new char[10];
strcpy(str, "testing");
return str;
}

void main()
{
char *str = func();
cout<<str<<endl;
delete str; // <--------Error is prompted in here
}

When I used "delete" keyword, error message is prompted out.
If I do not use "delete" keyword, it works fine.
But I have a query, the allocated memory cannot be released. If I have
100 or 1000 or more allocated memory (same as this situtation), my
program memory demand will be very large?? is it true?
How can I solve it ? or How can I release it ? and How can I impove
the program?

Please help. I am a newbie in C++.

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top