473,750 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

From char* iterators to char* strings

Hi all,

I have 2 char* iterators str and end and I'm doing as follows:
string id_str(str, end);
const char * id = id_str.c_str();

but these has 2 problems afaik. One, I'm generating a string as an
intermediate step to get a char*, which seems useless. Two, I don't
know when id gets destroyed or when the chars to where id points to are
cleaned. I could now use strcopy to copy id to a freshly allocated
string but... is there any more direct way? (more efficient perhaps???)

Cheers,

Paulo Matos

Jul 23 '05 #1
3 1450
"pmatos" <po**@sat.ine sc-id.pt> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I have 2 char* iterators str and end and I'm doing as follows:
string id_str(str, end);
const char * id = id_str.c_str();

but these has 2 problems afaik. One, I'm generating a string as an
intermediate step to get a char*, which seems useless. Two, I don't
know when id gets destroyed or when the chars to where id points to are
cleaned. I could now use strcopy to copy id to a freshly allocated
string but... is there any more direct way? (more efficient perhaps???)


Well, you can always do this:

size_t len = end - str;
char* id = new char[len+1];
std::copy(str, end, id);
id[len] = '\0';

However, before following this path, I suggest you think about why you want
a char* in the first place. It just adds to your bookkeeping hassles. Why
not just use the string itself?
Jul 23 '05 #2

Andrew Koenig wrote:
"pmatos" <po**@sat.ine sc-id.pt> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I have 2 char* iterators str and end and I'm doing as follows:
string id_str(str, end);
const char * id = id_str.c_str();

but these has 2 problems afaik. One, I'm generating a string as an
intermediate step to get a char*, which seems useless. Two, I don't
know when id gets destroyed or when the chars to where id points to are cleaned. I could now use strcopy to copy id to a freshly allocated
string but... is there any more direct way? (more efficient
perhaps???)
Well, you can always do this:

size_t len = end - str;
char* id = new char[len+1];
std::copy(str, end, id);
id[len] = '\0';

However, before following this path, I suggest you think about why you want a char* in the first place. It just adds to your bookkeeping hassles. Why not just use the string itself?


Oh well, I might well start a flame war... hope not. However, I'm
programming for efficiency and it seems to me handling char * to be
faster than handling strings. Would I be incorrect for any reason?
Anyway, even if using strings, I'd need to use string * a lot since I'd
be passing them around and I don't wish to be passing them by value.

Cheers,

Paulo Matos

Jul 23 '05 #3
"pmatos" <po**@sat.ine sc-id.pt> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Oh well, I might well start a flame war... hope not. However, I'm
programming for efficiency and it seems to me handling char * to be
faster than handling strings. Would I be incorrect for any reason?
Have you measured it? The differences, if any, would depend on the
particular implementations of strings and memory allocation that you happen
to be using.
Anyway, even if using strings, I'd need to use string * a lot since I'd
be passing them around and I don't wish to be passing them by value.


Or perhaps define a class that lets you get the performance characteristics
you want in a more disciplined way.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2030
by: Vaca Louca | last post by:
Hello, I write an ISAPI authentication module which uses Berkeley DB and want it to be as efficient as possible. Both ISAPI and BerkeleyDB use arrays of chars (char *) to pass and receive information. I know that C++ strings are supposed to be the "right" way to handle strings but I suspect that converting "char *" to string
17
3356
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
3
16823
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
2
22605
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
3
1829
by: edu.mvk | last post by:
Hi, i have the following code which updates the vector of strings( p_vector ) each time it goes into the loop for the first iteration we have only one string in the vector. But in the while loop we are updating the vector and adding few more strings at the end. I'm expecting that In the second iteration the "it" should point to the updated vector, but it is pointing to the "NULL".
4
3576
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by 2 applications (or will be at least when it is done). the structure is declared in the procedure under the pointer infoVar. members tXXL are integer lengths of the strings that as all saved (concatenated) at address of oAndS_str, which is of type...
4
455
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING COMPACT COHERENT CODE THAT WORKS WITH STRINGS, IT ALL BEGINS TO MAKE SINCE*/ /* The basics of C++ are Classes, that build Types. Which are used to create quick and dirty routines in the smallest possible space. The Classes & Routines uses the...
26
13801
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma and return "12384" What is the most efficient, fastest way to approach this? Thanks,
9
3813
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal representation of vector<string>? Will the vector contain the string objects or will it contain pointers/references to the string objects? The reason I ask is that it is not clear to me how the v.reserve() and &v operations would work for a vector<string>.
0
8838
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
9577
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
9396
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
9339
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
9256
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
6804
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
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
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
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.