nains
nains

Reputation: 259

How to calculate where an indexed value in a 3d array will be in memory? How to calculate where an indexed value in a char** will be in memory?

The problem states: Given the following array declarations and indexed accesses, compute the address where the indexed value will be in memory. Assume the array starts at location 200 on a 64-bit computer.

a. double d[3][4][4]; d[1][2][3] is at: _________
b. char *n[10]; n[3] is at: _________

I know the answers are 416 and 224 (respectively), but I do not understand how those numbers were reached.

For part a, I was told the equation: address-in-3d-array= start-address + (p * numR * numC + (i * numC) + j) * size-of-type (where start address = 200, the numR and numC come from the original array, and the i,j, and p come from the location you are trying to find).

Nothing I do makes this equation come to 416. I have been viewing the order of the array as d[row][column][plane]. Is that incorrect? I have also tried looking at it as d[plane][row][column], but that didn't seem to work either.

For part b, I'm not sure where to start as I thought that as the array is an array of pointers, it's location would be in the heap. I'm not sure how to get 224 from that.

I need to answer these questions by hand, not using code.

Upvotes: 0

Views: 389

Answers (2)

p-a-o-l-o
p-a-o-l-o

Reputation: 10057

In words:

Given a generic array d[s1][s2][s3] of elements of size S, the offset of the d[x][y][z] element is

[(x * s2 * s3) + (y * s3) + z] * S

In the array double d[3][4][4], with S = sizeof(double) = 8, the location of d[1][2][3] is at offset:

[(1 * 4 * 4) + (2 * 4) + 3] * 8 = 216

Sum the offset (216) to the start (200) to get 416.

Given a generic array n[s1] of elements of size S, the offset of the n[x] element is

x * S

In the array char * n[10], with S = 8 (pointers size on 64bit platforms), the location of n[3] is at offset

3 * 8 = 24

Sum the offset (24) to the start (200) to get 224.

In code:

int main()
{
    double d[3][4][4];

    size_t start = 200;

    size_t offset =
          sizeof(d[0])       * 1
        + sizeof(d[0][0])    * 2
        + sizeof(d[0][0][0]) * 3;

    std::cout << start + offset << std::endl; //416 on my machine

    char * n[10];
    offset = 3 * sizeof(char*);

    std::cout << start + offset << std::endl; //224 on every 64bit platforms
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

For this array declaration

double d[3][4][4];

to calculate the address of the expression

d[1][2][3]

You can use the following formula

reinterpret_cast<double *>( d ) + 1 * 16 + 2 * 4 + 3

that is the same (relative to the value of the expression) as

reinterpret_cast<char *>( d ) + 27 * sizeof( double )

So you can calculate the address like the address of the first element of the array plus the expression 27 * sizeof( double ) where double is equal to 8.

For this array

char *n[10];

the address of the expression

n[3]

is

reinterpret_cast<char *>( n ) + 3 * sizeof( char * )

Upvotes: 1

Related Questions