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


Groups > comp.std.c > #1376

Re: Structure Packing.

From Hans-Bernhard Bröker <HBBroeker@t-online.de>
Newsgroups comp.std.c
Subject Re: Structure Packing.
Date 2012-06-13 15:53 +0200
Message-ID <a3rkb6Fb4tU1@mid.dfncis.de> (permalink)
References (4 earlier) <4FD7659E.89172310@bytecraft.com> <a3p9irFkgbU1@mid.dfncis.de> <4FD79328.A040F7B8@bytecraft.com> <jr88ej$qan$1@dont-email.me> <4FD7B831.8B6BBEB9@bytecraft.com>

Show all headers | View raw


On 12.06.2012 23:44, Walter Banks wrote:
> Eric Sosman wrote:

>>       Perhaps structs are not the right tool for the job?  Instead of
>>
>>          struct {
>>              unsigned char flag;
>>              double value;
>>          } data[1000];
>>
>> ... you might do better to write
>>
>>          unsigned char data_flag[1000];
>>          double data_value[1000];

Or the slightly less obscure

	struct {
	    unsigned char flag[1000];
	    double value[1000];
	} data;

I guess one big reason that array-of-structs is favoured over this 
struct-of-arrays approach quite a lot these days is that all those 
caches favour locality over dense packing.

> This whole question started to show itself with a piece of customer
> code that has a large struct array with about 25 % of the ram
> space un-used.

> From a customer point of view there isn't an easy way to
> identify the solution to the problem without knowing a quite
> a bit about both this specific hardware and the C compiler
> constraints allocating struct space.

Well, writing tight code does tend to require such knowledge anyway.  A 
rule-of-thumb like "big things first in structs" covers most of the 
ground, and should be known to anyone seriously working with this kind 
of processor.

> There are several data types involved. It is information a programmer
> should not need to know at that level of detail for most routine
> applications

OTOH "routine applications" should not cut their RAM needs quite so 
close to the bone that this really matters.

> Having the compiler make an intelligent suggestion is a
> workable solution.

I agree that the optimal solution here would be an elective info/warning 
message from the compiler as it sees sub-optimal packing; particularly 
if optimization is turned on to favour size over speed.  GCC's -Wpadded 
comes to mind.

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