[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Question of casts, lvalues, and & operator

I've found that G++ 3.2 has a problem optimizing this code.

#include <stdio.h>

int func_b (void** ppv)
  *ppv = (void*) 2;
  return 0;

char* test (void)
  char* pa = NULL;
  func_b (&(void*)pa);
  return pa;

int main (int, char**)
  char* p = NULL;
  p = test ();
  printf ("%p\n", p);

  return 0;

When compiling with -O0, everything is OK.  -Os, on the other hand,
will optimize the return and always yield NULL.

I sent a preprocessed version of the errant file to gcc-bugs and was
told that my syntax is faulty and that the compiler will issue

Here's his comment:

 (Your code should get a warning, or perhaps even a hard error -
  you're applying the & operator to a cast to non-reference type, which
  is not an lvalue, so it's invalid.)

Yet, -Wall does not make the compiler complain.  

He says that the line ought to be

  f ((void**)&p);

which does generate correct code.  Still, it seems to me that either
the compiler ought to warn or error, *or* the original code ought to

So, is this simply an example of the compiler failing to warn?

Reply to: