tags:

views:

138

answers:

3

Suppose I have:

unsafe {
    byte* start = GetStartLocation();
    int something = start[4];
}

What is 'something'? The value of the memory address 4 bytes down from start?

+1  A: 

The value of something is the byte value of offset 4 from start widened to an int type.

It's equivalent to the following code

byte temp = start[4];
int something = temp;
JaredPar
+1  A: 

start[4] will evaluate to:

*(start + 4)
jeffamaphone
+5  A: 

Say start points to memory location 0x12345678, and memory looks like this:

  0x12345678   0x12
  0x12345679   0x34
  0x1234567a   0x56
  0x1234567b   0x78
  0x1234567c   0x9a
  0x1234567d   0xbc

then something equals 0x9a.

The fact that something has type int doesn't matter to how start[4] is interpreted - it gets the byte value of the byte 4 locations away from start.

RichieHindle
Thank you! I need to enter 15 characters to post this, which leads me to believe that 'thank you's in general are not allowed, which means this will be deleted shortly, so I hope you see this! Thanks!
Sam Pearson
@Sam: Personally, I think the more "thank you"s on SO the better. Feel free to pad them with smileys as necessary. 8-)
RichieHindle
There's always a way :)
Artem Russakovskii
Thank you
Artem Russakovskii