+ 1
Going past boundaries to next array in struct, undefined behavior?
Is the following undefined behavior? It reads past the array boundaries of the first array, into the second array in the struct. struct container { int arr_one[4]; int arr_two[4]; }; int main(void) { struct container ct; int i; container_init(&ct); // initialize arrays with some default values // read into the second array for (i = 0; i < 8; i++) { printf("%d\n", ct.arr_one[i]); } return 0; } The question I have is whether accessing the array past its boundaries will invoke undefined behavior? Or is it defined because the memory that follows is allocated on the stack? Full source here: https://code.sololearn.com/csetWYywaBMH
5 ответов
+ 1
Yes. It is undefined.
Since it allocates continuous memory locations, you are getting arr_two[] values by arr_one[] after exceeding index range.. Check this same code in any other compilers. You may not get same output.
hope it make sence....
+ 1
@Jayakrishna🇮🇳
You're right, i tried compiling the same source with godbolt.org's compiler and it did throw a warning message.
I guess i have to find a way to enable a warning for that kind of code unless it's a gcc 12.1 feature... which is 2 version numbers higher than what i have.
Anyway, thanks. 👍
0
@Jayakrishna🇮🇳
Yes, i wrapped them in a struct on purpose to see if i could access the second array from the first, knowing they would be laid out in memory next to each other. I thought i could get around the UB this way. 😬
maybe i could've done the same with two local arrays , but if i remember, the compiler can rearrange the order in which variables are pushed on the stack (for alignment and optimization reasons) so i wanted to be sure the next memory slot was from the second array and not some other variable.
it was mostly an experiment to see if it was possible, but with UB I guess it's proven to be a terrible idea.
Have a good day! 🤘
0
After playing around a little, using a pointer seem to hide the warning and output the expected values:
int *p = ct.arr_one;
for (i = 0; i < 8; i++)
printf("%d\n", *p++);
Now I'm not sure if using a pointer gets around the undefined behavior or if it's still there and the warning just disappeared.
0
It is actually because you are using a struct pointer which stores it's values in continues memory locations
Like
arr_one[0] say 1000 and int takes 4bytes
arr_one[1] say 1004
arr_one[2] say 1008
arr_one[3] say 1012
arr_two[0] say 1016
arr_two[1] say 1020
arr_two[2] say 1024
arr_two[0] say 1028
So by pointer ct. arr_one[i] is just ith location from starting. Even though, arr_two is defined or not defined, you can access out of bound by a pointer in c but you get garbage values..
Here you are getting garbage value only because it is set by other variables, not by itself.
A pointer increment just access next location, whether it is defined or undefined if memory available to program. That's why through arrays, c compiler don't generate array index out of bounds errors. If the memory is not available, or invalid by program then it generates "monitor core dumped " error.
Through pointer, you just accesing next, next,.. next locations. It won't check, whether next is set by struct or not. Is same by struct ct.