Skip to content

Instantly share code, notes, and snippets.

@Costava
Created March 26, 2021 00:24
Show Gist options
  • Save Costava/721381fb2da906d99268d779712059b6 to your computer and use it in GitHub Desktop.
Save Costava/721381fb2da906d99268d779712059b6 to your computer and use it in GitHub Desktop.
// Example compilation:
// gcc index_out_of_bounds.c -std=c99 -Wall -Wextra
//
// Compiles with ZERO warnings/errors on gcc 10.2.0
//
// Adding -O2 or above causes 2 warnings
// for the printf line (this is by design of gcc, not a bug):
// array subscript -80 is below array bounds of ‘const int[5]’ [-Warray-bounds]
// array subscript 80 is above array bounds of ‘const int[5]’ [-Warray-bounds]
//
// -Warray-bounds is enabled by -Wall
//
// Info on gcc warnings:
// https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
#include <stdio.h>
int main(void) {
const int pos_foo = 80;
const int neg_foo = -80;
const int numbers[] = {575757, 111, 222, 333, 444};
printf("pos: %d neg: %d\n", numbers[pos_foo], numbers[neg_foo]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment