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


Groups > comp.lang.c > #14157

Re: Alignment problems

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Alignment problems
Date 2011-12-01 13:46 +0000
Organization A noiseless patient Spider
Message-ID <jb80hb$ct8$1@dont-email.me> (permalink)
References <jb6gbf$58a$1@speranza.aioe.org>

Show all headers | View raw



"jacob navia" <jacob@spamsink.net> wrote in message
news:jb6gbf$58a$1@speranza.aioe.org...

> If I store double data in the "Data" member the machine will
> crash since apparently it is a 32 bit SPARC, and the start of Data
> is NOT aligned to an eight byte boundary.
>
> One solution (albeit very ugly) is:
>
> typedef struct _ListElement {
>     struct _ListElement *Next;
>     double alignment1;
>     char Data[MINIMUM_ARRAY_INDEX]; // Generic storage...
> } ListElement;

Why not just insert a 4-byte 'alignment1' element, but conditionally. So
that it is only used for a machine known to be a 32-bit SPARC.  This will
need to be done manually somewhere, unless the compiler for that machine
already has an identifying macro.

This would cost 4 bytes on SPARC and 0 elsewhere. Inserting an 8-byte field
unconditionally would cost 12 bytes on SPARC, and at least 8 bytes
elsewhere.

Unions might work, but usually mean rewriting all the code (because the name
of the union has to be specified on each access, for most compilers).

Alternatively, don't compilers have a 'pack' directive for exactly this 
purpose? Again this would be conditional. Ideally a way of controlling 
alignment on a field-by-field basis is needed.

-- 
bartc 

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


Thread

Alignment problems jacob navia <jacob@spamsink.net> - 2011-12-01 01:05 +0100
  Re: Alignment problems Ian Collins <ian-news@hotmail.com> - 2011-12-01 13:15 +1300
  Re: Alignment problems pacman@kosh.dhis.org (Alan Curry) - 2011-12-01 02:57 +0000
  Re: Alignment problems gordonb.hpln0@burditt.org (Gordon Burditt) - 2011-11-30 21:27 -0600
  Re: Alignment problems jacob navia <jacob@spamsink.net> - 2011-12-01 13:38 +0100
    Re: Alignment problems pacman@kosh.dhis.org (Alan Curry) - 2011-12-01 20:38 +0000
      Re: Alignment problems jacob navia <jacob@spamsink.net> - 2011-12-01 21:50 +0100
        Re: Alignment problems Ian Collins <ian-news@hotmail.com> - 2011-12-02 10:27 +1300
          Re: Alignment problems Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-04 00:23 +0000
            Re: Alignment problems Ian Collins <ian-news@hotmail.com> - 2011-12-04 13:31 +1300
        Re: Alignment problems ImpalerCore <jadill33@gmail.com> - 2011-12-01 14:34 -0800
  Re: Alignment problems "BartC" <bc@freeuk.com> - 2011-12-01 13:46 +0000
    Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-01 14:15 -0800
      Re: Alignment problems David Thompson <dave.thompson2@verizon.net> - 2012-01-19 02:44 -0500
  Re: Alignment problems "James" <no@spam.invalid> - 2011-12-01 19:43 -0800
  Re: Alignment problems "Brain" <Brian@myhome.com> - 2011-12-04 02:47 -0600
    Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-04 08:26 -0800
      Re: Alignment problems Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-08 00:12 +0200
        Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-07 22:31 -0800
          Re: Alignment problems lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2011-12-08 21:43 -0800
            Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-09 01:31 -0800
              Re: Alignment problems Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-09 14:15 +0200

csiph-web