Re: Convert from byte[] to structure value

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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" <mons.2110@xxxxxxxxx> wrote in message
news:1155149536.523423.157210@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


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" <mons.2110@xxxxxxxxx> wrote 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

.



Relevant Pages

  • Re: Prime Numbers
    ... /*This code finally compiles as a 'c' code ... One more question....if i don't free up the memory allocated at the end ... it would appear that we are best served by an array ... prime numbers can be represented by an unsigned long int. ...
    (comp.lang.c)
  • Re: Java Indexing- Historical question
    ... And when the array is of primitives there should be a dramatic benefit in memory usage as well. ... RestrictedRangeIntegerKeyedMapif you prefer) implements Mapthat only accepted keys in a given range, and stored its elements in an array. ... public ArrayMap(int base, int size) { ...
    (comp.lang.java.programmer)
  • Re: contiguity of arrays
    ... Dan.Pop@cern.ch (Dan Pop) writes: ... There is nothing magic about dynamically allocated memory. ... object and then used to access such an object or an array of such ... could disallow ptrbecause the relevant array of int is only 2 ...
    (comp.lang.c)
  • Re: memory and speed
    ... but only handles int to save resource/memory ... The size of the IntVector could grow very big, such as 50000, ... Please comment my code regarding the speed and memory usage, ... Instead of keeping all the values in one array whose ...
    (comp.lang.java.programmer)
  • Re: Struct with fixed-length array member - is it possible?
    ... > int V1; ... > THE SAME MEMORY LAYOUT as the C structure. ... > block and all data, including the array, should be placed in this block. ... > public struct XYZ ...
    (microsoft.public.dotnet.framework)