Re: wstring always 2-byte aligned?

2011-06-02 Thread Andrej Mitrovic
Btw there's .alignof for these things, which will return 2 bytes.


Re: wstring always 2-byte aligned?

2011-06-02 Thread Steven Schveighoffer

On Thu, 02 Jun 2011 16:53:12 -0400, Nick Sabalausky  wrote:


I found a user comment on an MDSN Windows API reference page (Which I've
since lost, but I think it was somewhere in the Registry section.) that
claims that the Unicode-taking functions in the Windows API (or at least
some of them) require the unicode strings to be aligned on a two-byte
boundary, otherwise they might not work.

Do D's wstrings (in both D1 and D2) always follow this two-byte alignment
(provided that you're not doing any packed-alignment structs, or
cast-trickery), or is it something that we need to manually check?


Easy enough to test:

steves@steve-laptop:~/testd$ cat testalign.d
struct Foo
{
ubyte pad;
wchar wc;
}

pragma(msg, Foo.wc.offsetof.stringof);

struct Foo2
{
ubyte pad;
dchar dc;
}

pragma(msg, Foo2.dc.offsetof.stringof);
steves@steve-laptop:~/testd$ ~/dmd-2.053/linux/bin32/dmd -c testalign.d
2u
4u

So I'd say it does align to 2-byte boundaries (and dchar to 4-byte).  I  
can't think of a situation where the compiler would break this rule,  
except for manually overridden (as you mentioned).


Note that all dynamic heap allocations are 16-byte aligned.

-Steve


Re: wstring always 2-byte aligned?

2011-06-02 Thread Andrej Mitrovic
Maybe they mean UCS-2? I know that for example Charles Petzold's
Programming Windows book assumes that UTF16 characters are *always* 2
bytes wide. So maybe that has something to do with that alignment
requirement.


wstring always 2-byte aligned?

2011-06-02 Thread Nick Sabalausky
I found a user comment on an MDSN Windows API reference page (Which I've 
since lost, but I think it was somewhere in the Registry section.) that 
claims that the Unicode-taking functions in the Windows API (or at least 
some of them) require the unicode strings to be aligned on a two-byte 
boundary, otherwise they might not work.

Do D's wstrings (in both D1 and D2) always follow this two-byte alignment 
(provided that you're not doing any packed-alignment structs, or 
cast-trickery), or is it something that we need to manually check?