This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Why "global" variable can't be processed properly ?


robinhorde@yahoo.com.cn writes:

>   I have a question about "global" variable accessing when gcc compile
>   some code. See example below:

This question would be more appropriate for the mailing list
gcc-help@gcc.gnu.org.  Please take any follow-ups to that list.


> int GetNewValue( int iValue )
> {
>     //do something...
>
>     //i change the global variable intentionally.
>     iCurArrayIdx = 1;   
>
>     //assume the return is 1
>     return 1;
> }
>
> int main(int argc, char* argv[])
> {
>     aiArray[iCurArrayIdx] = GetNewValue( aiArray[iCurArrayIdx] );
>
>     printf( "aiArray[iCurArrayIdx] = %d\n", aiArray[iCurArrayIdx] );
>
>     return 0;
> }

The order of evaluation of the left hand side and the right hand side of
the assigment operator is unspecified in C.  Therefore, the behaviour of
this program is unspecified, because iCurArrayIdx could be retrieved
before or after GetNewValue is called, and different compilers are free
to implement this program differently.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]