Home > Software engineering >  why do I have a runtime #2 failure in C when I have enough space and there isn't many data in t
why do I have a runtime #2 failure in C when I have enough space and there isn't many data in t

Time:10-30

I'm writing this code in C for some offline games but when I run this code, it says "runtime failure #2" and "stack around the variable has corrupted". I searched the internet and saw some answers but I think there's nothing wrong with this.

#include <stdio.h>
int main(void) {
    int a[16];
    int player = 32;
    for (int i = 0; i < sizeof(a); i  ) {
        if (player 1  == i) {
            a[i] = 254;
        }
        else {
            a[i] = 32;
        }
    }
    printf("%d", a[15]);
    return 0;
}

CodePudding user response:

Your loop runs from 0 to sizeof(a), and sizeof(a) is the size in bytes of your array.

Each int is (typically) 4-bytes, and the total size of the array is 64-bytes. So variable i goes from 0 to 63.

But the valid indices of the array are only 0-15, because the array was declared [16].

The standard way to iterate over an array like this is:

#define count_of_array(x) (sizeof(x) / sizeof(*x))

for (int i = 0; i < count_of_array(a); i  ) { ... }

The count_of_array macro calculates the number of elements in the array by taking the total size of the array, and dividing by the size of one element.

In your example, it would be (64 / 4) == 16.

CodePudding user response:

sizeof(a) is not the size of a, but rather how many bytes a consumes.

a has 16 ints. The size of int depends on the implementation. A lot of C implementations make int has 4 bytes, but some implementations make int has 2 bytes. So sizeof(a) == 64 or sizeof(a) == 32. Either way, that's not what you want.

You define int a[16];, so the size of a is 16.

So, change your for loop into:

for (int i = 0; i < 16; i  )

CodePudding user response:

You're indexing too far off the size of the array, trying to touch parts of memory that doesn't belong to your program. sizeof(a) returns 64 (depending on C implementation, actually), which is the total amount of bytes your int array is taking up.

There are good reasons for trying not to statically declare the number of iterations in a loop when iterating over an array. For example, you might realloc memory (if you've declared the array using malloc) in order to grow or shrink the array, thus making it harder to keep track of the size of the array at any given point. Or maybe the size of the array depends on user input. Or something else altogether.

There's no good reason to avoid saying for (int i = 0; i < 16; i ) in this particular case, though. What I would do is declare const int foo = 16; and then use foo instead of any number, both in the array declaration and the for loop, so that if you ever need to change it, you only need to change it in one place. Else, if you really want to use sizeof() (maybe because one of the reasons above) you should divide the return value of sizeof(array) by the return value of sizeof(type of array). For example:

#include <stdio.h>

const int ARRAY_SIZE = 30;

int main(void)
{
    int a[ARRAY_SIZE];

    for(int i = 0; i < sizeof(a) / sizeof(int); i  )
        a[i] = 100;

    // I'd use for(int i = 0; i < ARRAY_SIZE; i  ) though
}
  •  Tags:  
  • c
  • Related