473,396 Members | 2,093 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,396 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 2024
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.