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 arrays , Exceptions, Multi-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: