[Bug c++/19172] New: strcpy bug? or mine?
I have been trying to figure out why I get this outoput: 4 4 khgukytit8ig 13 # And not this: 4 4 khgukytit8igddd0 36 # THE CODE # #include using namespace std; class MyString { public: MyString( const char * ); ~MyString(); const char *getChar(); private: char *word; }; MyString::MyString( const char *str ) { cout << "Constructor: START" << endl; cout << "str: " << str << " strlen(str): " << strlen(str) << endl; word = new char( strlen(str) ); memset( word, '-', strlen(str) ); cout << word << " " << strlen( word ) << endl; strcpy(word, str); cout << "str: " << str << " strlen(str): " << strlen(str) << endl << "word:" << word << " strlen(word): " << strlen(word) << endl; cout << "Constructor: END" << endl << endl; } MyString::~MyString() { delete [] word; word = 0; } const char * MyString::getChar() { return word; } int main( void ) { MyString str4("khgukytit8igddd0"); MyString str2( "0123456789qwertypoopp" ); cout << sizeof(str4) << " " << sizeof(str4.getChar()) << " " << str4.getChar() << " " << strlen(str4.getChar()) << endl; cout << sizeof(str2) << " " << sizeof(str2.getChar()) << " " << str2.getChar() << " " << strlen(str2.getChar()) << endl; return EXIT_SUCCESS; } btw, Im doing this as excersise, so any buggy things on the code please let me know. -- Summary: strcpy bug? or mine? Product: gcc Version: 3.4.3 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: rolosworld at gmail dot com CC: gcc-bugs at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172
[Bug c++/19172] strcpy bug? or mine?
--- Additional Comments From rolosworld at gmail dot com 2004-12-28 01:00 --- (In reply to comment #1) > You forgot C strings are null terminated. I did this: word = new char( strlen(str) + 1 ); word[strlen(str)] = '\0'; and still get the same output..? -- What|Removed |Added Status|RESOLVED|UNCONFIRMED Resolution|INVALID | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172
[Bug c++/19172] strcpy bug? or mine?
--- Additional Comments From rolosworld at gmail dot com 2004-12-28 01:23 --- Im using: "khgukytit8igddd0" "0123456789qwertypoopp" Im compiling the code with: g++ (GCC) 3.4.3 20041125 (Gentoo Linux 3.4.3-r1, ssp-3.4.3-0, pie-8.7.7) my CFLAGS for gentoo are (the ones used to compile gcc): CFLAGS="-march=athlon-xp -O2 -pipe -mno-fancy-math-387 -mfpmath=sse" aditional info (gcc -v -save-temps -o string string.cpp): Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.3/specs Configured with: /var/tmp/portage/gcc-3.4.3-r1/work/gcc-3.4.3/configure --enable-version-specific-runtime-libs --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4.3 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.3/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.3/include/g++-v3 --host=i686-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --enable-shared --enable-threads=posix --disable-multilib --disable-libgcj --enable-languages=c,c++,f77 Thread model: posix gcc version 3.4.3 20041125 (Gentoo Linux 3.4.3-r1, ssp-3.4.3-0, pie-8.7.7) /usr/libexec/gcc/i686-pc-linux-gnu/3.4.3/cc1plus -E -quiet -v -D_GNU_SOURCE string.cpp -mno-sse2 -mtune=pentiumpro -o string.ii -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172
[Bug c++/19172] strcpy bug? or mine?
--- Additional Comments From rolosworld at gmail dot com 2004-12-28 01:30 --- ok, I'll keep experimenting here, I belive its probably one bug from me :-/ I'll try the C equivalent and try to learn from this! lol thanks for the help -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172
[Bug c++/19172] strcpy bug? or mine?
--- Additional Comments From rolosworld at gmail dot com 2004-12-28 01:36 --- true, didn't notice the brackets. scary bug from me lol thank guys -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172