February 27, 2012 Re: Binary I/O for Newbie | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Brandt | On 02/27/2012 11:43 AM, Tobias Brandt wrote: >>> If you wrote to the file with a C++ program, then I guess the >>> compiler aligned the data so that the whole struct is 128 bytes >>> in size. Technically, the C++ compiler is allowed to do >>> anything short of changing the order of the struct fields. > >> That is correct for non-POD types. The C++ compiler must treat >> POD structs essentially as if they are C structs. > > Correct me if I'm wrong. But as far a I know the C standard also > allows arbitrary alignment. You were correct. I somehow misread "short of changing the order" as meaning "even changing the order". But even then I wasn't entirely correct. Just found this thread: http://stackoverflow.com/q/281045 C guarantees that the members are not reordered, but C++ allows reordering by "The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1)." Ali |
February 27, 2012 Re: Binary I/O for Newbie | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Brandt | On Monday, 27 February 2012 at 19:28:07 UTC, Tobias Brandt wrote:
>> So, something like this should work:
>> [...]
>
> It really depends on how you wrote the file originally. If you
> know that it is packed, i.e. 10+32+32+32=106 bytes per record,
> then yes.
>
> If you wrote to the file with a C++ program, then I guess the
> compiler aligned the data so that the whole struct is 128 bytes
> in size. Technically, the C++ compiler is allowed to do
> anything short of changing the order of the struct fields.
>
> You could just let your C++ program print out sizeof(TaqIdx) or
> manually divide the file size by the number of records (if you
> know it) to make sure.
Just looked at my old C++ code. And the struct looks like this:
struct TaqIdx {
char symbol[10];
int tdate;
int begrec;
int endrec;
}__attribute__((packed));
So I am guessing I want to use the align(1) as Justin suggested. Correct?
TJB
|
February 27, 2012 Re: Binary I/O for Newbie | ||||
---|---|---|---|---|
| ||||
Posted in reply to tjb | > Just looked at my old C++ code. And the struct looks like this:
>
>
> struct TaqIdx {
> char symbol[10];
> int tdate;
> int begrec;
> int endrec;
> }__attribute__((packed));
>
> So I am guessing I want to use the align(1) as Justin suggested. Correct?
Yes.
|
Copyright © 1999-2021 by the D Language Foundation