public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/11882] New: Uninitialised bool variables do not work properly
@ 2003-08-11 13:03 rescuer at polettix dot it
  2003-08-11 13:12 ` [Bug c++/11882] " schwab at suse dot de
  2003-08-11 15:28 ` bangerth at dealii dot org
  0 siblings, 2 replies; 3+ messages in thread
From: rescuer at polettix dot it @ 2003-08-11 13:03 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/11882] Uninitialised bool variables do not work properly
  2003-08-11 13:03 [Bug c++/11882] New: Uninitialised bool variables do not work properly rescuer at polettix dot it
@ 2003-08-11 13:12 ` schwab at suse dot de
  2003-08-11 15:28 ` bangerth at dealii dot org
  1 sibling, 0 replies; 3+ messages in thread
From: schwab at suse dot de @ 2003-08-11 13:12 UTC (permalink / raw)
  To: gcc-bugs

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


schwab at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


------- Additional Comments From schwab at suse dot de  2003-08-11 13:12 -------
Use of uninitialized variables leads to undefined behaviour, which is what you 
are observing.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/11882] Uninitialised bool variables do not work properly
  2003-08-11 13:03 [Bug c++/11882] New: Uninitialised bool variables do not work properly rescuer at polettix dot it
  2003-08-11 13:12 ` [Bug c++/11882] " schwab at suse dot de
@ 2003-08-11 15:28 ` bangerth at dealii dot org
  1 sibling, 0 replies; 3+ messages in thread
From: bangerth at dealii dot org @ 2003-08-11 15:28 UTC (permalink / raw)
  To: gcc-bugs

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



------- Additional Comments From bangerth at dealii dot org  2003-08-11 15:28 -------
And as for the bit-patterns: gcc assumes that bools have only one bit set or cleared, so 
flipping it is enough to change from true to false. On the other hand, an expression like 
 (tmp ? "true" : "false") 
if implicitly converted to 
  ((int)tmp !=0 ? ...) 
so all bits in tmp are considered. 
 
W.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-08-11 15:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-08-11 13:03 [Bug c++/11882] New: Uninitialised bool variables do not work properly rescuer at polettix dot it
2003-08-11 13:12 ` [Bug c++/11882] " schwab at suse dot de
2003-08-11 15:28 ` bangerth at dealii dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).