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]
Other format: [Raw text]

[Bug c++/11882] New: Uninitialised bool variables do not work properly


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11882

           Summary: Uninitialised bool variables do not work properly
           Product: gcc
           Version: 3.2.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rescuer at polettix dot it
                CC: gcc-bugs at gcc dot gnu dot org

[Excuse me if the "triplet" fields are empty, I have no clue of their meaning.]
[Yes, I do know that uninitialised variables are not good programming]

I think there's a bug in GCC 3.2.2 dealing with correct bool type handling.
Please forgive me if this is not a bug.

Consider the following fragment of code (please note that tmp and tmp2 are not
initialised):

// BEGIN FRAGMENT OF CODE, compile with g++
#include <iostream>

typedef bool mybool;

int main(int argc, char *argv[])
{
	mybool tmp;
	bool tmp2;
	std::cout << "Now tmp is " << (tmp ? "true" : "false") << std::endl;
	std::cout << "Now tmp2 is " << (tmp2 ? "true" : "false") << std::endl;
	tmp = !tmp;
	std::cout << "Now tmp is " << (tmp ? "true" : "false") << std::endl;
	std::cout << "Now tmp2 is " << (tmp2 ? "true" : "false") << std::endl;
	return 0;
}
// END FRAGMENT OF CODE

The net effect of this code *could* be a print like the following:

>>>>  Now tmp is true
>>>>  Now tmp2 is true
>>>>  Now tmp is true
>>>>  Now tmp2 is true

which seems buggy due to the fact that the bool and mybool types should have
only two valid states, i.e. true and false.

Looking more in depht with gdb, I discovered that mybool is really defined
something like a char or similar. The lack of initialisation *could* lead to
initial values different from 0 or 1 (in my case, 64, but in the original code
was 243), so the following happens:

* when tmp = !tmp; is called, only the last bit is changed
* when the test is called, all bits are considered (hence the test will be
always true if the random initial value is different from 0 and 1).

Is this a bug? I think so, but I'm not in the standardisation process! Here
follow my specs:
g++ --version
g++ (GCC) 3.2.2
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 

g++ -v
Reading specs from /usr/lib/gcc-lib/i386-slackware-linux/3.2.2/specs
Configured with: ../gcc-3.2.2/configure --prefix=/usr --enable-shared
--enable-threads=posix --enable-__cxa_atexit --disable-checking --with-gnu-ld
--verbose --target=i386-slackware-linux --host=i386-slackware-linux
Thread model: posix
gcc version 3.2.2

Note: the compiler is what is shipped with Slackware 9.0.


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