Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #126329

Re: Aligned Issue when malloc()

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Aligned Issue when malloc()
Date 2018-02-06 13:48 +0000
Organization A noiseless patient Spider
Message-ID <87y3k6kz52.fsf@bsb.me.uk> (permalink)
References <7d04ef8a-4410-4855-a69a-34c350465879@googlegroups.com> <874lmumj2n.fsf@bsb.me.uk> <dbe46486-cc00-4ecd-9288-7ef6b54846c9@googlegroups.com>

Show all headers | View raw


hwding <m@amastigote.com> writes:
<snip>
> Thanks for the answers, Ben & lan.
> Now I think I'm clear with this "struct hack" stuff.
> But I'm wondering why we choose void * and double
> but not int, key_t or float?
>
> What is "strictest alignment"?

Someone chose those types in an attempt to get the strictest alignment,
but there is no reliable way to do that using a union.  A long double
may need more alignment, structs may need more, even arrays of int may
need more.

In modern C, there is an _Alignof operator, so the way to do this now is
to rely on the fact that malloc allocates storage aligned for any type,
and to put the size at the end, suitably aligned -- since you know the
type, you can make sure it is correctly aligned.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Aligned Issue when malloc() hwding <m@amastigote.com> - 2018-02-06 01:17 -0800
  Re: Aligned Issue when malloc() Ian Collins <ian-news@hotmail.com> - 2018-02-06 22:41 +1300
  Re: Aligned Issue when malloc() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-06 11:52 +0000
    Re: Aligned Issue when malloc() hwding <m@amastigote.com> - 2018-02-06 04:35 -0800
      Re: Aligned Issue when malloc() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-06 13:48 +0000
        Re: Aligned Issue when malloc() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-02-06 12:55 -0800
      Re: Aligned Issue when malloc() Richard Damon <Richard@Damon-Family.org> - 2018-02-06 08:52 -0500
        Re: Aligned Issue when malloc() "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-06 09:59 -0500
          Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-06 07:20 -0800
            Re: Aligned Issue when malloc() Robert Wessel <robertwessel2@yahoo.com> - 2018-02-06 11:50 -0600
            Re: Aligned Issue when malloc() Keith Thompson <kst-u@mib.org> - 2018-02-06 11:34 -0800
            Re: Aligned Issue when malloc() Egor <egor@ruby.local> - 2018-02-09 16:20 +0200
              Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-15 08:56 -0800
                Re: Aligned Issue when malloc() Egor <egor@ruby.local> - 2018-02-15 19:35 +0200
                Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-15 11:27 -0800
                Re: Aligned Issue when malloc() Egor <egor@ruby.local> - 2018-02-16 02:48 +0200
                Re: Aligned Issue when malloc() supercat@casperkitty.com - 2018-02-16 08:49 -0800
      Re: Aligned Issue when malloc() "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-06 09:41 -0500

csiph-web