Thread: Declaring an array and then accessing elements that should be out of bounds.

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    17

    Declaring an array and then accessing elements that should be out of bounds.

    Hi everyone,

    I have the following lines of code in front of me:

    Code:
    unsigned char (*image)[x_resolution][3];
    image = malloc(x_resolution * y_resolution * sizeof(char[3]));
    Where x_resolution and y_resolution are both 1000.
    I noticed it is then possible to do something like this:

    Code:
    memcpy(image[0][5], "\0\0\0", 3);
    What bugs me is that the array was declared as a two dimensional array with 1000 x 3 elements and yet we can can access elements that are seemingly out of bounds without trouble. I also understand that technically we're still within the bounds of the allocated memory, but still does this style make sense?

    Thanks,
    Omar

  2. #2
    Registered User
    Join Date
    Apr 2018
    Posts
    10
    C doesn't check array boundaries and leaves it up to you, the following code will be compiled without a warning even if it's undefined behaviour.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int a[2] = {1,2};
        size_t i;
        for(i = 0; i < 10; i++)
            printf("%d\n", a[i]);
    
        return EXIT_SUCCESS;
    }

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    17
    I'm assuming this isn't good C-style though right? It just seems counter intuitive that you're forced to declare how many elements your array is gonna contain only for the compiler to let you go beyond that boundary later on.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    You're not forced to declare the size if you're using an initializer (like what SillyStudent does in line 7 ). The compiler can infer the size of the array automagically.
    The main argument that C has against bound checking is performance. The thinking behind it is "The programmer knows exactly what he wants, and he can check for out-of-bounds by himself if he so wishes".
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing elements in an array of pointers to arrays
    By fxtdr79 in forum C Programming
    Replies: 5
    Last Post: 06-05-2010, 09:29 PM
  2. Accessing elements in a list
    By jk1998 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 03:58 AM
  3. accessing user entered array elements by index?
    By richdb in forum C Programming
    Replies: 10
    Last Post: 04-08-2006, 11:10 AM
  4. Accessing elements in a struct
    By supaben34 in forum C++ Programming
    Replies: 8
    Last Post: 10-03-2004, 09:55 AM
  5. declaring an array of n elements based on input
    By strobe9 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 04:37 PM

Tags for this Thread