Skip to content

Instantly share code, notes, and snippets.

@arsenm
Created January 3, 2012 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsenm/1552930 to your computer and use it in GitHub Desktop.
Save arsenm/1552930 to your computer and use it in GitHub Desktop.
Why is the size smaller than it should be?
import std.stdio;
struct Vector(T)
{
T x, y, z;
}
struct Node(T)
{
Vector!(T) position;
T mass;
Node!(T)* next;
// if this is commented out the size is correct
Cell!(T)* cell()
{
return (cast(Cell!(T)*) &this);
}
/* On 64-bit system:
with cell() included: (Cell!(double).sizeof is only half of the sum of the element sizes)
Sizes body = 72 cell = 56 node = 40
Children Size = 64
Cell must be at least 120
Cell sum 120
without cell() included: (Cell!(double).sizeof = sum of the element sizes)
Sizes body = 72 cell = 120 node = 40
Children Size = 64
Cell must be at least 120
Cell sum 120
*/
}
struct Body(T)
{
align Node!(T) node;
Vector!(T) velocity;
bool ignore;
}
struct Cell(T)
{
align Node!(T) node;
Node!(T)* more;
T critRadius2;
union
{
Node!(T)* children[8];
struct QuadMatrix
{
T xx, xy, xz;
T yy, yz;
T zz;
};
QuadMatrix quad;
}
}
void main(string[] args)
{
writefln("Sizes body = %d cell = %d node = %d", Body!(double).sizeof, Cell!(double).sizeof, Node!(double).sizeof);
writefln("Children Size = %d ", Cell!(double).children.sizeof);
writefln("Cell must be at least %d ",
Node!(double).sizeof + (Node!(double)*).sizeof + double.sizeof + (Node!(double)*[8]).sizeof
);
writefln("Cell sum %d",
Cell!(double).node.sizeof
+ Cell!(double).more.sizeof
+ Cell!(double).critRadius2.sizeof
+ Cell!(double).children.sizeof
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment