Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memory allocation of statically allocated arrays always sequential in terms of address value?

When we create an array on stack , say int arr[20]; , is it guaranteed that each cell of the array is 4 bytes apart in the memory ? Or it might happen that some of the cells are not allocated sequentially ?

like image 432
Ayush Avatar asked Oct 16 '25 05:10

Ayush


1 Answers

An array is guaranteed to be stored in contiguous memory. Pointer arithmetic is defined in such a way that

&arr[i] + 1 == &arr[i + 1]
like image 127
Igor Tandetnik Avatar answered Oct 17 '25 17:10

Igor Tandetnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!