[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
resend: possible g++ 3.3.3 bug regarding (char&) cast]
From: |
dick . wieland |
Subject: |
resend: possible g++ 3.3.3 bug regarding (char&) cast] |
Date: |
Thu, 4 Nov 2004 16:36:47 -0500 (EST) |
User-agent: |
SquirrelMail/1.4.3a |
---------------------------- Original Message ----------------------------
Subject: [Fwd: possible g++ 3.3.3 bug regarding (char&) cast]
From: address@hidden
Date: Thu, November 4, 2004 3:22 pm
To: address@hidden
--------------------------------------------------------------------------
Following up on my last submission, the problem lies specifically with the
way the compiler resolves the (char&) and (char) casts. In the attached
code, I have uncommented the call to function "get" , and corrected a typo
in the 2nd call to that function. You can see that "get" performs
differently depending on the cast used.
// here is the bug (problem)
// in gcc 3.3.3
// if we say "(char)uc" in the file.get(...) line, the code behaves //
differently than expected.
// formally, the function is declared as ifstream::get(char& c), so you
would // think that specifying the parameter as (char)uc would offend the
// compiler. It does not. Instead it seems to break the reference, // and
ends up effectively leaving the parameter untouched, as if
// it was not being passed by reference any more but by value.
// in the example here, what you see is all zeros, the untouched
// value uc was initialized with.
// in gcc 3.2 either way works correctly
// incorrect in 3.3.3:
// while ( file.get( (char)uc ) ) {
// this works in both gcc 3.2 and gcc 3.3.3
#include <stdio.h>
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
void get( char& );
int main ( int argc, char** argv )
{
string filename;
const size_t maxSize = 100000;
cout << "enter filename [limit " << maxSize << " bytes]: ";
cin >> filename;
cout << "---------- good --------------------" << endl;
{
unsigned char uc = 'X';
unsigned int unLenPacketDataString = 0;
size_t i = 0;
ifstream file( filename.c_str(), ios::binary );
while ( file.get( (char&)uc ) ) {
get( (char&)uc);
cout << uc ;
unLenPacketDataString = ++i;
if ( i == maxSize ) {
cout << " Exceeded buffer size of " << maxSize << endl;
exit( EXIT_FAILURE );
}
}
file.close();
}
// this works in gcc 3.2 but gives erroneous results in gcc 3.3.3 cout
<< "---------- bad --------------------" << endl;
{
unsigned char uc = 'X';
unsigned int unLenPacketDataString = 0;
size_t i = 0;
ifstream file( filename.c_str(), ios::binary );
while ( file.get( (char)uc ) ) {
get( (char)uc);
cout << uc ;
unLenPacketDataString = ++i;
if ( i == maxSize ) {
cout << " Exceeded buffer size of " << maxSize << endl;
exit( EXIT_FAILURE );
}
}
file.close();
}
};
void get( char& c)
{
c='G';
return;
}
---------------------------- Original Message ----------------------------
Subject: possible g++ 3.3.3 bug regarding (char&) cast
From: address@hidden
Date: Thu, November 4, 2004 2:31 pm
To: address@hidden
--------------------------------------------------------------------------
This code snippet works (correctly I think) under gcc 3.2
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib
--enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
and incorrectly(?) under
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --disable-libunwind-exceptions --with-system-zlib
--enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
================================================================== g++
problem.cpp -o problem
//file problem.cpp
// here is the bug (problem)
// in gcc 3.3.3
// If we say "(char)uc" in the file.get(...) line, the code
// compiles but behaves differently than expected. The parameter
// is unchanged on return from file.get, but no error is generated. //
formally, the function is declared as ifstream::get(char& c), so I would
// think that specifying the parameter as (char)uc would offend the //
compiler. It does not.
// In the example here, the "good" code spits out whatever
// file you choose to read; in the "bad" code, what you see is all x's, //
the value uc was initialized with.
// In gcc 3.2 both the "good" and the "bad" code generate
// the same (expected) result.
#include <stdio.h>
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
void get( char& );
int main ( int argc, char** argv )
{
string filename;
const size_t maxSize = 100000;
cout << "enter filename [limit " << maxSize << " bytes]: ";
cin >> filename;
cout << "---------- good --------------------" << endl;
{
unsigned char uc = 'X';
unsigned int unLenPacketDataString = 0;
size_t i = 0;
ifstream file( filename.c_str(), ios::binary );
while ( file.get( (char&)uc ) ) {
//get( (char&)uc);
cout << uc ;
unLenPacketDataString = ++i;
if ( i == maxSize ) {
cout << " Exceeded buffer size of " << maxSize << endl;
exit( EXIT_FAILURE );
}
}
file.close();
}
// this works in gcc 3.2 but gives erroneous results in gcc 3.3.3 cout
<< "---------- bad --------------------" << endl;
{
unsigned char uc = 'X';
unsigned int unLenPacketDataString = 0;
size_t i = 0;
ifstream file( filename.c_str(), ios::binary );
while ( file.get( (char)uc ) ) {
//get( (char&)uc);
cout << uc ;
unLenPacketDataString = ++i;
if ( i == maxSize ) {
cout << " Exceeded buffer size of " << maxSize << endl;
exit( EXIT_FAILURE );
}
}
file.close();
}
};
void get( char& c)
{
c='G';
return;
}
problem.ii
Description: Binary data
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- resend: possible g++ 3.3.3 bug regarding (char&) cast],
dick . wieland <=