public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rolosworld at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/19172] New: strcpy bug? or mine?
Date: Tue, 28 Dec 2004 00:09:00 -0000	[thread overview]
Message-ID: <20041228000900.19172.rolosworld@gmail.com> (raw)

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


             reply	other threads:[~2004-12-28  0:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-28  0:09 rolosworld at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041228000900.19172.rolosworld@gmail.com \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).