public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: string class :: how to get char* from a string object
       [not found] <2B721C6525F0D411B1E900B0D0226BDD023EAAC9@mohmsg01.ad.infos ys.com>
@ 2003-11-18 12:59 ` Eljay Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2003-11-18 12:59 UTC (permalink / raw)
  To: Jyotirmoy Das, gcc-help

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* string class :: how to get char* from a string object
@ 2003-11-18  5:34 Jyotirmoy Das
  0 siblings, 0 replies; 2+ messages in thread
From: Jyotirmoy Das @ 2003-11-18  5:34 UTC (permalink / raw)
  To: gcc-help

Hi,
  I have two question. 
  1) What will be the best way to get char * from a string. Whether this is a good option?
  string str("abcdef");
  char * cstr = const_cast<char *> str.c_str() ; 
    Problem is that, then user can modify the cstr which in turn modify the str object. That is not desirable. 

  2) If i do the following ::
   string str("abcdef");
  Whether string class will append a '\0' after storing the 'abcdef' or not ?
  What will be the capacity of the above string object ? Why it is not the same as no. of character of "abcdef" ?

  Thanking you,
  Jyotirmoy Das
jyotirmoy_das@infosys.com
  

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-11-18 12:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <2B721C6525F0D411B1E900B0D0226BDD023EAAC9@mohmsg01.ad.infos ys.com>
2003-11-18 12:59 ` string class :: how to get char* from a string object Eljay Love-Jensen
2003-11-18  5:34 Jyotirmoy Das

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