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 2649
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., etc.
The intent is to finish by assigning the list to a string that I would
write to disk: recstr = string.join(recd,'')
|
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()
|
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"
|
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 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.
|
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*...
|
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...
|
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))
{
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |