'C Hexadecimal Memory locations
Okay, I'm working on learning C and working with some memory locations.
Assume the sizeof(double) is 8
and we have a double dbl_array[5] with a memory location of 0x7fffffffe360.
What would the memory location be for &(dbl_array[4])?
When running locally I can see that the location goes from b34b0 to b34d0, but I'm stuck on how to apply this to the assumed location.
Any tips would be amazing!
Solution 1:[1]
Calculate the address in bytes as base + 4*sizeof(double). So 0x7fffffffe360 + 4*8.
Solution 2:[2]
What would the memory location be for
&(dbl_array[4])?
Without knowing the type of computer and/or operating system running this program, this question cannot be answered; it is only possible to say which address this element would have on 99% of all computers:
If X is an array of the data type Y and the address of the element X[n] is Z, the address of the element X[n+1] is Z+sizeof(Y).
For this reason, the address of the element X[n+M] is Z+M*sizeof(Y).
The address of an array is the address of element X[0].
Now simply take a calculator that can calculate hexadecimal numbers and perform the following calculation: 0x7fffffffe360 + 4 * 8
However, there are counterexamples where the address calculation is done differently: The "huge" memory layout on x86-16 for example...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lundin |
| Solution 2 | Martin Rosenau |
