digitalmars.D.learn - wstring always 2-byte aligned?
- Nick Sabalausky (8/8) Jun 02 2011 I found a user comment on an MDSN Windows API reference page (Which I've...
- Andrej Mitrovic (4/4) Jun 02 2011 Maybe they mean UCS-2? I know that for example Charles Petzold's
- Steven Schveighoffer (23/31) Jun 02 2011 Easy enough to test:
- Andrej Mitrovic (1/1) Jun 02 2011 Btw there's .alignof for these things, which will return 2 bytes.
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?
Jun 02 2011
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.
Jun 02 2011
On Thu, 02 Jun 2011 16:53:12 -0400, Nick Sabalausky <a a.a> 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
Jun 02 2011
Btw there's .alignof for these things, which will return 2 bytes.
Jun 02 2011