[Bug c++/19172] New: strcpy bug? or mine?

rolosworld at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Dec 28 00:09:00 GMT 2004


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



More information about the Gcc-bugs mailing list