You are not logged in.
Pages: 1
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
use
g++ -S your_c++_file.cxxand check the assembly
Offline
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
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
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
You might want to use std::string ![]()
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
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 ![]()
Last edited by Rulatir (2007-11-04 21:00:16)
Offline
@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. ![]()
Offline
@LE_Shatai:
use substr: http://www.cppreference.com/cppstring/substr.html
Offline
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. ![]()
Glad you've gotten your problems solved.
Offline
@drakosha
Thanks for the hint. Somehow I missed this function, as it saves a lot lines of code. ![]()
@Cerebral
I like initialized variables. ![]()
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
Pages: 1