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++/14958] New: incorrect compile of ?:, specifically ptr = flag? class_obj: 0;


I have a class tmparray<T> which converts to T*.

When I use

short * foop = flag? foo : 0;

... where foo is a tmparray<short>, I expected this
to act as

short * foop = flag? (short*)foo : (short*)0;

In fact, when flag is false, a non-zero value
is placed in foop which is different from what you
get when flag is true, but close to that.

If *either* of the casts are added, it works properly.
This seems to be independent of -ansi and -O, which
makes me wonder if there's some bizarre reason this
*should* actually happen, but it doesn't seem to make
a lot of sense. It also happens on 3.2

Example attached. It prints this for me
----------------------
address of foo = 0xbffff810, data at 0x8049998
foop = 0x80499b8
address of foo = 0xbffff810, data at 0x8049998
foop = 0x8049998
----------------------
I expected this (this is what you get when a cast is added)
----------------------
address of foo = 0xbffff810, data at 0x8049950
foop = (nil)
address of foo = 0xbffff810, data at 0x8049950
foop = 0x8049950
----------------------
Here is the code
======================

template <class T>
class tmparray {
 protected:
    T * m_p;
 public:
    tmparray( int n ){
        m_p = new T [n];
    }
    operator T* () const { return m_p; }
	void discard() {
		if( m_p ){
			delete [] m_p;
			m_p = 0;
		}
	}
	~tmparray() { discard(); }
};
#include <stdio.h>
	
//
// the ?: in the function below is not compiled
// correctly (I think). When the flag is false,
// the result stored into foop is non-zero and
// rather strange. When *either* expression alongside
// the ':' is cast to (short*), it works properly

void buggyfunc(bool flag)
{
	tmparray<short> foo(12);
	printf("address of foo = %p, data at %p\n", &foo, (short*)foo);
	short * foop = flag? foo : 0;
	printf("foop = %p\n", foop );
}


main()
{
	buggyfunc(false);		// foop should be (nil)!!
	buggyfunc(true);		// foop should print data address
}

-- 
           Summary: incorrect compile of ?:, specifically ptr = flag?
                    class_obj: 0;
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gsmith at alumni dot uwaterloo dot ca
                CC: gcc-bugs at gcc dot gnu dot org


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


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