Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global Integer not changed from function

1 view
Skip to first unread message

HOLGER BUICK

unread,
Jun 7, 2003, 5:00:50 AM6/7/03
to
The following code, compiled with LCC, do -not- turn the
global variable dGlobalTest to 33, but it is counted to 1000 like
dFunctionTest;
Is this correct work from the compiler or is it a C-language problem?

int count(int d);
int dGlobalTest=0;
int dFunctionTest=0;

int count(d){
// d is not used
dGlobalTest=33;
dFunctionTest++;
return 1;
}

int main(int argc,char *argv[]){
for(int n=0;n<1000;n++){
dGlobalTest+=count(dGlobalTest);
}

printf("\r\n\r\n dGlobalTest = %d ,"
" dFunctionTest = %d \r\n\r\n",
dGlobalTest,dFunctionTest);

return 0;
}


John

unread,
Jun 7, 2003, 6:16:27 AM6/7/03
to

"HOLGER BUICK" <HOLGER...@freenet.de> wrote in message
news:3ee1a991$0$16599$9b62...@news.freenet.de...

> The following code, compiled with LCC, do -not- turn the
> global variable dGlobalTest to 33, but it is counted to 1000 like
> dFunctionTest;
> Is this correct work from the compiler or is it a C-language problem?

I reckon it has to be a C language issue. Not a problem as such, it's called
undefined behaviour because you are referencing the same variable without an
intervening sequence point.

here

dGlobalTest+=count(dGlobalTest);

for example the following breaks this rule.

a[i] = i++;


John


John

unread,
Jun 7, 2003, 6:26:42 AM6/7/03
to

"HOLGER BUICK" <HOLGER...@freenet.de> wrote in message
news:3ee1a991$0$16599$9b62...@news.freenet.de...
> The following code, compiled with LCC, do -not- turn the
> global variable dGlobalTest to 33, but it is counted to 1000 like
> dFunctionTest;
> Is this correct work from the compiler or is it a C-language problem?

Here are some notes that might help.

==================
From a FAQ on C

A sequence point is a point in time (at the end of the
evaluation of a full expression, or at the ||, &&, ?:, or comma
operators, or just before a function call) at which the dust
has settled and all side effects are guaranteed to be complete.


The ANSI/ISO C Standard states that

Between the previous and next sequence point an
object shall have its stored value modified at
most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed
only to determine the value to be stored.
==================


John


0 new messages