Table of contents
1.
Introduction
2.
Accessing an array out of bounds
3.
Access non-allocated location of memory
3.1.
Code
3.2.
 Output
4.
Segmentation fault
4.1.
Code
4.2.
Output
5.
Frequently Asked Questions
5.1.
What does undefined behavior signify?
5.2.
What is the ISO standard?
5.3.
What is garbage value in c?
5.4.
What is segmentation error?
5.5.
How does c++ handle the problem of Accessing an array out of bounds?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Accessing an array out of bounds in C/C++

Author Apoorv
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

What does this term out of bound means in array?

The array index out of bounds error is a special case of the buffer overflow error. It occurs when the index used to address array items exceeds the allowed value. It's the area outside the array bounds which is being addressed, that's why this situation is considered a case of undefined behavior. 

Now all of us who are familiar with Java must be thinking in java we do not have to handle such things. There are functions in high-level languages like Java that restrict you from accessing an array that is out of bounds by throwing an exception like java.lang. ArrayIndexOutOfBoundsException. However, there is no comparable functionality in C that deals with Accessing an array out of bounds, thus the programmer must deal with the circumstance on their own.

In this blog, we will discuss the consequences of Accessing an array out of bounds.

Also Read About, Sum of Digits in C, C Static Function

Accessing an array out of bounds

C makes no provisions for dealing with the problem of accessing an invalid index. It's referred to as Undefined Behavior in the ISO C standard.

For the program's current state, an undefined behavior (UB) is the result of running computer code whose behavior is not required by the language specification to which the code can comply (e.g. memory). This usually arises when the source code translator makes assumptions that aren't met during execution.

Let's see all the problems which can occur during the phenomenon of Accessing an array out of bounds:
You can also read about the memory hierarchy, And Tribonacci Series

Access non-allocated location of memory

In this scenario, if we are accessing an array out of bounds, the pointer will print the garbage value stored there. The program may access some piece of memory that is already owned by it.

Code

// Accessing an array out of bounds 
#include <stdio.h>

int main()
{
    int arr[] = {34,56,66,78};

    // Index inside the bound
    printf("arr [0] is %d\n", arr[0]);
     
    // arr[12] is out of bound or 12th index is out of bound
    printf("arr[12] is %d\n", arr[12]);
   
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 Output

arr [0] is 34
arr[12] is 2097152


So you can see when we are trying to access the index out of the bound, in that case, it is printing the garbage value. This memory location contains a garbage value.

You can also read about the dynamic arrays in c and  Short int in C Programming

Segmentation fault

In this scenario, the program may access memory that it does not own, resulting in program crashes such as segmentation faults.

Code

// aAccessing an array out of bounds
#include <stdio.h>
int main()
{
    int arr[] = {11,12,13,14,15};
    printf("arr [0] is %d\n",arr[0]);
     
    // Allocating memory to the index out of bound
    arr[11] = 56;
    printf("arr[11] is %d\n",arr[11]);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

run: line 1:     3 Segmentation fault      (core dumped) ./a.out


You can also read about dynamic array in c.

Frequently Asked Questions

What does undefined behavior signify?

Undefined behavior (UB) in computer programming is the effect of running a program whose behavior is specified as uncertain in the language specification to which the computer code adheres.

What is the ISO standard?

Experts from throughout the world agree on ISO standards. Consider them a formula for describing the optimal approach to do anything. Standards involve a wide range of activities, including the creation of products, the management of processes, the delivery of services, and the provision of resources.

What is garbage value in c?

When you allocate a variable, any memory for that variable is reserved. In some programming languages, such as C, if a variable is assigned but not allocated, it is said to have a garbage value, which means that certain data is kept by a random set of the computer's storage.

What is segmentation error?

A request for a page that the process does not have listed in its descriptor table, or an erroneous request for a page that it does have listed, causes a segmentation fault (e.g., a written request on a read-only page). A dangling pointer is one that may or may not point to a valid page but always points to an "unexpected" memory segment.

How does c++ handle the problem of Accessing an array out of bounds?

The std::vector class template in C++, on the other hand, does not require bounds checking. The std::at() member function can also do bounds-checking on a vector.

Conclusion

In this article, we have extensively discussed the theory and consequences of Accessing an array out of bounds. 

After reading about the Accessing an array out of bounds, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see arraysExceptionsMulti-Dimensional Arrays, and Single-Dimensional Arrays.

If you want to learn more about such hidden number theory algorithms and want to practice some quality questions which require you to excel your preparation s a notch higher, then you can visit our Guided Path for these number theory algorithms on  Coding Ninjas Studio.To be more confident in data structures and algorithms, try out our DS and Algo Course. Until then, All the best for your future endeavors, and Keep Coding.

Recommended Readings:

 

Live masterclass