Topic: c_str () and data () methods in std::string


Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/03/09
Raw View
Hi,

1. What is the difference between
        c_str () and data () methods in std::string?

2. Are there any situations when we must use data () but not c_str ()?

        Thanks in avance,
        Alex


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/03/09
Raw View
Alex Vinokur wrote:

> 1. What is the difference between
>         c_str () and data () methods in std::string?

c_str returns a pointer to a null terminated array of charT (of size
size() + 1); data returns a pointer to an array of size size(),
w/o the '\0' at the end

> 2. Are there any situations when we must use data () but not c_str ()?

I don't know of any.

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/09
Raw View
Alex Vinokur wrote:
>
> Hi,
>
> 1. What is the difference between
>         c_str () and data () methods in std::string?
>
> 2. Are there any situations when we must use data () but not c_str ()?

Implementors may choose to implement c_str() in ways that require them
to allocate an extra byte of memory the first time c_str() is called, to
store the terminating '\0'. data() avoids that possibility; a minor
issue, in most cases.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]