Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::string and char pointers

Reply
Thread Tools

std::string and char pointers

 
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      07-18-2003
Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string's "content" so that the char pointer can point to
the same text? And vice versa; can I give the std::string a pointer and a
length and then give the std::string control over the pointer and its
content?

I'm basically trying to avoid copying large text between an std::string and
a char pointer, and vice versa.

Is there anyhing in the std::string class to support this?

, Espen


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-18-2003
Espen Ruud Schultz wrote:
> Lets say I have a char pointer and an std::string. Is it possible to get a
> pointer to the std::string's "content" so that the char pointer can point to
> the same text?


Yes. Read about 'std::string::c_str()' method. But in any case,
'std::string' will not hand over control of the stored string to the
char pointer. 'std::string' will retain full control of the stored
string and may invalidate the pointer returned by a previous call to
'c_str()'.

> And vice versa; can I give the std::string a pointer and a
> length and then give the std::string control over the pointer and its
> content?


No. 'std::string' will make a copy.

> I'm basically trying to avoid copying large text between an std::string and
> a char pointer, and vice versa.


The best strategy in this case is to use 'std::string' alone and try to
avoid using strings "owned" by char pointers.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

 
Reply With Quote
 
 
 
 
John Dibling
Guest
Posts: n/a
 
      07-18-2003
On Fri, 18 Jul 2003 13:22:09 -0700, Andrey Tarasevich
<(E-Mail Removed)> wrote:

>Espen Ruud Schultz wrote:
>> Lets say I have a char pointer and an std::string. Is it possible to get a
>> pointer to the std::string's "content" so that the char pointer can point to
>> the same text?

>
>Yes. Read about 'std::string::c_str()' method. But in any case,


No! Sure you can get a char const* from a string, but it's not
guaranteed to be any good after youv'e used it. If you use c_str(),
you have to use that pointer right away, then throw it away. If you
try to save the pointer and use it later, you *could* have a wild
pointer.

Long story short, you need to deep-copy the string. Why do you want a
char* to point to a std::string buffer anyway? Duplication of data is
*usually* a bug.

21.3.6.2 states:

