This is a for loop in c++. I dont understand why it does not give a segmentation fault on execution.
int main()
{
int arr[5];
for (int x = 0; x <= 5; x++)
{
arr[x] = x;
}
return 0;
}
This is a for loop in c++. I dont understand why it does not give a segmentation fault on execution.
int main()
{
int arr[5];
for (int x = 0; x <= 5; x++)
{
arr[x] = x;
}
return 0;
}
This is undefined behavior. Undefined behavior means that anything can happen, including:
To get a bit more formal, this is how the C++11 Standard defines undefined behavior:
behavior for which this International Standard imposes no requirements [ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. —end note ]
Concerning the reason why doing x[5]
is indeed undefined behavior, that's because x[5]
is equivalent to *(x + 5)
(see paragraph 8.3.4/6), and paragraph 5.3.1/1 about the unary operator *
specifies:
The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.” [...]
But since x + 5
does not point to any object, and the above paragraph does not specify what the result of dereferencing such a pointer should be, the previously quoted sentence applies:
[...] Undefined behavior may be expected when this International Standard omits any explicit definition of behavior [...]
Which means that x[5]
is undefined behavior.
A segmentation fault occurs when a user program attempts to do one of :
So you are correct in realizing that you are reaching out of bounds in your array, and on the last loop iteration you are accessing something outside of your program's allocated memory. It just so happens that the piece of memory is not system memory and it exists, so it lets you read it.
If you ran this bit of code enough times you should eventually get a segmentation fault because it will happen to be placed right up against system memory or at the end of memory.
I think Andy Prowl has already answered best in saying that it's undefined behaviour.
But if you're interested in the specifics of why it doesn't crash, on my compiler at least, the variable x
is allocated in the position on the stack immediately following the array. When you assign x
to arr[5]
, you're really just assigning x
back to itself.
Obviously this may be different from one compiler to the next. Just thought you might be interested to know at least what one particular compiler is doing.