473,503 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use char* in map instead of using string

151 New Member
How to use char* instead of using string as key and value.

map <string, string> m;
m["Test"]= "Pass";
m["Result"]= "Fail";

When i try to use char* as key value there is memory leak.

map <const char*, const char*> m;
m["Test"]= "Pass";
m["Result"]= "Fail";

How to avoid memory leak? Thanks in advance.

Regards
Jun 25 '10 #1
6 2641
newb16
687 Contributor
First, you need to implement comparer for the char* as a key, or else two keys with the same string value at different addresses will be considered different. Then use shared_ptr to take care of keys and values.
Jun 25 '10 #2
Banfa
9,065 Recognized Expert Moderator Expert
I feel compelled to ask why would you want to make this change?
Jun 25 '10 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
You avoid the memory link by using a struct/class that has a destructor.

You cannot use the STL containers without well-behaved classes. That is, classes with proper constructors, destructors, copy constructors and assignment operators. You may also need an operator< or a suitable comparator function.
Jun 25 '10 #4
whodgson
542 Contributor
If 'char*'is a C string type (which it is)how could you do so and why would you want to ?
Jul 2 '10 #5
Banfa
9,065 Recognized Expert Moderator Expert
@weaknessforcats
I completely understand what you mean and whole heartedly agree with the sentiment but that isn't quite true. You can use the containers with the numeric PODs (int, long, float, enum etc). It is arrays (or pointers to arrays) that are troublesome.

For anyone thinking I have left out structs I basically lump them under "well-behaved classes" since a struct is a class.
Jul 2 '10 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
If 'char*'is a C string type (which it is)
A char* is not a string type.

Instead it is a pointer to a single char.

True, some C functions ASSUME a char* is a pointer to a C-style string but it is always an assumption.

In C++ the string type is string.
Jul 2 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
7987
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
7
10613
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
4
1593
by: trellow422 | last post by:
How do I convert the following variable to a variable of type String? char record = new char; I have tried the following: String temp = record.ToString(); temp =...
2
48779
by: Bill Todd | last post by:
What is the easiest way to convert a char array to string? -- Bill
9
2615
by: ad | last post by:
I have a string like "1,2,2,3,3,3,4" I want to trim off the duplicate part, and make it to "1,2,3,4" How can I do?
2
8155
by: Alper Akcayoz | last post by:
Hello Esteemed Developpers I would like to thank you in advance for your sincere responses I am a fresh Visual C++ .NET Developer. Can you kindly guide me for How to Convert char* to System::String
21
2278
by: Hitesh | last post by:
Hi, I get path strings from a DB like: \\serverName\C:\FolderName1\FolderName2\example.exe I am writing a script that can give me access to that exe file. But problem is that string is not...
6
7454
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
2
2167
by: wingleader | last post by:
Hi. Got such a problem. I wrote a url decoding programm. The main idea is, that programm take char by char from incoming string, decides what changes must be made, and then inserts them to...
3
1676
by: Skejan | last post by:
Hello, I wrote small function which translates text in my application: map<string,string> localization; 1 string: universal identifier of localized text 2 string: text to localize const...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
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
5596
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,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.