473,657 Members | 2,474 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 2649
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
7996
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., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
7
10622
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 namespace std; : : vector<string>* getTyphoon()
4
1599
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 = System.Convert.ToString(record); The result I get with both is "System.Char"
2
48788
by: Bill Todd | last post by:
What is the easiest way to convert a char array to string? -- Bill
9
2622
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
8178
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
2301
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 universal path, I need to add C$. Any idea how I can add $ char in that string. ServerName is not fixed length. It could be any chars length.
6
7461
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 methods in CLI return System::String^. I originally just gcnew'd System::String^ passing in the c_str(). But I can't really have as many gcnew's as I'm using for overhead and for fear of leaks. So my question is this, how can I get the char*...
2
2175
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 enother. But, seems, like there is a logical error, because it runs endless circles and never comes to an end. Could someone fix an error?string Utils::urldecode(string incoming) { string outgoing;//RESULT STRING char current;//CURRENT CHAR char...
3
1682
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 char* App::localize(string local) { if (!localization.count(local)) {
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.