From char* iterators to char* strings

P

pmatos

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
 
A

Andrew Koenig

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?
 
P

pmatos

Andrew said:
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
 
A

Andrew Koenig

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top