www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alignment of struct

reply "TJB" <broughtj gmail.com> writes:
I am a bit confused by the alignment of a struct in D.  I have 
some C++ code that I am translating into D.

Here is the C++:

#include <fstream>
#include <iostream>
#include <string.h>

struct TradesIdx
{
   char symbol[10];
   int tdate;
   int begrec;
   int endrec;
}__attribute__((packed));

struct TradesBin
{
   int ttim;
   int prc;
   int siz;
   short int g127;
   short int corr;
   char cond[2];
   char ex[1];
}__attribute__((packed));

int main()
{
   std::string 
infile("/Users/tyler/BFE/Measures/data/201212/T201212A.IDX");
   std::ifstream fin(infile.c_str(), std::ios::in | 
std::ios::binary);

   std::cout << "Size of TradesIdx = " << sizeof(TradesIdx) << 
std::endl;
   std::cout << "Size of TradesBin = " << sizeof(TradesBin) << 
std::endl;

   return 0;
}

Which when run gives the values:

Size of TradesIdx = 22
Size of TradesBin = 19

Here is the D code:

import std.stdio : writefln;
import std.stream;

align(1) struct TradesIdx
{
   char[10] symbol;
   int tdate;
   int begrec;
   int endrec;
}

align(1) struct TradesBin
{
   int ttim;
   int prc;
   int siz;
   short g127;
   short corr;
   char[2] cond;
   char[1] ex;
}

void main()
{
   string infile = 
"/Users/tyler/BFE/Measures/data/201212/T201212A.IDX";
   auto fidx = new File(infile, FileMode.In);

   writefln("Size of TradesIdx = %s", TradesIdx.sizeof);
   writefln("Size of TradesBin = %s", TradesBin.sizeof);
}

Which when run gives:

Size of TradesIdx = 24
Size of TradesBin = 19

Why the difference?  What is weird is that the two codes used to 
give the same answers. Has the alignment value of structs changed 
recently?
Mar 13 2014
parent reply Justin Whear <justin economicmodeling.com> writes:
On Thu, 13 Mar 2014 23:19:59 +0000, TJB wrote:
 
 Why the difference?  What is weird is that the two codes used to give
 the same answers. Has the alignment value of structs changed recently?
It did change a few versions ago (not sure exactly when). Try adding `align(1):` inside the structs before the members.
Mar 13 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Justin Whear:

 It did change a few versions ago (not sure exactly when).  Try 
 adding
 `align(1):` inside the structs before the members.
Yes it's a recent change, the align() on outside is the alignment of the whole struct, and the align inside is the alignment of the single field relative to the start of the struct or the precedent field. align(1) struct Foo { align(1) double x; } Bye, bearophile
Mar 13 2014
parent "TJB" <broughtj gmail.com> writes:
On Friday, 14 March 2014 at 00:07:12 UTC, bearophile wrote:
 Justin Whear:

 It did change a few versions ago (not sure exactly when).  Try 
 adding
 `align(1):` inside the structs before the members.
Yes it's a recent change, the align() on outside is the alignment of the whole struct, and the align inside is the alignment of the single field relative to the start of the struct or the precedent field. align(1) struct Foo { align(1) double x; } Bye, bearophile
Thanks! That was it. TJB
Mar 14 2014