Question

In an interview, I was asked:

You are given a stack with starting address of 0th. The value of the stack is 1000 and each location can store 8 bytes of data. What is the memory location of 42nd element ?

Était-ce utile?

La solution

Let's look at some values and try to find a pattern.

0 -> 1000
1 -> 1008
2 -> 1016

It starts at 1000 and goes up by 8 each time, so

n -> 1000 + 8*n

42 -> 1000 + 8*42
42 -> 1336

Autres conseils

It is quite simple.

  • Element[0] = Memory[1000]
  • sizeof(Element) = 8

Then:

Element[42] = Memory[1000 + 8*42]

@Time S.

The statement "it grows up" is not necessarily true. It can also grow down.

So you second example is more correct. N can be negative in that case.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top