Re: reading a C++ structure from a binary file using C#



"Ben Voigt" <rbv@xxxxxxxxxxxxx> wrote in message news:u4yT6LxcHHA.3632@xxxxxxxxxxxxxxxxxxxxxxx

"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:%23PmPPawcHHA.284@xxxxxxxxxxxxxxxxxxxxxxx
"Ben Voigt" <rbv@xxxxxxxxxxxxx> wrote in message news:ee$9PDwcHHA.1000@xxxxxxxxxxxxxxxxxxxxxxx

"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:eDerzTucHHA.4632@xxxxxxxxxxxxxxxxxxxxxxx
"Ben Voigt" <rbv@xxxxxxxxxxxxx> wrote in message news:eUJz4%23tcHHA.208@xxxxxxxxxxxxxxxxxxxxxxx

"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:uklz82tcHHA.4260@xxxxxxxxxxxxxxxxxxxxxxx
Nand,

In order to do this in .NET, I would read from the file in 74 byte blocks (4 + 20 + 50). Then, I would call the static ToInt32 method on the

The C++ code used sizeof(s)... you must print this value out from C++. It is unlikely to be 74, since the structure contains an int member which prefers 4-byte alignment... so I think two bytes of padding is added by the C++ compiler and the record spacing will be 76 bytes. But check it, because it pragma pack was used several different values would be possible.


Why? The int is the first field of the struct, the second is a char array which has no alignment restriction, so, whatever the packing, the length written on disk will be 74.

As I said, the OP's C code writes sizeof(s) bytes to disk. Padding is included in sizeof, because sizeof is the separation between adjacent array elements which must both be properly aligned.

If there's only one record in the file, it's a non-issue. But I was responding to a post mentioning "read the file in 74 byte blocks".


Oh, I see what you mean, but here the sizeof depends on the packing specified (or the default packing). This is why I'm always trying to define my structs like this;


#pragma pack(show)

Did you mean to add here:
#pragma pack(push, 1)

?

struct student
{
int roll_no;
char name[20];
char qualification[50];
};
#pragma pack(pop)
#pragma pack(show)

when writing to disk, especially when these files have to be read by "foreign" applications. No surprises here, the size is exactly the sum of all it's elements.

That's very non-portable though, many CPUs will generate hard exceptions if you use unaligned data. Also the compiler is allowed to reorder the members. So it's best to serialize each element individually.

Agreed, you can have portability issues doing this, but this is also true when relying on the default packing.
Note also that such issues are quite common when passing data from one platform (HW and OS) to another, I would never use this kind of packing for "application data structures" only when I need to pass binary data across program boundaries, and I'm never using this to pass data across non "compatible" (HW and OS) systems.
Anyway, the only CPU I know that will generate hard exceptions is Intel's Itanium, others like Intel's IA32, Intel 64 (X64 EM), AMD 32 and 64, all handle alignment issues in µcode (taking a small performance hit). The Alpha CPU raises an alignment exception, which the OS can correct or just ignore, on a per application basis, I thought Intel's Itanium did exactly the same.


Willy.

.



Relevant Pages

  • Re: reading a C++ structure from a binary file using C#
    ... because it pragma pack was used several different values ... the OP's C code writes sizeofbytes to disk. ... included in sizeof, because sizeof is the separation between adjacent ... Oh, I see what you mean, but here the sizeof depends on the packing ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reading a C++ structure from a binary file using C#
    ... because it pragma pack was used several different values would be possible. ... The int is the first field of the struct, the second is a char array which has no alignment restriction, so, whatever the packing, the length written on disk will be 74. ... Padding is included in sizeof, because sizeof is the separation between adjacent array elements which must both be properly aligned. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sizeof, Alignment and Pack
    ... > char a; ... > int w; ... > #pragma pack ... You never measure CB other than with a sizeof. ...
    (microsoft.public.vc.language)
  • Re: padding mechanism in structures
    ... #pragma pack([n]) ... Specifies packing alignment for structure and union members. ... level by the pack pragma. ...
    (comp.lang.c)
  • Re: gcc 3 versions wont compile hello.cpp
    ... changed in gcc3, how can one use gcc3 for non-vertical ... Given a pragma to change the default to _System, ... int foo(int a, int b) ... Both use the stack right to left and both prepend an underscore. ...
    (comp.os.os2.programmer.tools)