www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/25/21:24:33

From: bowman AT montana DOT com (bowman)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Converting a string to a char *
References: <840i65$auc$1 AT bob DOT news DOT rcn DOT net> <MPG DOT 12ce1d301f91f34e98b78e AT news DOT freeserve DOT net> <auV84.1816$GS5 DOT 3306 AT newsfeed DOT slurp DOT net> <MPG DOT 12ceeb5dd57db0a298b796 AT news DOT freeserve DOT net>
User-Agent: slrn/0.9.5.7 (UNIX)
Lines: 27
Message-ID: <re794.2258$GS5.3538@newsfeed.slurp.net>
Date: Sat, 25 Dec 1999 17:34:15 GMT
NNTP-Posting-Host: 208.4.224.153
X-Trace: newsfeed.slurp.net 946143255 208.4.224.153 (Sat, 25 Dec 1999 11:34:15 CDT)
NNTP-Posting-Date: Sat, 25 Dec 1999 11:34:15 CDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Andrew R. Gillett <arganoid AT fatal-design DOT com> wrote:

>I don't know exactly how c_str() works - does the char array created by 
>it continue to exist after the line on which it is called?

Not reliably. To quote Stroustrup, "The user cannot rely on its value after
a subsequent call on a non-const function on the string." He is talking about
data(), but the next sentence states c_str is like data, but adds the zero
terminator.
(C++ Programming Language 3rd Ed pg 589)

Also, the preferred method to get the string into a buffer is the copy() member
of the basic_string class, if you're a C++ purist. Of course, if you're a
purist, you probably are using the C++ interface rather than the C version.

One possible pitfall is C++ strings may contain embedded '\0' characters, since
the length is of the string is stored separately, and does not depend on the
'\0' terminator. The entire string will be correctly copied to a buffer, but 
the C style functions will not see anything past the first '\0'.

Also, it is possible to derive classes from basic_string that will not act as
expected if converted to C style and handled with the usual calls. C is not
always graceful if naive assumptions are made when handling
internationalization.



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019