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
6 2641
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.
Banfa 9,065
Recognized Expert Moderator Expert
I feel compelled to ask why would you want to make this change?
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.
If 'char*'is a C string type (which it is)how could you do so and why would you want to ?
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.,...
|
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...
|
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 =...
|
by: Bill Todd |
last post by:
What is the easiest way to convert a char array to string?
--
Bill
|
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?
| |
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
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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: 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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |