This is the mail archive of the gcc-bugs@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: diagnostic request


On Wed, Feb 06, 2002 at 11:18:54AM -0000, phr-2002@nightsong.com wrote:
>     int a[3] = {1, 2, 3};
>     test() { a[a[0]]=1; }
> 
> looks like it sets a[1] to 1.  However, according to some intricate
> reading of the ANSI C standard, its behavior is apparently undefined--

I don't see what is undefined about this particular example.

Perhaps you mean:

for(int i = 1; i <= 2; ++i)
  a[a[0] - 1] = i + 1;

This would become

for(int i = 1; i <= 2; ++i)
{
  int a0 = a[0];
  a[a0 - 1] = i + 1;		// Garanteed/assumed not to change a[0] (which is the 'problem').
}

and thus become:

int a0 = a[0];
for(int i = 1; i <= 2; ++i)
  a[a0 - 1] = i + 1;

[ 

  and finally

  int a0 = a[0];
  a[a0 - 1] = 3;

  which is equivalent to

  a[0] = 3;

  instead of the 'expected'

  a[0] = 2;
  a[1] = 3;

]

-- 
Carlo Wood <carlo@alinoe.com>


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