From: Eljay Love-Jensen <eljay@adobe.com>
To: Jyotirmoy Das <Jyotirmoy_Das@infosys.com>, gcc-help@gcc.gnu.org
Subject: Re: string class :: how to get char* from a string object
Date: Tue, 18 Nov 2003 12:59:00 -0000 [thread overview]
Message-ID: <5.2.1.1.0.20031118064040.0185b770@iplan-mn.corp.adobe.com> (raw)
In-Reply-To: <2B721C6525F0D411B1E900B0D0226BDD023EAAC9@mohmsg01.ad.infos ys.com>
Hi Jyotirmoy,
Here's a routine that produces a char* from a string:
char* charArray(std::string const& s)
{
char* rv = new char[s.length()+1];
strcpy(rv, s.c_str(), s.length()+1);
return rv;
}
Note, you are responsible for doing the memory cleanup:
char* p = charArray(s);
delete[] p;
>Whether [casting away const] is a good option?
Not really. You'd be better off ADDING const to those parts that are working with the char const*.
Stripping off the const is changing the contract, which the std::string doesn't necessarily support. (Can vary from implementation to implementation. What may work on platform XYZ may not work on platform ABC.)
NOTE: manipulating characters through a char* (where the const has been stripped off) does NOT necessarily manipulate the characters in the underlying std::string!
>Whether string class will append a '\0' after storing the 'abcdef' or not ?
Not necessarily. The std::string has a length specifier, akin to Pascal-style strings (but different in that the length isn't stored at the head of the character array).
>What will be the capacity of the above string object ?
Varies from implementation to implementation.
Use the std::string capacity() method to ascertain, on a case-by-case basis.
>Why it is not the same as no. of character of "abcdef" ?
The std::string makes a tradeoff between string manipulation efficiency and storage overhead. The "slack" bytes are used to reduce the number of re-allocs during the life of the string.
Whether or not the capacity is the same as the number of characters of "abcdef" can be determined by the std::string capacity() method.
Stroustrup's C++ Programming Language 20.3 talks about std::string.
HTH,
--Eljay
next parent reply other threads:[~2003-11-18 12:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <2B721C6525F0D411B1E900B0D0226BDD023EAAC9@mohmsg01.ad.infos ys.com>
2003-11-18 12:59 ` Eljay Love-Jensen [this message]
2003-11-18 5:34 Jyotirmoy Das
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5.2.1.1.0.20031118064040.0185b770@iplan-mn.corp.adobe.com \
--to=eljay@adobe.com \
--cc=Jyotirmoy_Das@infosys.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).