Requires: [snip] Nor shall the p[rogram treat the retuned valuie as a
valid pointer value after any subsequent call to a non-const
function... [snip]

>> And vice versa; can I give the std::string a pointer and a
>> length and then give the std::string control over the pointer and its
>> content?

>
>No. 'std::string' will make a copy.


Right, but you could use an auto_ptr instead of a string.

Just what is it you are trying to accomplish?

</dib>

John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection

 
Reply With Quote
 
Oliver Fleischmann
Guest
Posts: n/a
 
      07-18-2003
On Fri, 18 Jul 2003 21:49:47 +0200, "Espen Ruud Schultz"
<(E-Mail Removed)> wrote:

>Lets say I have a char pointer and an std::string. Is it possible to get a
>pointer to the std::string's "content" so that the char pointer can point to
>the same text? And vice versa; can I give the std::string a pointer and a
>length and then give the std::string control over the pointer and its
>content?
>
>I'm basically trying to avoid copying large text between an std::string and
>a char pointer, and vice versa.
>
>Is there anyhing in the std::string class to support this?
>
>, Espen
>


std::string.c_str()

Olli

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-18-2003
John Dibling wrote:
> ...
>>> Lets say I have a char pointer and an std::string. Is it possible to get a
>>> pointer to the std::string's "content" so that the char pointer can point to
>>> the same text?

>>
>>Yes. Read about 'std::string::c_str()' method. But in any case,

>
> No! Sure you can get a char const* from a string, but it's not
> guaranteed to be any good after youv'e used it.


This doesn't make any sense. What is a "used pointer"? There is no such
notion in C++. The pointer obtained from 'c_str()' may be used as many
times as user wants as long as this pointer remains valid. The actions
that invalidate this pointer are clearly described in the specification
of C++ standard library. You actually quite it below.

> If you use c_str(),
> you have to use that pointer right away, then throw it away.


There in so such things as "right away" and "throw away" in the
specification of standard library. The period this pointer remains valid
is unambiguously defined and depends on the behavior of the concrete
program.

> If you
> try to save the pointer and use it later, you *could* have a wild
> pointer.


Yes, you could. Just like I said in my original message, 'std::string'
doesn't relinquish control of the controlled sequence.

> Long story short, you need to deep-copy the string. Why do you want a
> char* to point to a std::string buffer anyway?


There are circumstances when you need to deep copy a string and there
are circumstances when you don't need to deep copy a string. It depends
on concrete context.

> Duplication of data is *usually* a bug.


I don't understand what you are trying to say. After promoting data
duplication for a while, you suddenly declare it to be a bug.

> 21.3.6.2 states:
>
> Requires: [snip] Nor shall the p[rogram treat the retuned valuie as a
> valid pointer value after any subsequent call to a non-const
> function... [snip]


Once again, why did you start response with "No!" and then went on to
basically restate everything that I said in my previous message. Did you
try to read what I said before replying?

>>> And vice versa; can I give the std::string a pointer and a
>>> length and then give the std::string control over the pointer and its
>>> content?

>>
>>No. 'std::string' will make a copy.

>
> Right, but you could use an auto_ptr instead of a string.
> ...


Huh? Just how do you plan to use an 'auto_ptr' in this case?

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

 
Reply With Quote
 
John Dibling
Guest
Posts: n/a
 
      07-18-2003
On Fri, 18 Jul 2003 14:16:21 -0700, Andrey Tarasevich
<(E-Mail Removed)> wrote:


>Yes, you could. Just like I said in my original message, 'std::string'
>doesn't relinquish control of the controlled sequence.



You are absolutely right. I did not read the words "'std::string'
will retain full control of the stored string and may invalidate the
pointer returned by a previous call to 'c_str()'." in your original
post due to careless reading, and I misunderstood your argument to be
a faulty one. My humble apologies.

</dib>

John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection
 
Reply With Quote
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      07-18-2003
Thanx guys! I'm replying to my own post for several reason, one of them is
to "start over" so to speak...

Anyway, I knew about c_str() but I thought there was another way that didn't
use zero termination. The reson why I needed it not to be zero terminated
was trivial, and after I thought about it, non-existing. So to pop that out
of this subject, c_str() is the way to get a pointer.

Though getting a pointer to the content isn't the main thing I want, but
giving a pointer to an std::string and just say "Here, take this pointer and
the text it points to. This is yours. From now on I want to deal with this
text through you."

This is the abstract of what I'm trying to do, and the reason is simple: I
read a text file from disk and into memory. The function that reads the
file is unimportant, the only thing that matters is that it take a pointer
to an already allocated space in memory. The file in question can be big,
or it can be huge. Either way, I find it unnecessary to have to copy the
text to get it into an std::string object. And since internally,
std::string do keep a single pointer to its content, why not just tell
std::string it can use an existing pointer? That way I don't have to copy
the actual text...

So is there any way for me to give an std::string object an existing pointer
and have the std::string use that pointer exclusively? Though I standard
way to go about this would be best, I really don't care. However, I do not
wish to derive a new class from std::string just for this, and I really just
hope there is a "proper" way to do this...

, Espen


 
Reply With Quote
 
Jacques Labuschagne
Guest
Posts: n/a
 
      07-18-2003
Espen Ruud Schultz wrote:

> Thanx guys! I'm replying to my own post for several reason, one of them
> is to "start over" so to speak...
>
> Anyway, I knew about c_str() but I thought there was another way that
> didn't
> use zero termination. The reson why I needed it not to be zero terminated
> was trivial, and after I thought about it, non-existing. So to pop that
> out of this subject, c_str() is the way to get a pointer.


Zero-termination really has nothing to do with it... You want a pointer
to the string's internal representation so you can modify it, yes?
You want to be able to say something like
read_file("foo.txt", my_string.get_internal_buffer());
Right?
That's not how strings work. You can't get that kind of access to the
internals of a string.

You have two options. (a) Copy the text from char* to std::string or (b)
rewrite the function that reads in the file.

--
It isn't that unix isn't a user friendly operating system,
it's just choosy about which users it wants to be friends
with, and even the best of friends occasionally fight.

 
Reply With Quote
 
Jim Fischer
Guest
Posts: n/a
 
      07-18-2003
Espen Ruud Schultz wrote:
> Lets say I have a char pointer and an std::string. Is it possible to get a
> pointer to the std::string's "content" so that the char pointer can point to
> the same text? And vice versa; can I give the std::string a pointer and a
> length and then give the std::string control over the pointer and its
> content?
>
> I'm basically trying to avoid copying large text between an std::string and
> a char pointer, and vice versa.
>
> Is there anyhing in the std::string class to support this?


If I understand your question correctly, I think the answer is "no".

Comment 1) IIRC, there is no standard way to obtain a char* pointer to
the actual "guts" of a std::string object.

[n.b. If you invoke the std::string object's c_str() method, the method
returns a pointer to a const-valued (unmodifiable) char array whose
contents "match" the contents of the std::string object. Note that the
returned pointer becomes invalid "after any subsequent call to a
non-const member function of the class basic_string that designates the
same object as this." [See: 21.3.6/2] For example:
std::string s("hello");
const char *p = s.c_str();
s += '*'; // modifying 's' invalidates the address in 'p'
// 'p' is now invalid
]


Comment 2) AFAIK, the std::string class is not designed to use an
existing char array as the "guts" of a std::string object. IOW, the
contents of the existing char array are copied into the "guts" of the
std::string object.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      07-18-2003
"Jacques Labuschagne" <(E-Mail Removed)> wrote in message
news:3111839.KXdPFfGyGt@klesk
> Espen Ruud Schultz wrote:
>
>> Thanx guys! I'm replying to my own post for several reason, one of them
>> is to "start over" so to speak...
>>
>> Anyway, I knew about c_str() but I thought there was another way that
>> didn't
>> use zero termination. The reson why I needed it not to be zero
>> terminated was trivial, and after I thought about it, non-existing. So
>> to pop that out of this subject, c_str() is the way to get a pointer.

>
> Zero-termination really has nothing to do with it... You want a pointer
> to the string's internal representation so you can modify it, yes?
>


No, I don't. I want a pointer to the std::string's internal representation
so I can read it. Excuse me for being rude, but I already said that the
part about getting the pointer was done with...

>
> You want to be able to say something like
> read_file("foo.txt", my_string.get_internal_buffer());
> Right?
>


Nope, wrong again. I want to be able to do something like:

read( "foo.txt", PointerToChar );
String.UseThisPointer( PointerToChar, SizeInformationHere );

, Espen


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments