Subject: Re: [c++ stl] std::string bug?
From: "Dylan Cuthbert" <[email-masked]>
To: [email-masked]
Date: Tue, 5 Mar 2002 15:50:13 +0900
Newsgroups: gmane.os.cygwin

data doesn't necessarily return a zero-terminated string, it simply returns
a ptr to the raw data.  It is only zero-terminated if you call c_str() which
is, of course, why there are two functions in the first place.

This is in the wrong mailing list however, redirect it to the appropriate
gnu gcc mailing list.

Regards

---------------------------------
Q-Games, Dylan Cuthbert.
http://www.q-games.com/personal/utils

"Gwenaël Durand" <[email-masked]> wrote in message
news:[email-masked]...
> Hi,
>
> It seems that one constructor of the std::string class or the data()
method
> doesn't work properly.
>
> string::data() and string::c_str() are supposed to be equivalent but don't
> always behave the same.
>
> have a look at the following  code and its test.
>
> ....?
>
> Thanks,
> Gwenaël
>
> PS: I update cygwin this morning => 1.3.9
> __________________________________
> #include <iostream>
> #include <string>
> using namespace std;
>
> void foo(const std::string& _str){
>       cout<<"foo: cout<<_str            => "<< _str   <<endl;
>       cout<<"foo: cout<<_str.data() => "<< _str.data()  <<endl;
>       cout<<"foo: cout<<_str.c_str()=> "<< _str.c_str() <<endl;
>       cout<<"foo: _str.length()         => "<< _str.length()<<endl<<endl;
> }
>
> void main(int argc, char **argv)
> {
>   std::string str;
>   if (argc > 1) str = argv[1];
>   else str = "OOOOOOO";
>
>   cout<<"----------foo(str);"<<endl;
>   foo(str);
>
>   cout<<"----------foo(\"say hello!\");"<<endl;
>   foo("say hello!");
>
>   cout<<"----------foo(\"...\");"<<endl;
>   foo("...");
>
>   cout<<"----------foo(str);"<<endl;
>   foo(str);
> }
> _________________________________
>
> > g++ code.cpp -o code.exe
> > code.exe
> ----------foo(str);
> foo: cout<<_str            => OOOOOOO
> foo: cout<<_str.data()  => OOOOOOO
> foo: cout<<_str.c_str() => OOOOOOO
> foo: _str.length()           => 7
>
> ----------foo("say hello!");
> foo: cout<<_str           => say hello!
> foo: cout<<_str.data() => say hello!
> foo: cout<<_str.c_str()=> say hello!
> foo: _str.length()         => 10
>
> ----------foo("...");
> foo: cout<<_str           => ...
> foo: cout<<_str.data() => ... hello!                    <<<<<=========
> ???????????!!!!!!!
> foo: cout<<_str.c_str()=> ...
> foo: _str.length()         => 3
>
> ----------foo(str);
> foo: cout<<_str           => OOOOOOO
> foo: cout<<_str.data() => OOOOOOO
> foo: cout<<_str.c_str()=> OOOOOOO
> foo: _str.length()          => 7
> >
> __________________________________
>
> From my cygcheck.out:
>     Cygwin DLL version info:
>         DLL version: 1.3.9
>         DLL epoch: 19
>         DLL bad signal mask: 19005
>         DLL old termios: 5
>         DLL malloc env: 28
>         API major: 0
>         API minor: 51
>         Shared data: 3
>         DLL identifier: cygwin1
>         Mount registry: 2
>         Cygnus registry name: Cygnus Solutions
>         Cygwin registry name: Cygwin
>         Program options name: Program Options
>         Cygwin mount registry name: mounts v2
>         Cygdrive flags: cygdrive flags
>         Cygdrive prefix: cygdrive prefix
>         Cygdrive default prefix:
>         Build date: Mon Jan 21 12:48:41 EST 2002
>         Shared id: cygwin1S3
>
>
> Cygwin Package Information
> Last downloaded files to: E:\downloads\Cygwin
> Last downloaded files from:
> ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus
>
> Package             Version
> ash                 20020131-1
> autoconf            2.52a-1
> autoconf-devel      2.52-4
> autoconf-stable     2.13-4
> automake            1.5b-1
> automake-devel      1.5-5
> automake-stable     1.4p5-5
> bash                2.05a-3
> binutils            20011002-1
> curl                7.9.3-1
> cygutils            0.9.9-1
> cygwin              1.3.9-1
> diff                0.0
> fileutils           4.1-1
> findutils           4.1
> gawk                3.0.4-1
> gcc                 2.95.3-5
> gdbm                1.8.0-3
> grep                2.5g
> groff               1.17.2-1
> gzip                1.3.2-1
> jbigkit             1.2-6
> jpeg                6b-7
> less                358-3
> libintl             0.10.38-3
> libintl1            0.10.40-1
> libncurses5         5.2-1
> libncurses6         5.2-8
> libpng              1.0.12-1
> libpng2             1.0.12-1
> libreadline4        4.1-2
> libreadline5        4.2a-1
> libxml2             2.4.13-1
> libxslt             1.0.10-1
> login               1.4-3
> m4                  0.0
> make                3.79.1-5
> man                 1.5g-2
> mingw-runtime       1.2-1
> mktemp              1.4-1
> ncurses             5.2-8
> newlib-man          20001118-1
> opengl              1.1.0-5
> openssl             0.9.6c-3
> pcre                3.7-1
> perl                5.6.1-2
> popt                1.6.2-1
> readline            4.2a-1
> sed                 3.02-1
> sh-utils            2.0-2
> tar                 1.13.19-1
> termcap             20010825-1
> terminfo            5.2-1
> textutils           2.0.21-1
> tiff                3.5.7-1
> vim                 6.0.93-1
> w32api              1.2-1
> wget                1.8.1-1
> which               1.5-1
> xpm-nox             4.2.0-1
> zlib                1.1.3-7
>
>
>
>
> --
> ______________________________________
> Gwenael . Durand@laposte . net
> http://perso.wanadoo.fr/gwenael.durand
> ______________________________________
>
>
>
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Bug reporting:         http://cygwin.com/bugs.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
>
>

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/