Olba12
Olba12

Reputation: 335

Trying to understand what is wrong with the following code in C

I have a task to find why this code is wrong.

#include <stdlib.h>
#include <stdio.h>

#define fail(a)          ((test == 0 || test == a) ? fail##a() : 0)
#define N                (10)

int  a[N] = { 1 }; 
int* b = &a[0];


void fail1()

{
    printf("a[0] = %d\n", a[0]);
    printf("b[0] = %d\n", b[0]);
    printf("*b   = %d\n", *b);
    *b = 2;
    a[N] = 3;
    printf("*b = %d\n", *b);

}

...

int main(int argc, char **argv)
{
    int        test = 0;

    if (argc > 1) {
            sscanf(argv[1], "%d", &test);
            printf("doing test %d\n", test);
    } else
            puts("doing all tests");

    fail(1);
    fail(2);
    fail(3);
    fail(4);
    fail(5);
    fail(6);

    puts("lab6 reached the end");

    exit(0);
}

By using valgrind it tells me that there is a fail in printf("*b = %d\n", *b);. I have noticed that by commenting a[N] = 3; valvgrind gives no errors. But I do not understand why. I know that this has something to do with memory and I know that a[N] ask for element outside the array.

Upvotes: 1

Views: 163

Answers (3)

user1023602
user1023602

Reputation:

There is no array-bounds checking in C. a[N] is off the end of the array, which is a[0] .. a[N-1]

If you read or write outside the array bounds, you get a random result (what people call UB "undefined behaviour" these days). This could be

Reading x = a[outside];

  • The code crashes
  • You get a random value
  • You get the value you expect

Writing a[outside] = x;

  • The code crashes
  • The write happens, but when reading it back the value has changed.
  • The write happens, and reading it back is ok (ie. nothing appears to happen)
  • The write happens, but you get crashes in other parts of the code.

Example, assuming a[] is an array of char.

    a[0] | a[1] | ... | a[N-1] | s[0] | s[1] | s[2] | s[3] | s[4] | s[5]
    1    | 0    |     | 7      | 'H'  | 'e'  | 'l'  | 'l'  | '0'  | 0

Writing to a[N] does not change the array, it overwrites a different variable instead (in this case, part of a string). You corrupt the program's memory, causing chaos.

    a[N] = 255;                 /*bug*/

    a[0] | a[1] | ... | a[N-1] | s[0] | s[1] | s[2] | s[3] | s[4] | s[5]
    1    | 0    |     | 7      | 255  | 'e'  | 'l'  | 'l'  | '0'  | 0

In this example, reading and writing a[N] will appear to work, but it has corrupted another string variable (which used to say "Hello" but now doesnt). This may cause bugs later on, or perhaps noone will notice for years!

When programming in C, you need to be extremely careful and disciplined.

Upvotes: 2

Mayhem
Mayhem

Reputation: 507

Array indexing starts from 0, so the last index of a is N-1 not N. By writing to a[N] you are accessing memory not part of the array and thus causing undefined behavior.

Upvotes: 4

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

C arrays have 0-based indexing. For an array defined like a[N], the maximum valid element will be a[N-1].

a[N] points to out of bound memory. Trying to access out of bound memory, invokes undefined behavior.

Now, while you may be knowing the above fact, what you might have ignored in the effect of UB. Once you hit UB, nothing is guaranteed. Simple solution, don't write a code which can invoke UB.

Upvotes: 5

Related Questions