From the course: Secure Coding in C

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Accessing elements beyond the array size

Accessing elements beyond the array size - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Accessing elements beyond the array size

- [Instructor] It's entirely possible in C to reference elements outside of an array's range. In this code, the array at line five has three elements. Line seven attempts to output the fourth element. I'll build the code, and your compiler may catch an array bounds warning, but it's only a warning. This compiler didn't catch it. Let's see if this code actually runs, and it does. The code runs, outputting whatever garbage is in memory beyond the final array element. Now, I don't think any other current programming language lets you get away with this level of abuse. In this example, the array element to access is set by variable E at line six. It's way out of bounds. Let's build the code, and see if the error is caught, and it's not, and let's see if the code even runs. And it does, element 53 is accessed. This is amazing, because sometimes a program like this, this egregious, will crash. One solution shown in this improved…

Contents