public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/19172] New: strcpy bug? or mine?
@ 2004-12-28  0:09 rolosworld at gmail dot com
  2004-12-28  0:15 ` [Bug c++/19172] " pinskia at gcc dot gnu dot org
                   ` (8 more replies)
  0 siblings, 9 replies; 11+ messages in thread
From: rolosworld at gmail dot com @ 2004-12-28  0:09 UTC (permalink / raw)
  To: gcc-bugs

I have been trying to figure out why I get this outoput:
4 4 khgukytit8ig 13

#########################

And not this:

4 4 khgukytit8igddddddddddddddddddddddd0 36

#############################################

THE CODE

#############################################

#include <iostream>

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("khgukytit8igddddddddddddddddddddddd0");
  MyString str2(
"0123456789qwertypppppppppppppppppppppppppoooooooooooooooooooooooooooooooooooooooooopppppppppppppp"
);

  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


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

end of thread, other threads:[~2004-12-28  1:36 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
2004-12-28  0:15 ` [Bug c++/19172] " pinskia at gcc dot gnu dot org
2004-12-28  1:00 ` rolosworld at gmail dot com
2004-12-28  1:32   ` Martin Sebor
2004-12-28  1:07 ` pinskia at gcc dot gnu dot org
2004-12-28  1:23 ` rolosworld at gmail dot com
2004-12-28  1:27 ` pinskia at gcc dot gnu dot org
2004-12-28  1:31 ` rolosworld at gmail dot com
2004-12-28  1:32 ` sebor at roguewave dot com
2004-12-28  1:34 ` pinskia at gcc dot gnu dot org
2004-12-28  1:36 ` rolosworld at gmail dot com

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