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

* [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
@ 2004-12-28  0:15 ` pinskia at gcc dot gnu dot org
  2004-12-28  1:00 ` rolosworld at gmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-12-28  0:15 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-12-28 00:15 -------
You forgot C strings are null terminated.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172


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

* [Bug c++/19172] strcpy bug? or mine?
  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
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 11+ messages in thread
From: rolosworld at gmail dot com @ 2004-12-28  1:00 UTC (permalink / raw)
  To: gcc-bugs


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


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

* [Bug c++/19172] strcpy bug? or mine?
  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:07 ` pinskia at gcc dot gnu dot org
  2004-12-28  1:23 ` rolosworld at gmail dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-12-28  1:07 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-12-28 01:07 -------
I cannot reproduce this on powerpc-darwin with the mainline or 3.3.2? what target are you running on?

Are you sure that you don't have an invisable character in the string?

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172


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

* [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
                   ` (2 preceding siblings ...)
  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
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: rolosworld at gmail dot com @ 2004-12-28  1:23 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From rolosworld at gmail dot com  2004-12-28 01:23 -------
Im using:
"khgukytit8igddddddddddddddddddddddd0"
"0123456789qwertypppppppppppppppppppppppppoooooooooooooooooooooooooooooooooooooooooopppppppppppppp"

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


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

* [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
                   ` (3 preceding siblings ...)
  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
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-12-28  1:27 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-12-28 01:27 -------
Can you try a released FSF gcc?
Also if it is a bug in strcpy, then this is not a bug in GCC since GCC does not contain strcpy but in glibc, 
make sure that equivant C code works too.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172


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

* [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
                   ` (4 preceding siblings ...)
  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
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: rolosworld at gmail dot com @ 2004-12-28  1:31 UTC (permalink / raw)
  To: gcc-bugs


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


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

* [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
                   ` (5 preceding siblings ...)
  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
  8 siblings, 0 replies; 11+ messages in thread
From: sebor at roguewave dot com @ 2004-12-28  1:32 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From sebor at roguewave dot com  2004-12-28 01:32 -------
Subject: Re:  strcpy bug? or mine?

rolosworld at gmail dot com wrote:

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

Don't you mean new char [strlen(str) + 1]? I.e., brackets, not
parentheses (the latter allocates a single char and initializes
it with the length of str).

Martin



-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172


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

* Re: [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  1:00 ` rolosworld at gmail dot com
@ 2004-12-28  1:32   ` Martin Sebor
  0 siblings, 0 replies; 11+ messages in thread
From: Martin Sebor @ 2004-12-28  1:32 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs

rolosworld at gmail dot com wrote:

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

Don't you mean new char [strlen(str) + 1]? I.e., brackets, not
parentheses (the latter allocates a single char and initializes
it with the length of str).

Martin


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

* [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
                   ` (6 preceding siblings ...)
  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
  8 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-12-28  1:34 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-12-28 01:34 -------
That is the bug:
  word = new char( strlen(str) +1);

should be:
  word = new char[ strlen(str) +1];

Otherwise you are allocating only one char.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19172


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

* [Bug c++/19172] strcpy bug? or mine?
  2004-12-28  0:09 [Bug c++/19172] New: strcpy bug? or mine? rolosworld at gmail dot com
                   ` (7 preceding siblings ...)
  2004-12-28  1:34 ` pinskia at gcc dot gnu dot org
@ 2004-12-28  1:36 ` rolosworld at gmail dot com
  8 siblings, 0 replies; 11+ messages in thread
From: rolosworld at gmail dot com @ 2004-12-28  1:36 UTC (permalink / raw)
  To: gcc-bugs


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


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