Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

std::string and char pointers

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

> "Jacques Labuschagne" <(E-Mail Removed)> wrote in message
> Nope, wrong again. I want to be able to do something like:
>
> read( "foo.txt", PointerToChar );
> String.UseThisPointer( PointerToChar, SizeInformationHere );
>


As others have said, it can't be done.

--
To err is human. To forgive is not our policy.
-- MIT Assassins Guild

 
Reply With Quote
 
 
 
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      07-18-2003
"Jacques Labuschagne" <(E-Mail Removed)> wrote in message
news:1874502.j6fyHt1I0n@klesk
> Espen Ruud Schultz wrote:
>
>> "Jacques Labuschagne" <(E-Mail Removed)> wrote in message
>> Nope, wrong again. I want to be able to do something like:
>>
>> read( "foo.txt", PointerToChar );
>> String.UseThisPointer( PointerToChar, SizeInformationHere );
>>

>
> As others have said, it can't be done.
>


Yeah, sometimes I find the standard nothing more than limiting. Lots of
"safe guards" and "proper ways". Bah, I'm just annoyed. Thanx though...

, Espen


 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      07-18-2003
"Espen Ruud Schultz" <(E-Mail Removed)> wrote in news:v1%Ra.7185
$(E-Mail Removed):

> "Jacques Labuschagne" <(E-Mail Removed)> wrote in message
> news:1874502.j6fyHt1I0n@klesk
>> Espen Ruud Schultz wrote:
>>
>>> "Jacques Labuschagne" <(E-Mail Removed)> wrote in message
>>> Nope, wrong again. I want to be able to do something like:
>>>
>>> read( "foo.txt", PointerToChar );
>>> String.UseThisPointer( PointerToChar, SizeInformationHere );
>>>

>>
>> As others have said, it can't be done.
>>

>
> Yeah, sometimes I find the standard nothing more than limiting. Lots

of
> "safe guards" and "proper ways". Bah, I'm just annoyed. Thanx

though...

Well, it's only a matter of using the tool the way it's designed. One
problem with your suggestion is that std::string would have no way of
knowing _how_ that buffer was allocated. It could have been allocated
with new, or it could be a local variable, or it could be a pointer to a
string literal, or.... etc.

As a result std::string wouldn't know what operations are possible on
that buffer. As an example, if you passed it a pointer to a string
literal, your program may explode when you tried to modify one of the
characters later.

A second example problem is that your buffer may have been allocated
using a custom allocator of some sort (perhaps it's allocating from some
sort of shared memory), std::string wouldn't know how to deallocate it.
 
Reply With Quote
 
Stephen Howe
Guest
Posts: n/a
 
      07-19-2003
>So to pop that out of this subject, c_str() is the way to get a pointer.

Not the only way. data() will do so as well without appenidng a terminating
0.

Stephen Howe


 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      07-19-2003
In article <Ev_Ra.511$(E-Mail Removed)2.webusenet.com>,
Jim Fischer <(E-Mail Removed)> wrote:
>Espen Ruud Schultz wrote:
>>
>> 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.


I think the "guts" are implementation-dependent anyway. The internal
representation of std::string data might be something simple like a
dynamically allocated array of char, or it might be something more
complicated that is optimized for dealing with large strings, depending on
whose version of the standard library you're using.

--
Jon Bell <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-19-2003

"Espen Ruud Schultz" <(E-Mail Removed)> wrote in message
news:daYRa.7149$(E-Mail Removed)...
> 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
>


If you want to be fast and loose try this

string buf(10000);
my_func(&buf[0]);

Strictly illegal but if your string implementation uses contiguous memory
then it may well work.

Another option would be to replace string with vector<char> when the above
really would work.

john


 
Reply With Quote
 
Espen Ruud Schultz
Guest
Posts: n/a
 
      07-19-2003
"Stephen Howe" <(E-Mail Removed)> wrote in message
news:3f18a138$0$961$(E-Mail Removed)
>> So to pop that out of this subject, c_str() is the way to get a pointer.

>
> Not the only way. data() will do so as well without appenidng a
> terminating 0.
>


Hmm, this makes me wonder. Are you sure data() doesn't have a zero in the
end? If both c_str() and data() return pointers to the internal text, then
if one of them don't have a zero and the other have one, then std::string
must copy and keep at least two versions of its content when someone call
either c_str() or data()...?

It seems like the best way to deal with this is that std::string only keep
one copy of its content and always have a zero termination at the end...

, Espen


 
Reply With Quote
 
Jacques Labuschagne
Guest
Posts: n/a
 
      07-19-2003
Espen Ruud Schultz wrote:
> I find these problems to be trivial. Of course you have to know your
> std::string and code accordingly...


But the standard does not mandate any particular implementation, only the
interface to the implementation. You are of course free to learn the
internals from scratch for each new standard library implementation you
use, provided that those details are available.
It would perhaps be an illuminating experience were you to attempt an
implementation of std::string...

--
The tube is civilization.

 
Reply With Quote
 
John Dibling
Guest
Posts: n/a
 
      07-19-2003
On Sat, 19 Jul 2003 00:09:08 +0200, "Espen Ruud Schultz"
<(E-Mail Removed)> wrote:

> the only thing that matters is that it take a pointer
>to an already allocated space in memory.


Can't be done; std::string isn't a smart pointer. But, you can
reserve() space in the string, then call your function using
string.begin(). This will look to your function like a plain char*,
and you wont have to allocate a special temporary buffer. I believe I
saw this method in Meyers' Effective STL, if anybody would like to
look it up.

>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...


Because std::string isn't a smart pointer, but you are trying to use
it as if it were. If you really want a smart pointer, use a smart
pointer. std::auto_ptr is a smart pointer you can use, tho I must
admit I personally don't care for it. (Actually, I don't like smart
pointers at all, but that's a topic for another show...)

Repeating another idea many others have suggested, you can also use a
vector<char>, with which you can accomplish anything a string can do,
plus much more. Since memory allocated by vector is guaranteed by the
standard to be contigious, you can use vector::begin() as a char
const*. If you vector::reserve(), you can also use vector::begin()
like a writable char* buffer.

>wish to derive a new class from std::string just for this, and I really just


There are tools available. Don't reinvent the wheel.

</dib>

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

John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-19-2003
John Dibling wrote:
> ...
> Repeating another idea many others have suggested, you can also use a
> vector<char>, with which you can accomplish anything a string can do,
> plus much more. Since memory allocated by vector is guaranteed by the
> standard to be contigious, you can use vector::begin() as a char
> const*. If you vector::reserve(), you can also use vector::begin()
> like a writable char* buffer.
> ...


You can use '&*v.begin()' or '&v.front()' or '&v[0]' (where 'v' is
'std::vector<char>') as char buffer. But not just 'v.begin()'. In some
implementations 'v.begin()' may return a pointer, but it is not true in
general case.

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

 
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