Bug in gcc: assignment to non-lvalue

2007-09-20 Thread Michiel de Bondt
Hello all,

I think the gcc compiler should not accept assignments to non-lvalues.
gcc does this however, but does not execute the assignment (probably the
assignment is executed on a copy, but that copy is entirely floating).

Here is my code:

#include 
#include 
#include 

template class SuperArray
{
  T **block;
  int size;

public: 
  SuperArray() { block = (T **) calloc (32768, sizeof(T *)); if (!block)
exit (1); size = 0; }
  void Wipe() { int k; for (k=0; k<=size-1>>16; k++) free (block[k]);
size = 0; }
  ~SuperArray() { int k; for (k=0; k<=size-1>>16; k++) free (block[k]);
free (block); }
 
  T /*&*/operator[] (int index)
  {
if (index < 0) exit (1); // range check for debugging
if (index < size) return block[index>>16][index&65535];
if (index > size) exit (1); // range check for debugging
// size must increase
int k;
for (k=(size-1>>16)+1; k<=(index>>16); k++)
{
  block[k] = (T *) calloc (65536, sizeof (T));
  if (!block[k]) exit (1);
}
size = index + 1;
return block[index>>16][index&65535];
  }
  T *operator+ (int index)
  {
if (index < 0) exit (1); // range check for debugging
if (index < size) return block[index>>16] + (index&65535);
if (index > size) exit (1); // range check for debugging
// size must increase
int k;
for (k=(size-1>>16)+1; k<=(index>>16); k++)
{
  block[k] = (T *) calloc (65536, sizeof (T));
  if (!block[k]) exit (1);
}
size = index + 1;
return block[index>>16] + (index&65535);
  } 
};

struct string100 { char value[100]; };

main ()
{
  SuperArray a;
  strcpy ((a + 0)->value, "No assignment.");
  strcpy (a[0].value, "Non-lvalue assignment."); // illegal
  printf ("%s\n", (a + 0)->value);
} 





Re: Bug in gcc: assignment to non-lvalue

2007-09-20 Thread Michiel de Bondt
Using strings to show my point was not a good idea. You can add a field 
"int number" to the struct and perform similar operations (with = 
instead of strcpy).


But even with strings, gcc should give an error like: "strcpy(const 
char*, const char*) does not exists". In case of a "typedef char 
string100[100]", gcc behaves correctly by ringing alarm bells. So it 
seems to be the . operator that behaves incorrectly.


If you replace /*&*/ by & in the SuperArray class, the program becomes 
correct (in my opinion), but also the effect of the program becomes 
different.


Regards, Michiel


Andrew Pinski schreef:

On 9/20/07, Michiel de Bondt <[EMAIL PROTECTED]> wrote:
  

struct string100 { char value[100]; };



  

  strcpy (a[0].value, "Non-lvalue assignment."); // illegal



So you basically have:
a.operator[](0).value

Where value ia an array, I cannot remember the specific rules here but
value decays to &value[0] which is considered a rvalue but value[0] is
a lvalue even if a.operator[](0) is an rvalue.
So I think GCC is correct in accepting this code.

Thanks,
Andrew Pinski