[comp.lang.c] Is this a bug?

rob@raksha.eng.ohio-state.edu (Rob Carriere) (08/31/90)

I have found what I suspect is a bug in the C compiler I am using, but I would
like confirmation before I start bad-mouthing.   :-)

I have code that looks like this:

typedef union ut { T1 e1; T2 e2 } ut;
void f( int const p1, ut const p2 );
void foo( void )
{
   int i;   ut u;
   ...
   f( i, u );      /* ERROR: typemismatch in parameter p2 in function f */
   ...
}

Calling it like so _does_ work

   f( i, *(ut const *)&u );

I cannot find anything in The Fine Manual that says I have to do acrobatics
like this.  Do I need to take reading 050, or does the compiler need glasses?
:-) 

The compiler claims ANSI C conformance.

SR
---

sarima@tdatirv.UUCP (Stanley Friesen) (09/04/90)

In article <5690@quanta.eng.ohio-state.edu> rob@raksha.eng.ohio-state.edu (Rob Carriere) writes:
>I have found what I suspect is a bug in the C compiler I am using, but I would
>like confirmation before I start bad-mouthing.   :-)
 
>typedef union ut { T1 e1; T2 e2 } ut;
>void f( int const p1, ut const p2 );
>void foo( void )
>{
>   int i;   ut u;
>   ...
>   f( i, u );      /* ERROR: typemismatch in parameter p2 in function f */
>   ...
>}
 
>Calling it like so _does_ work
 
>   f( i, *(ut const *)&u );
 
Hmm , it certainly looks rather odd.  If the compiler is not broken, it is at
least flaky - it appears to be treating unions differently than ints.  This
is, at the very least, inconsistant.

I would like to say that it is also wrong, the 'const' stuff in the prototype
can have no effect in this case because parameters are passed by value, and
are thus copied.  Thus it seems that this 'outer' const should be ignored.
(If p1 and p2 were pointers this would be different).

Unfortunately I cannot find anyplace in the standard which actually specifies
my point of view.  I though I had seen such a passage, but I cannot find it.
(Perhaps it was present in a prior draft and deleted in the final)

Daniel_Roedding@fiction.ms.sub.org (09/09/90)

rob@raksha.eng.ohio-state.edu (Rob Carriere) writes:

> I have code that looks like this:

> typedef union ut { T1 e1; T2 e2 } ut;
                ^^                  ^^
                ||                  ||
                Ouch - this is not what you'd call elegant, is it??

> void f( int const p1, ut const p2 );
> void foo( void )
> {
>    int i;   ut u;
>    ...
>    f( i, u );      /* ERROR: typemismatch in parameter p2 in function f */
>    ...
> }

> Calling it like so _does_ work

>    f( i, *(ut const *)&u );

It really seems to be a bug. But if you omit the "const" modifier, you'll
have less trouble. I think many compilers don't know how to handle "const"
correctly.

BTW: Passing structures per value?? Have a stack size > 1 Meg?? :-)

Daniel

---
..!uunet!mcsun!unido!mcshh!veeble!fiction!daniel; daniel@fiction.ms.sub.org

if (*++*argv!='=')
   (s->out=(void (*)(int,int,char *,...))outvstr)(s->id,s->spec,s->fmt);

I *love* C... :-)

kym@bingvaxu.cc.binghamton.edu (R. Kym Horsell) (09/13/90)

In article <404594@fiction> Daniel_Roedding@fiction.ms.sub.org writes:
\\\
>BTW: Passing structures per value?? Have a stack size > 1 Meg?? :-)
\\\

Why not? I seem to remember quite a lot of graphics work that's
using the idea (bitblt?).

Passing struct stuff around on the stack isn't bad per se -- you just
have to balance the size of such stuff vs the number of indirect
refs you save by having it local.

-Kym Horsell