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


Groups > comp.std.c > #1371

Re: Structure Packing.

From Walter Banks <walter@bytecraft.com>
Newsgroups comp.std.c
Subject Re: Structure Packing.
Date 2012-06-12 15:24 -0400
Organization Aioe.org NNTP Server
Message-ID <4FD79758.38D5CBF0@bytecraft.com> (permalink)
References <12b3df42-f6a0-4411-a5e5-136900f8255b@googlegroups.com> <jqovin$9sr$1@dont-email.me> <4FD6ABFA.72F52C75@bytecraft.com> <jr7fpp$eti$1@dont-email.me> <jr7qcu$p1u$1@dont-email.me>

Show all headers | View raw



Martin Shobe wrote:

> I don't think you understand what's being proposed.
> (Of course, it might be my understanding that's faulty). The project is
> to build a compiler for a system that has some strict alignment
> restrictions, and very little RAM. As such, there isn't an issue with
> different alignments on different systems, and the fact that those
> asserts may fail actually helps.
>
> The issue I see is that the asserts in the following must not fail.
>
> struct s {
>     unsigned char flag;
>     // hole here that is exactly the right size for
>     // an unsigned char.
>     unsigned int value;
> } x;
>
> // y has been placed by the compiler into the hole in x immediately
> // after x.flag
> unsigned char y;
>
> int main()
> {
>     unsigned char * hole = &x.flag + 1;
>
>     y = 42;
>     *hole = 0xFF;
>     assert(*hole == 0xFF);
>     assert(y == 42);
>
>     return 0;
> }

This is the exact problem.

I am less sure that pointer math in the asserts
should be expected to reference the holes.


The following struct illustrates the problem
Assume that ints need to be on even boundaries
(assume for a moment that ints are 16 bits char is 8 bits)
the following struct will take a total of 6 bytes two are unused

struct  {
   unsigned char flag;
   unsigned int value;
   unsigned char flag2;
} z;

The following struct contains the same information
but only requires 4 bytes of ram to store the
information with no unused bytes. This is a
change the compiler struct allocator 'could' do.

struct  {
   unsigned char flag;
  unsigned char flag2;
   unsigned int value;
 } o;

Compatibility is not an issue even that could be
covered with an allocation rules pragma.

I am totally convinced that using the holes for other
globals is a bad idea.

I am less convinced but very uncomfortable to
changing a structs element order to first fit within
a struct.

Intetresting  problem

Walter..

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


Thread

Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-06 17:27 -0700
  Re: Structure Packing. James Kuyper <jameskuyper@verizon.net> - 2012-06-06 21:19 -0400
    Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-11 22:39 -0400
      Re: Structure Packing. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-12 09:21 -0400
        Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 11:51 -0400
          Re: Structure Packing. Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-06-12 18:37 +0200
            Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 15:06 -0400
              Re: Structure Packing. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-12 16:22 -0400
                Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 17:44 -0400
                Re: Structure Packing. Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-06-13 15:53 +0200
                Re: Structure Packing. Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-06-18 12:37 +0300
        Re: Structure Packing. Martin Shobe <martin.shobe@yahoo.com> - 2012-06-12 16:22 +0000
          Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 15:24 -0400
            Re: Structure Packing. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-12 16:33 -0400
              Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 17:17 -0400
  Re: Structure Packing. Keith Thompson <kst-u@mib.org> - 2012-06-06 18:35 -0700
    Re: Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-06 21:58 -0700
      Re: Structure Packing. Keith Thompson <kst-u@mib.org> - 2012-06-06 23:54 -0700
        Re: Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-07 07:47 -0700
          Re: Structure Packing. James Kuyper <jameskuyper@verizon.net> - 2012-06-07 12:39 -0400
    Re: Structure Packing. jacob navia <jacob@spamsink.net> - 2012-06-07 11:14 +0200
      Re: Structure Packing. Keith Thompson <kst-u@mib.org> - 2012-06-07 09:46 -0700
        Re: Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-07 11:57 -0700

csiph-web