public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/14958] New: incorrect compile of ?:, specifically ptr = flag? class_obj: 0;
@ 2004-04-14 18:54 gsmith at alumni dot uwaterloo dot ca
  2004-04-14 19:13 ` [Bug c++/14958] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: gsmith at alumni dot uwaterloo dot ca @ 2004-04-14 18:54 UTC (permalink / raw)
  To: gcc-bugs

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


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

* [Bug c++/14958] incorrect compile of ?:, specifically ptr = flag? class_obj: 0;
  2004-04-14 18:54 [Bug c++/14958] New: incorrect compile of ?:, specifically ptr = flag? class_obj: 0; gsmith at alumni dot uwaterloo dot ca
@ 2004-04-14 19:13 ` pinskia at gcc dot gnu dot org
  2004-04-14 19:28 ` bangerth at dealii dot org
  2004-04-14 19:46 ` gsmith at alumni dot uwaterloo dot ca
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-04-14 19:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-04-14 18:35 -------
No this is invalid as it is really doing the following:

(short*) (flag? typeof(foo)(foo) : typeof(foo) (0))

so that it is creating a tempary variable to store the tmparray<T> array.
you need to explicantly do the follow to get the behavior you want:
short * foop = flag? (short*)foo : (short*)0;

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


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


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

* [Bug c++/14958] incorrect compile of ?:, specifically ptr = flag? class_obj: 0;
  2004-04-14 18:54 [Bug c++/14958] New: incorrect compile of ?:, specifically ptr = flag? class_obj: 0; gsmith at alumni dot uwaterloo dot ca
  2004-04-14 19:13 ` [Bug c++/14958] " pinskia at gcc dot gnu dot org
@ 2004-04-14 19:28 ` bangerth at dealii dot org
  2004-04-14 19:46 ` gsmith at alumni dot uwaterloo dot ca
  2 siblings, 0 replies; 4+ messages in thread
From: bangerth at dealii dot org @ 2004-04-14 19:28 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-04-14 18:54 -------
Indeed. First, the result 
type of any expression is independent of the type of the variable 
it is assigned to, so the conversion you proposed can't happen. 
Second, the type of one of the 2nd/3rd parameters to the ?: operator 
is converted to the type of the other one if possible. In your case, 
  flag? foo : 0 
is equivalent to 
  flag? foo : (foo)0 
with foo=tmparray<short>. 
 
W. 

-- 


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


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

* [Bug c++/14958] incorrect compile of ?:, specifically ptr = flag? class_obj: 0;
  2004-04-14 18:54 [Bug c++/14958] New: incorrect compile of ?:, specifically ptr = flag? class_obj: 0; gsmith at alumni dot uwaterloo dot ca
  2004-04-14 19:13 ` [Bug c++/14958] " pinskia at gcc dot gnu dot org
  2004-04-14 19:28 ` bangerth at dealii dot org
@ 2004-04-14 19:46 ` gsmith at alumni dot uwaterloo dot ca
  2 siblings, 0 replies; 4+ messages in thread
From: gsmith at alumni dot uwaterloo dot ca @ 2004-04-14 19:46 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gsmith at alumni dot uwaterloo dot ca  2004-04-14 19:28 -------
Thank you! I made the constructor 'explicit' and now I get an error
on that line, which is much better. I couldn't see any way that this
could be valid *and* produce these particular results, sorry to
clutter up the buglist.
This explains why both of these work:

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




-- 


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


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

end of thread, other threads:[~2004-04-14 19:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-14 18:54 [Bug c++/14958] New: incorrect compile of ?:, specifically ptr = flag? class_obj: 0; gsmith at alumni dot uwaterloo dot ca
2004-04-14 19:13 ` [Bug c++/14958] " pinskia at gcc dot gnu dot org
2004-04-14 19:28 ` bangerth at dealii dot org
2004-04-14 19:46 ` gsmith at alumni dot uwaterloo dot ca

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).