Calculate memory address by pointer arithmetic

1

Suppose the elements of the v vector are of type int and each int occupies 8 bytes on your computer.

If the address of v[0] is 55000, what is the value of the expression v + 3 ?

    
asked by anonymous 14.04.2018 / 22:07

2 answers

4

I will follow your assumptions, although this does not actually occur.

As v is a pointer the arithmetic will be done on top of the elements it points to. The zero is always the beginning of the sequence (as is the case), and at the same time it is the first element of it. So if 0 is the first element, and of course, 1 is the second, we can conclude that 3 is the fourth element.

The statement says that the pointing element has 8 bytes, so each element moves 8 bytes.

The account should be v plus the element number times 8. Then the expression v + 3 will result in 55024 , which is the memory location that is the fourth element of the sequence. / p>

This applies to C and the basic use of C ++, but in the latter the semantics may be different according to the encoding of the vectorized type.

    
14.04.2018 / 22:22
1

The v + 3 will result in the memory location of v[3]

    
14.04.2018 / 22:11