Thread: Can assign out of bounds in an array

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    1

    Question Can assign out of bounds in an array

    Code:
    int myArray[3] = {0};    
    myArray[111] = 22;
    printf("\n%d\n", myArray[111]);
    The code above is not giving any errors or warnings when I compile and run it in CodeBlocks 20.03.

    The code echos the value of "22" to the command line.

    Should I be concerned that running code like this could overwrite memory it shouldn't?

    Do you know why this code is not presenting any errors or warning?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    You need to enable warnings, this warns at the online compiler:
    -Wall -Wextra -Wpedantic -Werror -O2 -fsanitize=address

    Output:
    Code:
    main.c:6:12: error: array subscript 111 is above array bounds of ‘int[3]’ [-Werror=array-bounds]
        6 |     myArray[111] = 22;
          |     ~~~~~~~^~~~~
    main.c:5:9: note: while referencing ‘myArray’
        5 |     int myArray[3] = {0};
          |         ^~~~~~~
    cc1: all warnings being treated as errors

    You might want to read this: Catching silly mistakes with GCC
    Last edited by thmm; 12-22-2021 at 05:01 AM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Should I be concerned that running code like this could overwrite memory it shouldn't?
    Defenitily YES!

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by Fergal View Post
    Code:
    int myArray[3] = {0};    
    myArray[111] = 22;
    printf("\n%d\n", myArray[111]);
    The code above is not giving any errors or warnings when I compile and run it in CodeBlocks 20.03.

    The code echos the value of "22" to the command line.

    Should I be concerned that running code like this could overwrite memory it shouldn't?

    Do you know why this code is not presenting any errors or warning?

    Thanks
    Yes, you should get an error if you use "-fsanitize=address -O2 -Wall" with GCC. However, keep in mind that the compiler cannot check for values that contain non constant/literal values. This is why it will always be your responsibility to check if a value is out of index. For that reason, always keep track of the length of the array in a variable.

    Another way you could get it is by dividing the total size of the array with the size of an element. For example your array stores integers (4 bytes). They are 3 of them so the size is 4*3 = 12 bytes for the whole array. So to get its length, you divide that by the value of an element and you get 3 (12 / 4 = 3)! In case you still don't understand or I'm not a good teacher, here is the code:

    Code:
    int myArray[3] = {0};
    printf("The length of my array is (%lu)\n", sizeof(myArray) / sizeof(myArray[0] ));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. array bounds overflow
    By BendingUnit in forum C Programming
    Replies: 3
    Last Post: 06-18-2006, 10:45 PM
  3. array bounds checking
    By anykey in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2005, 04:13 PM
  4. checking array bounds
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2002, 02:29 AM

Tags for this Thread