C++ Release memory problem

A

alan

Hi all,

I have a problem on release memory.

If I delcare the following pointer to pointer char, how can I free the
allocated memory ?

let say "myVar = 20" <---actually it is not a constant, but now I set
it to 20.

char **tempStr=(char**)new char[sizeof(char*)*myVar];
...
....do something to set value to each char pointer
...

I try to delete it by the following statement.

for (int i=0; i< myVar; i++)
delete tempStr;

BUT it is violet my program.

Can anybody tell me how to release memory in such case.

Thank you very much.
Alan
 
J

John Carson

alan said:
Hi all,

I have a problem on release memory.

If I delcare the following pointer to pointer char, how can I free the
allocated memory ?

let say "myVar = 20" <---actually it is not a constant, but now I set
it to 20.

char **tempStr=(char**)new char[sizeof(char*)*myVar];
..
...do something to set value to each char pointer

You need to show how you do this.
I try to delete it by the following statement.

for (int i=0; i< myVar; i++)
delete tempStr;

BUT it is violet my program.

Can anybody tell me how to release memory in such case.


Do you allocate memory with new? You only delete memory allocated with new.
I suggest you show a complete compileable example illustrating the problem.
 
I

Ioannis Vranos

alan said:
Hi all,

I have a problem on release memory.

If I delcare the following pointer to pointer char, how can I free the
allocated memory ?

let say "myVar = 20" <---actually it is not a constant, but now I set
it to 20.

char **tempStr=(char**)new char[sizeof(char*)*myVar];
..
...do something to set value to each char pointer



I feel like what you are doing is not what you want to do. If you want to
create an array of 20 char pointers on the free store you have to do:


char **tempstr=new char *[20];






Ioannis Vranos
 
S

Sharad Kala

[snip]
char **tempStr=(char**)new char[sizeof(char*)*myVar];
..
...do something to set value to each char pointer

Allocate like this -
char **tempStr=new char *[20];
I try to delete it by the following statement.

for (int i=0; i< myVar; i++)
delete tempStr;


Delete like this -
delete [] tempStr;

-Sharad
 
J

John Harrison

alan said:
Hi all,

I have a problem on release memory.

Problems releasing memory are usually caused by a bug earlier in the
program, not by releasing the memory in the wrong way.

john
 
C

Christopher Benson-Manica

alan said:
char **tempStr=(char**)new char[sizeof(char*)*myVar];

As others stated, this is wrong. It looks like you thought you were
using malloc:

char **tmpStr=(char**)malloc( sizeof(char*)*myVar );

Prefer the correct usage of new to the above, however.
..
...do something to set value to each char pointer
..

This "something" is actually important. For example, if the something
involves strdup() (just a wild guess)...
delete tempStr;


....this is also wrong. Delete things you've allocated with new;
free() things you've allocated with malloc() (such as strdup()'ed
strings). malloc/delete and new/free combinations should NEVER be
used.
Can anybody tell me how to release memory in such case.

If you post code that demonstrates the problem, I'm sure we can help.
On the other hand, why not just use std::vector<std::string>? Then
you don't have to worry about releasing memory at all.
 

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
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top