[Bug c++/13322] New: operator associativity bug

supolj at cs dot felk dot cvut dot cz gcc-bugzilla@gcc.gnu.org
Fri Dec 5 20:40:00 GMT 2003


Operator = should have associativity from the right to the left. But it is not 
in gcc allways true ( comparison: in .NET C++ it works "ok" - described 
lower ). 

It seems that first is counted output address of assignment on the LEFT side, 
then it is counted expression on the right side of the assignment and then all 
the assignment by the associativity from the right. But first is counted 
expression on the LEFT side. 
Example of the part of my diploma thesis executable code is below.

Description of the code in the main function: I have a stack referenced stack[0]
=top, stack[1]=onebelow..., I need to put two integers on it ( 1 and 2 ) then I 
need to write out stack[top=0] + stack[onebelow=1], so it writes 3.It is ok. 
Then I need to pop these two integers and push the poped expression on the 
stack. Whenever I push 3, it is ok. Whenever I push the expression, the operator
() is called first, so the values on the stack are changed because *top of the 
stack is changed and operator[] is called after, stack *top value refers to 
unwanted address. In .NET C++ first is called twice operator[] and then operator
(). That should be the right way as far as I understand C standards and 
associativity. Thanks for sollution.
----------------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

using namespace std;

template<
   typename Type,
   int initialSize = 1000,
   int firstOverflowIncrement = 1000000,
   int nextOverflowMultiplicator = 2,
   int maxSize = 10000000
>
class SemanticStack 
{
protected:
   Type* stack;   
   Type* max;
   Type* top;

   int size;
   bool firstOverflow;

public:     

   SemanticStack() : size(initialSize), firstOverflow(true) {
      stack = (Type*)malloc(sizeof(Type) * size);
      max = stack + size - 1;
      top = stack - 1;
   }

   ~SemanticStack()         { free(stack); }

   Type peek()      { return *top; }
   bool isEmpty()    { return top < stack; }
   Type operator[](int pos) { return *(top-pos); }
   Type& operator()(int pos) { top -= pos; return *top; }
   Type& addOne() { if ( top == max ) enlarge(); return *(++top); } 

protected:
   void enlarge()   {
      if (firstOverflow) {
         size += firstOverflowIncrement;
         firstOverflow = false;
      }
      else {
         size *= nextOverflowMultiplicator;
      }

      if (size > maxSize) {
         //yyerror("stack overflow");
         exit(EXIT_FAILURE);
      }
      
      Type* old = stack;
      stack = (Type*)realloc(stack, sizeof(Type) * size);
      max = stack + size - 1;
      top = stack + (top - old);
   }

};

int main()
{
    SemanticStack<int> stack;
    stack.addOne() = 1;
    stack.addOne() = 2;
    cout << stack[0] + stack[1] << endl; //3,ok
    stack(1) = stack[0] + stack[1];      //but stack(1) = 3 is ok
    cout << stack[0];                    //3, or anything else
}
---------------------------------------------------------

-- 
           Summary: operator associativity bug
           Product: gcc
           Version: 3.2.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: supolj at cs dot felk dot cvut dot cz
                CC: gcc-bugs at gcc dot gnu dot org


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



More information about the Gcc-bugs mailing list