Regarding emplace, arrays, and helper functions

Ali Çehreli acehreli at yahoo.com
Fri Aug 30 11:02:00 PDT 2013


On 08/30/2013 07:02 AM, Andrej Mitrovic wrote:

 > However will the compiler align all static arrays so their memory
 > begins at a proper offset?

The compiler considers only the element type of the static array.

 > Maybe a more appropriate question is: Is all stack data guaranteed to
 > be properly aligned, and on all platforms?

I don't know the answer but I don't think it is specified even for C++.

 > For example:
 >
 >      enum Size = paddedSize!C;
 >
 >      ubyte[1] preBuffer;
 >
 >      ubyte[Size][2] buffer;
 >
 > Is 'buffer' guaranteed to be aligned so its memory begins at a good
 > offset?

I don't think so because the elements are ubytes and they can be placed 
any odd address. Perhaps it works on the stack but the following 
align(1) struct demonstrates that your buffer can be at an odd address:

import std.stdio;

align(1)
struct S
{
     ubyte[1] preBuffer;
     ubyte[16][2] buffer;
}

void main()
{
     writeln(S.preBuffer.offsetof);    // prints 0
     writeln(S.buffer.offsetof);       // prints 1
}

Ali



More information about the Digitalmars-d-learn mailing list