Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Pointer conversion and alignment Date: Mon, 19 Nov 2018 13:51:39 -0800 Organization: None to speak of Lines: 48 Message-ID: References: <20181117143819.43938ee34006bbcc1826bf0c@gmail.com> <20181120000227.bd5d6bdc9d0afe69fdbef1af@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="7f5cceb17ce04cc77f77fa86be7df331"; logging-data="15533"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FAJA2QwRHdBlWxTmY8RMj" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:wzushFp3FXXyx5LqGs1cXEjIjP0= sha1:VEXua/FFKAwIyMAaaoVKQHRkDN0= Xref: csiph.com comp.lang.c:138135 Anton Shepelev writes: > Thank you for the reply, jadill33: [...] >> However, if you know the max_align_t size for your system, you can allocate >> integer multiples of that value and place the data at that offset. >> >> #define c_maxalign_sizeof(type) ( c_align_size( sizeof (type), ALIGN_MAX ) ) >> >> c_maxalign_sizeof example >> ----------------------------------------------- >> | type | sizeof | c_maxalign_sizeof | >> ----------------------------------------------- >> char 1 8 >> short int 2 8 >> int 4 8 >> long int 4 8 >> long long int 8 8 >> float 4 8 >> double 8 8 >> long double 12 16 >> size_t 4 8 >> ptrdiff_t 4 8 >> void* 4 8 >> struct c_timeval 16 16 >> struct c_list 12 16 >> struct c_array 28 32 >> >> ALIGN_MAX = 8 >> >> This is output from my sample program. It depends on c_align_size and >> ALIGN_MAX. > > Does it meant that int's occupy four bytes in memory yet > must be aligned to eight bytes? You must have a 64-bit > system and compiler, right? I hope it doesn't mean that. If it does, it's wrong. A type cannot have an alignment larger than its size. There are no gaps between array elements, which implies that int cannot have a size of 4 bytes and an alignment of 8 bytes. [...] -- Keith Thompson (The_Other_Keith) kst@mib.org Will write code for food. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"