How Do You Calculate the Memory Address of Temp[40,30,20] in a 3D Array?

In summary, the conversation discusses finding the memory location of a three-dimensional array named Temp, with each element storing 4 bytes. The array is setup in a specific way where the last index varies the fastest. To find the address of a specific element, the formula includes the base address, element size, column size, row size, and the indexes of the element. The conversation also includes a correction for the indexes starting at 1 instead of 0.
  • #1
acadian
4
0

Homework Statement



The problem is finding the memory location of a three-dimentional array Temp. Each index of temp goes from 1 to 100 and each element stores 4 bytes. The array is setup such that the last index varies fastest, aka:

Temp[1,1,1], Temp[1,1,2], ... Temp[1,1,100], Temp[1,2,1]
Temp[1,100,100], Temp[2,1,1], ... Temp[2,1,2], ... Temp[2,1,100], Temp[2,2,1], ... Temp[100,100,100]

Find the address of the array Temp[40,30,20]

Homework Equations



I know that For a three-dimensional array, the formula to compute the offset into memory is the following:

Address = BaseAddress + ((depthindex*col_size+colindex) * row_size + rowindex) * Element_Size

Attempted work

for the array Temp[40,30,20]

Address = BaseAddress + ((depthindex*col_size+colindex) * row_size + rowindex) * Element_Size

BaseAddress = m
Element_size = 4 bytes
Col_size = 100
Row_size = 100

Depthindex = 40
Colindex = 30
Rowindex = 20

Address = m + ( (40*100 + 30)*100 + 20 ) * 4 bytes
= m + ( (4030)*100 + 20 ) * 4 bytes
= m + (403020) * 4 bytes
= m + 1612080


Am I on the right track, here?

Thanks so much!
 
Physics news on Phys.org
  • #2
Looks ok to me.
 
  • #3
Except that you're placing your first element off your starting address:

Address Temp[1,1,1]
Address = BaseAddress + ((1*100+1)*100+1)*4 = BaseAddress + 40404

we expected it to be BaseAddress, right?

Hint: that equation would be corrected if the indexes started at 0; as they start at 1 you need to... for each index in the formula
 

Related to How Do You Calculate the Memory Address of Temp[40,30,20] in a 3D Array?

1. What are memory addresses of 3D arrays?

The memory address of a 3D array refers to the location of the first element in the array in the computer's memory. It is a unique identifier that allows the computer to access and manipulate the data stored in the array.

2. How are memory addresses of 3D arrays calculated?

The memory addresses of 3D arrays are calculated by taking into account the size of each dimension in the array and the data type of the elements. The computer uses a mathematical formula to determine the exact location of each element in the array.

3. Can the memory addresses of 3D arrays be changed?

No, the memory addresses of 3D arrays cannot be changed. They are assigned by the computer during the creation of the array and are fixed for the duration of the program. Any changes made to the array will be reflected in the same memory addresses.

4. How do memory addresses of 3D arrays affect performance?

The memory addresses of 3D arrays can impact performance as they determine how efficiently data can be accessed and manipulated. If the memory addresses are not optimized, it can lead to slower execution times and inefficient use of memory.

5. Are memory addresses of 3D arrays the same on different computers?

No, memory addresses of 3D arrays can vary on different computers and operating systems. This is because the computer's memory management system may assign addresses differently, and the size of the data type can also vary between systems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
8K
  • Programming and Computer Science
Replies
2
Views
8K
Back
Top