In C/C++, you're guaranteed that fields are stored in memory in the
order they're declared. For example, (assuming x86) in
class foo {
int x;
double y;
int z;
};
the memory layout will be x, then y, then z. But as you may be aware,
there may be alignment issues. Ignoring vtable issues and the like, x is
at offset 0, but y probably isn't at offset 4, but at offset 8 (and z at
offset 16). Some CPU architectures perform better if data is aligned on
"natural boundaries" (and some will actually abort if the data isn't
aligned). So doubles are preferentially stored at addresses that are a
multiple of 8, and there would be 4 bytes of padding (uninitialized,
ignored data) between x and y. You must (again, talking just C/C++) use
some extra-language feature (usually #pragma) to adjust (e.g. eliminate)
these pad bytes).
C# addresses this issue by ***not*** guaranteeing that data is stored in
the order you declare. In the above case, it might store data in memory
in the order y, x, z. Or even y, z, x. Or x, z, y (or z, x, y). All of
which satisfy alignment rules, and save 4 bytes of padding per instance.
To get around this, prefix your class with
[StructLayout(LayoutKind.Sequential)]
class foo ...
You'll also need a
using System.Runtime.InteropServices;
for the StructLayoutAttribute et al.
HTH
P.S. Side note: Without StructLayout, the alignments are *not*
determined at compile time, since the compiler doesn't know if this will
be run on a 32-bit architecture, 64-bit, different CPU families (x86,
PowerPC, etc), all of which may have different alignment rules.
moni wrote:
The data here is being written into a memory mapped file by a service
that is running on the machine. My GUI program is reading the data from
the Memory mapped file.
I dont think it can be an endian problem because I have managed to read
all other string values, short int values correctly .
Am I reading the structure value correctly when I do,
TextBox6.Text = BitConverter.ToUInt32(buffer1,0).ToString();
where,
Marshal.Copy( new IntPtr( pBuffer.ToInt32()+ 71), buffer1, 0, 4 );
is copying 4 btes of data onto buffer1 from the ptr pointed to the MMF.
Thanks.
Jim H wrote:
>Could there be an endian problem? Is the data coming from another machine
or was the data put in network byte order before being sent?
jim
"moni" <mo*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googleg roups.com...
>>>
Marshal.Copy( new IntPtr( pBuffer.ToInt32()+ 71), buffer1, 0, 4 );
I am copying 4 bytes which are part of my structure into buffer1 which
is a byte array.
Here pBuffer is a ptr to the Memory Mapped File I have.
In order to read from this array I am doing this:
TextBox6.Text = BitConverter.ToUInt32(buffer1,0).ToString();
But it is giving me garbage values.
Any clue?
Thanks
Michael Bray wrote:
"moni" <mo*******@gmail.comwrote in news:1155145116.321730.52460
@b28g2000cwb.googlegroups.com:
How do I get the structure value to display.
>
>
You have to read each value of the structure in order it is stored in the
array. If you are the one writing the byte array, then you could also
look
into serializing / deserializing the structure.
-mdb