std::map and newed char *

B

brianhray

Hello,

Say I have a map:

typedef map< MyString, const char* > MyMap;

The char* are being new'd. Do these needed manually deleted or are they
deleted in the destructor? I know if the map was using std:string they
would be freed.

If so, in *my* destructor where mMyMap is a memeber, I am trying:

for ( MyMap::iterator iter = m_mymap.begin(); iter != m_mymap.end();
iter++ )
delete [] (*iter).second;

Although, how do I know these are getting freed? Is there some sort of
test I can add. I tried to assing the (*iter).second to the const char*
but in the debugger this makes it look like these did not get deleted.

-- Brian Ray
 
R

Rolf Magnus

Hello,

Say I have a map:

typedef map< MyString, const char* > MyMap;

The char* are being new'd. Do these needed manually deleted or are they
deleted in the destructor?

Why should they be? The map doesn't even know where the pointer comes from.
I know if the map was using std:string they would be freed.

In this case, the string's destructor is called, which does whatever is
needed to release the string's resources. However, pointers don't have
destructors, so nothing is done when they are destroyed.
If so, in *my* destructor where mMyMap is a memeber, I am trying:

for ( MyMap::iterator iter = m_mymap.begin(); iter != m_mymap.end();
iter++ )
delete [] (*iter).second;

Although, how do I know these are getting freed?

What do you mean?
I tried to assing the (*iter).second to the const char*
but in the debugger this makes it look like these did not get deleted.

Again, what do you mean? Did the delete[] operator not get called?
 
H

Howard

Hello,

Say I have a map:

typedef map< MyString, const char* > MyMap;

The char* are being new'd. Do these needed manually deleted or are they
deleted in the destructor?

In general, you need to delete anything you new. (We'll leave "smart
pointers" out of the picture for now, since you're not asking about them.)
I know if the map was using std:string they
would be freed.

That's because a std::string is not a pointer. It has a destructor which
will get called when the container is destroyed. Pointers don't have
destructors, so nothing will get done for them.
If so, in *my* destructor where mMyMap is a memeber, I am trying:

for ( MyMap::iterator iter = m_mymap.begin(); iter != m_mymap.end();
iter++ )
delete [] (*iter).second;

Although, how do I know these are getting freed? Is there some sort of
test I can add. I tried to assing the (*iter).second to the const char*
but in the debugger this makes it look like these did not get deleted.

If you call delete[], then you're deleting. The compiler will do what it is
supposed to do. Is there some problem you're anticipating with the code?

-Howard
 
Y

Yong Hu

Hello,

Say I have a map:

typedef map< MyString, const char* > MyMap;

The char* are being new'd. Do these needed manually deleted or are they
deleted in the destructor? I know if the map was using std:string they
would be freed.

If so, in *my* destructor where mMyMap is a memeber, I am trying:

for ( MyMap::iterator iter = m_mymap.begin(); iter != m_mymap.end();
iter++ )
delete [] (*iter).second;

Although, how do I know these are getting freed? Is there some sort of
test I can add. I tried to assing the (*iter).second to the const char*
but in the debugger this makes it look like these did not get deleted.

If they are not deleted, then the for loop was not executed. You can
check if there is any pre-mature return.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top