On Sat, Jul 25, 2015 at 11:10 AM, enormouspenguin <
kimkh...@gmail.com> wrote:
> On Saturday, July 25, 2015 at 10:54:20 PM UTC+7, Matt Harden wrote:
>>
>>
http://golang.org/pkg/unsafe/#Sizeof will tell you the size of a type.
>> Also see AlignOf and OffsetOf.
>
> Thanks Matt, but I think you misread my question. I know about Sizeof,
> Alignof and Offsetof but what I want to know is how to find out the size of
> a *word*. Is it the same size as an int or uint var?
Yes.
>> I don't think you can rely on every struct in a slice or array to be
>> aligned. You can add padding where necessary to ensure alignment. Of course
>> alignment isn't guaranteed to remain the same across different compilers or
>> different versions of the same compiler.
>
>
> The piece of doc I quoted is so confusing. Is a struct in an allocated array
> considered allocated struct? If so, should all the structs in the array have
> its first word 64-bit aligned? The confusion is killing me.
The quote is "The first word in a global variable or in an allocated
struct or slice can be relied upon to be 64-bit aligned." The key
phrase there is "allocated", not "struct or slice." The first word in
a struct or slice that is allocated via new or via a composite literal
will be 64-bit aligned. A struct inside a slice or array is not
allocated by itself, and no particular alignment is guaranteed beyond
that of unsafe.AlignOf.
Ian