473,240 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

std::map and newed char *

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

Jul 3 '06 #1
3 2015
br*******@gmail.com wrote:
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?

Jul 3 '06 #2

<br*******@gmail.comwrote in message
news:11*********************@v61g2000cwv.googlegro ups.com...
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

Jul 3 '06 #3

br*******@gmail.com wrote:
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.
>
-- Brian Ray
Jul 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
14
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList =...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
9
by: Heck | last post by:
I'm using Visual Studio 2005, but i don't think that's (primarily) where I've gone wrong. I was experimenting, trying, by the way, to move into writing my own iterator so that I can learn,...
3
by: massysett | last post by:
I'm puzzled about part of the standard. 23.1 states that items stored in a container must be assignable. Therefore, the items in a map--that is, std::pair<const Key, valuemust be assignable....
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.