The importance of showing ALL of the code in C

From: Sean 'Captain Napalm' Conner <spc_at_conman.org>
Date: Wed Feb 6 18:13:55 2002

It was thus said that the Great "Fred Cisin (XenoSoft)" once stated:
>
> an example:
>
> int A[10];
> int N;
> N = 3;
> A[10] = 2;
> printf("%d",N);
>
> In many other languages, A[10] = 2 would not have any effect on N.

  And it might not have an effect on N in C either. The C compiler is free
to organize local storage any way it sees fit. So for example:

        {
          int x[1];
          char c;
          int y;

          c = 'C';
          y = 1;
          x[2] = 0x40;

          /* ... */
        }

  The compiler is free to organize the layout of the local storage as
(assuming ints are four bytes, represented by [ ]):

        x --> [ ][ ][ ][ ]
        y --> [ ][ ][ ][ ]
        c --> [ ]

  or:

        x --> [ ][ ][ ][ ]
        c --> [ ] - - - ( three bytes of padding )
        y --> [ ][ ][ ][ ]

  or possibly:

        c --> [ ]
        y --> [ ][ ][ ][ ]
        x --> [ ][ ][ ][ ]

  depending on various things like alignment restrictions or variable usage
(heavily used variables might be pushed closer to the base of the frame
pointer (pointer to local storage) to take advantage of smaller offsets for
instance). Such bugs are typically very hard to find. Heck, the assignment
to x[2] may be optimized out entirely if the compiler sees that nothing is
done with the result so it might go unnoticed for a long time until the code
is modified.

  -spc (Been There, Done That, Have The Bug Report ... )
Received on Wed Feb 06 2002 - 18:13:55 GMT

This archive was generated by hypermail 2.3.0 : Fri Oct 10 2014 - 23:34:44 BST