You are not logged in.

#1 2007-11-04 10:14:49

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

GCC/g++ Bug ?

Hi

I think I found a bug in the g++ of gcc-4.2.2 of Arch64, .
This is the code:

#include <string>
int main( )
{
    char *str = new char[20];
    cout << strlen(str) << endl;

    delete [] str;
    return 0;
}

So nothing special here.
The bug is, that the output ist always:

6

Even if I set the char to any other value, its size does not change.
I checked it against an gcc-4.1.2 kUbunut-32bit Linux and there is works.
Used compiler is:

g++

.

So can somebody confirm this?

CU
Martin

Offline

#2 2007-11-04 11:27:27

bboozzoo
Member
From: Poland
Registered: 2006-08-01
Posts: 129

Re: GCC/g++ Bug ?

use

g++ -S your_c++_file.cxx

and check the assembly

Offline

#3 2007-11-04 12:21:33

Rulatir
Banned
Registered: 2007-02-05
Posts: 94

Re: GCC/g++ Bug ?

LE_Shatai wrote:

Hi

I think I found a bug in the g++ of gcc-4.2.2 of Arch64, .
This is the code:

#include <string>
int main( )
{
    char *str = new char[20];
    cout << strlen(str) << endl;

    delete [] str;
    return 0;
}

So nothing special here.
The bug is, that the output ist always:

6

So can somebody confirm this?

Your problem is related to C/C++ programming. It is not specific to Arch Linux.

That said, your snippet doesn't even compile because you don't include <iostream> and because cout and endl are in namespace std, so you should write std::cout and std::endl.

Those errors fixed, you allocate an array of 20 characters but never initialize it, so it contains garbage. C strings are just arrays of characters, where end of string is indicated by null character (a byte with numerical value 0). What strlen does is simply searching for a null character starting from the given address and returning the offset. It just so happens that the garbage your uninitialized array contains has the first null character at offset 6, so strlen returns 6. When I fixed, compiled and ran your program, the result was 0, not 6. It is even possible for this program to crash if the array doesn't contain any nulls, in which case strlen would run past the end of the array and possibly into memory you don't own, causing memory violation.

Offline

#4 2007-11-04 13:37:37

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

Re: GCC/g++ Bug ?

Yes you'r right. I made a mistake, but it's confusing as it works with gcc-4.1.2 . And yes the givn code above does not compile.
I noticed this later, but the intention was to make clear what i ment.
But actually, shame on me.

Ok, so I had initialize the char-array.
I use memset(...) now.

// name is type char*

if(infile != NULL)
    delete [] infile;

infile = new char[strlen(name)-1];
memset(infile, 0, strlen(name)-1);

This works, but is there another possibility to initialize it?

CU
Martin

Offline

#5 2007-11-04 14:54:43

bboozzoo
Member
From: Poland
Registered: 2006-08-01
Posts: 129

Re: GCC/g++ Bug ?

pretty simple

void * operator new[] (size_t size) {
    void * ptr = calloc(size);
    if (NULL == ptr)
        throw std::bad_alloc;
    return ptr;
}

or use malloc & memset

but then you must provide your delete[] operator to free(2) the mem

Last edited by bboozzoo (2007-11-04 14:58:05)

Offline

#6 2007-11-04 18:05:14

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: GCC/g++ Bug ?

You might want to use std::string wink

BTW, "strlen(...)-1" u use is strange to me, you probably meant "strlen(...)+1"

Last edited by drakosha (2007-11-04 18:06:09)

Offline

#7 2007-11-04 20:52:44

Rulatir
Banned
Registered: 2007-02-05
Posts: 94

Re: GCC/g++ Bug ?

If you want to ensure that the allocated block pointed to by infile contains a valid empty C string, then all you need is:

*infile='\0';

or if you want to express it more explicitly,

infile[0]='\0';

that is, all you need is to put one terminating null character at offset 0. You don't have to initialize the whole array with memset because string functions will stop at first '\0' anyway. And as drakosha already said, use std::string smile

Last edited by Rulatir (2007-11-04 21:00:16)

Offline

#8 2007-11-05 08:03:48

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

Re: GCC/g++ Bug ?

@drakosha
My target was to cut the 'first' and the 'last' character of a string, and I did not find any std::string function that did this.
So the infile-string is 2 characters shorter, but with the terminating '\0' it's only one character.

@Rulatir:
Thanks for the hint, I will try this.
Makes the code much prettier. smile

Offline

#9 2007-11-05 10:12:41

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: GCC/g++ Bug ?

Offline

#10 2007-11-05 13:05:42

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: GCC/g++ Bug ?

LE_Shatai wrote:

Yes you'r right. I made a mistake, but it's confusing as it works with gcc-4.1.2 .

Ah, the joys of uninitialized memory.  It "worked" with your other version of gcc because the memory just happened to have a '\0' at the right place.  It's totally a coincidence, fluke, luck. smile

Glad you've gotten your problems solved.

Offline

#11 2007-11-05 21:20:24

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

Re: GCC/g++ Bug ?

@drakosha
Thanks for the hint. Somehow I missed this function, as it saves a lot lines of code. smile

@Cerebral
I like initialized variables. wink
But somehow I thought when I call >new char[i]< it's already initialized.
Don't ask me how I came to this conclusion...

Offline

Board footer

Powered by FluxBB