[openbeos] Re: possible gcc bug ?

  • From: Michael Pfeiffer <michael.pfeiffer@xxxxxxxxx>
  • To: openbeos@xxxxxxxxxxxxx
  • Date: Mon, 27 Jun 2005 20:13:20 +0200

Hi Stefano

The output is correct. I guess you misinterpret the meaning of the statement ref = sDec.
This simply assigns the value from sDec (which is always 0) to sInc. Afterwards
ref-- decrements the value from sInc (= -1).


Regards,
Michael

Am 27.06.2005 um 19:33 schrieb Stefano Ceccherini:


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 ?




--------------------------------------------------------------- Scegli il tuo dominio preferito e attiva la tua email! Da oggi l'eMail di superEva e' ancora piu' veloce e ricca di funzioni! http://webmail.supereva.it/new/ ---------------------------------------------------------------






Other related posts: