pointers - Basic casting question (C#) -
pointers - Basic casting question (C#) -
suppose have byte[] array, wondering doing when create pointer array , cast int. when dereference said int pointer, larger number in case bellow: (code may have errors)
byte[] bytes = new byte[100]; bytes[0] = 1; bytes[1] = 2; bytes[2] = 3; bytes[3] = 4; fixed (byte* pmem = &bytes[0]) { debug.writeline("{0:x16}", (int)pmem); byte* pmemplus1 = pmem + 1; debug.writeline("{0:x8}", *pmemplus1); byte* pmemplus2 = pmem + 2; debug.writeline("{0:x16}", *pmemplus2); int* parraybase = (int*) pmem; debug.writeline("{0:x8}", *parraybase); int* parraybaseplus1 = parraybase+1; debug.writeline("{0:x8}", *parraybaseplus1; } right expected, pmem, pmemplus1 , pmemplus2 dereference 1,2 , 3. (int)pmem take value of pointer (memory address).
when cast int pointer though, parraybase gives 4030201 , parraybaseplus1 gives number 4. latter makes sense because int 4 bytes long, right. don't understand. result when dereference int-cast array pointer (parraybase). explanations? might not understand concept of casting properly.
your memory layed out following:
01 02 03 04 00 00 00 00 parraybase pointing four-byte integer @ array's base of operations address. since integers stored in little endian format, results in value 0x04030201. interesting result when dereference parraybaseplus1. parraybaseplus1 pointing 4 bytes next first four. unless you've left out code, looks *parraybaseplus1 should 0.
c# pointers casting
Comments
Post a Comment