[openbeos] Re: possible gcc bug ?

  • From: Korli <korli@xxxxxxxx>
  • To: openbeos@xxxxxxxxxxxxx
  • Date: Mon, 27 Jun 2005 20:22:56 +0200

Hey Stefano,

ref = sDec; means you assign value of sDec to the reference of ref, in your 
case sInc.
a reference relationship can only be established once.

sDec is never changed, hence its zero value
sInc is assigned to zero value, and decremented each time.

Bye,
Jérôme

Stefano Ceccherini a écrit :

I found a weird behaviour of gcc. I don't know if it's normal, and I'd like someone more 
"in the know" than me to explain :)

This code:

#include <stdio.h>

static int sInc = 0;
static int sDec = 0;
static int sCounter = 0;

static void
do_stuff()
{
        int &ref = sInc;
        ref++;
        
        ref = sDec;
        ref--;
        
        sCounter++;
        
        printf("inc: %d (should be %d), dec: %d (should be %d)\n",
                sInc, sCounter, sDec, -sCounter);
}


int main() { for (int i = 0; i < 10; i++) do_stuff(); }

Results in this printout:
inc: -1 (should be 1), dec: 0 (should be -1)
inc: -1 (should be 2), dec: 0 (should be -2)
inc: -1 (should be 3), dec: 0 (should be -3)
inc: -1 (should be 4), dec: 0 (should be -4)
inc: -1 (should be 5), dec: 0 (should be -5)
inc: -1 (should be 6), dec: 0 (should be -6)
inc: -1 (should be 7), dec: 0 (should be -7)
inc: -1 (should be 8), dec: 0 (should be -8)
inc: -1 (should be 9), dec: 0 (should be -9)
inc: -1 (should be 10), dec: 0 (should be -10)

Am I dumb if I think it should be different or it's a bug in our gcc ?






Other related posts: