You are not logged in.

#1 2008-10-09 12:13:07

callmeishmael
Member
Registered: 2008-08-12
Posts: 5

g++ stringchars

Anyone know what the output of this means:

#include <iostream>
using namespace std;


int main(){
    char b;
    b='TARGET';

    cout << b << "\n";
    cout << 'TARGET' << "\n";

    return 0;

}

clearly g++ should give an error instead of a warning, but I have tried it on Mac, Win and Linux but still get only a warning and some stupid output. It's not the memory address (it gives the same value out on each machine), it can't be the ANSI code, for if I recall correctly, each letter would have three digits...

Any clues?

Last edited by callmeishmael (2008-10-09 12:13:47)

Offline

#2 2008-10-09 12:22:34

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,395
Website

Re: g++ stringchars

Moved to programing forum. 

Anyway

cout << '?????RGET'

outputs the same number.  So it seems to be converting the "char" to a long...

Offline

#3 2008-10-09 12:35:05

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,395
Website

Re: g++ stringchars

And here is the explanation....

In ASCII and binary
T = 84 = 01010100
E = 69 = 01000101
G = 71 = 01000111
R = 82 = 01010010

and so
RGET = 01010010010001110100010101010100 = 1380402516

which is the number you get on a 32 bit system.  The additional letters take the binary representation beyond 32 bits and so are lost.

Offline

#4 2008-10-09 13:51:00

callmeishmael
Member
Registered: 2008-08-12
Posts: 5

Re: g++ stringchars

Except I've done this on x86_64... and i686 and got the same on both...
and why does it read it right to left?


This only gives a warning. It should clearly be an error... icc worse gives the G from TARGET out at the first cout
and then gives 1413567047 (i assume the 64bit sum)

Last edited by callmeishmael (2008-10-09 14:03:28)

Offline

#5 2008-10-09 14:04:31

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,395
Website

Re: g++ stringchars

It does not really read right to left.  What it does is truncates the last 32 bits.  It is exactly the same as how assigning 65536 to and unsigned short give you 0 due to its limited range.

Not sure why it gives the same value on x86_64.  I thought it was being cast to an unsigned long which I though had 64 bits on x86_64 and 32 on i686.  I am probably wrong about that.

Offline

#6 2008-10-09 14:09:41

callmeishmael
Member
Registered: 2008-08-12
Posts: 5

Re: g++ stringchars

No, I think you are correct, I think that we have maybe found an optimisation problem on gcc x64 wink
It would surely take longer to put all the 0s in front and then just take the last 32 bits? Certainly to then access the details.

It should still not compile and instead give an error... I think MS actually have this right but can't check the compiler on this machine.

Offline

#7 2008-10-09 18:47:19

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: g++ stringchars

At the very least, 'TARGET' is invalid. Single quotes are only for single characters...

Offline

#8 2008-10-10 07:53:02

callmeishmael
Member
Registered: 2008-08-12
Posts: 5

Re: g++ stringchars

phrakture, that's the point.

It should NOT compile. The ability to output 'garbage' is not a good thing in the compilers.

Offline

#9 2008-10-10 18:38:25

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: g++ stringchars

Well, it's a good use case for using -Werror, but other than that. I'm baffled too

Offline

Board footer

Powered by FluxBB