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

  • Thread starter Thread starter acadian
  • Start date Start date
  • Tags Tags
    3d Array Memory
Click For Summary
SUMMARY

The discussion focuses on calculating the memory address of a specific element in a three-dimensional array, Temp[40,30,20]. The formula used for the calculation is Address = BaseAddress + ((depthindex * col_size + colindex) * row_size + rowindex) * Element_Size. Given the parameters—BaseAddress as 'm', Element_Size as 4 bytes, Col_size as 100, and Row_size as 100—the calculated address for Temp[40,30,20] is m + 1612080. A correction is noted regarding the starting index, which should be adjusted since the array indexing begins at 1.

PREREQUISITES
  • Understanding of three-dimensional arrays in programming
  • Familiarity with memory addressing concepts
  • Knowledge of array indexing and its implications
  • Basic arithmetic operations for offset calculations
NEXT STEPS
  • Study memory allocation techniques in C/C++ for multi-dimensional arrays
  • Learn about pointer arithmetic and its application in array manipulation
  • Explore the implications of different indexing starting points in array calculations
  • Investigate how to optimize memory usage for large data structures
USEFUL FOR

Students in computer science, software developers working with low-level memory management, and anyone interested in understanding the intricacies of multi-dimensional array handling in programming languages.

acadian
Messages
3
Reaction score
0

Homework Statement



The problem is finding the memory location of a three-dimensional 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
Looks ok to me.
 
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
12
Views
9K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K