This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

istringstream 'read' terminating on a null character


Hi

gcc version: GCC V3.0
platform: RedHat Linux 7.1 / x86

When I use GCC V3.0 I am finding that binary 'read' operations on an
istringstream are terminating on a null character.  The write operations are
unaffected by the null characters and setting the streams to binary does not
resolved the problem.

This behaviour is unexpected and differs from GCC version 2.96 and our other
compilers.  Below I have attached a short example and the results from
running this short program under GCC 3.0, GCC 2.96 and MSVC 6.0 that
illustrate the unexpected behaviour of GCC 3.0.

Where I have made mistakes please point out the error of my ways.  If this
is actually a bug can someone please confirm it (so I know it is not just a
bad install), and solutions/ideas would be appreciated.

Thanks
Mathew


Example:

#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>

using namespace std;

unsigned long int num = 0x12340078;

int main()
{
  string s;
  ostringstream os;//( ios_base::binary );
  os.write(reinterpret_cast<const char*>(&num), sizeof(num));
  s = os.str();

  printf("S length = %d\n", s.size());
  printf(" byte[0] = %x\n", (unsigned char) s[0] );
  printf(" byte[1] = %x\n", (unsigned char) s[1] );
  printf(" byte[2] = %x\n", (unsigned char) s[2] );
  printf(" byte[3] = %x\n", (unsigned char) s[3] );

  unsigned long int dest = 0;

  istringstream is( s );//, ios_base::binary );
  printf("good = %d\n", is.good() );
  is.read( reinterpret_cast<char*>(&dest),sizeof(dest));
  printf("gcount = %d\n", is.gcount());
  printf("dest = %x\n", dest);
  printf("good = %d\n", is.good() );

  return 0;
}

*********************  GCC 2.96 results:

[mathew@oscar mathew]$ g++ numtest2.cpp -o numtest2
[mathew@oscar mathew]$ ./numtest2
S length = 4
 byte[0] = 78
 byte[1] = 0
 byte[2] = 34
 byte[3] = 12
good = 1
gcount = 4
dest = 12340078
good = 1

*********************  MSVC 6 results:

S length = 4
 byte[0] = 78
 byte[1] = 0
 byte[2] = 34
 byte[3] = 12
good = 1
gcount = 4
dest = 12340078
good = 1
Press any key to continue

*********************  GCC 3.00 results:

[mathew@wallace MUItest]$ g++ -I /usr/local/include -L /usr/local/lib
numtest2.cpp -o numtest2
[mathew@wallace MUItest]$ g++ --version
3.0
[mathew@wallace MUItest]$ ./numtest2
S length = 4
 byte[0] = 78
 byte[1] = 0
 byte[2] = 34
 byte[3] = 12
good = 1
gcount = 1
dest = 78
good = 0
[mathew@wallace MUItest]$




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]