void func()
{
int arr[5]={1,2,3,4,5};
int variable = arr[1]; //variable will be set to 2
}
when this program is executed there will be contiguous memory equivalent of 5 integers is allocated in stack area of virtual address space as shown below for the array of 5 integers.
+---+
| | a[0]
+---+
| | a[1]
+---+
| | a[2]
+---+
| | a[3]
+---+
| | a[4]
+---+
Is there a space somewhere in the virtual address space (in code/data/stack/heap segment) allocated for the array variable arr
itself and not the array of 5 integers?
If not allocated, then-
Consider the code snippet - int variable = arr[1];
which will be converted by compiler as *(arr+1)
and will be present in code segment of the address space, which is read-only.
How does the arr
is evaluated to the starting address of the array arr
during every function call func
? Since each time the function is invoked the address of the call-stack could change, hence the address for the array arr
also changes.
So without having separate space for the array variable arr
how can this be achieved?