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


Groups > comp.lang.c > #7777

Re: Alignment of foo[1][1][1][1]

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: Alignment of foo[1][1][1][1]
Date 2011-07-06 13:53 -0400
Organization A noiseless patient Spider
Message-ID <iv27au$9bu$1@dont-email.me> (permalink)
References <iugt1r$b5q$1@dont-email.me> <l764e8-egr.ln1@jones.homeip.net> <96ca5fe2-2997-4c27-a466-a1c22113a316@u26g2000vby.googlegroups.com> <iv1q39$g8q$1@dont-email.me> <f92f3b6a-a679-4176-84ee-02f25bb4a6c2@w24g2000yqw.googlegroups.com>

Show all headers | View raw


On 7/6/2011 11:55, Harald van Dijk wrote:
> On Jul 6, 3:07 pm, Shao Miller<sha0.mil...@gmail.com>  wrote:
>> On 7/2/2011 17:16, Harald van Dijk wrote:
>>
>>> which does not suggest to me that objects behave as arrays of length 1
>>> in other aspects. Am I overlooking something?
>>
>> Do you mean such as the cast concern of the original post?  That seems
>> like a slippery slope. :S
>
> In what way? I don't see any downsides to disallowing
>
>    int i = 0;
>    ++(*(int(*)[1])&i)[0];
>
> If you need an array, do this:
>
>    int i[1] = { 0 };
>    ++i[0];
>
> If you don't need an array, do this:
>
>    int i = 0;
>    ++i;
>

Those examples cover "static" and "automatic" arrays.  What about 
"allocated" or even working within "allocated" space with manual 
alignment accounting?

> Do you have a real example where it would be useful for you to
> reinterpret a non-array as an array (beyond what the standard already
> permits)?

Actually yes, this thread was prompted by a suggestion I'd made to 
someone else in another thread.  They wanted an "opaque type" which 
would be effectively compatible (non-Standardese) with the actual type. 
  That is, to pretend that one type was two different types:

   void foo_func(foo * my_foo);
   void bar_func(bar * my_bar);

I'd suggested the arbitrary:

   typedef foo bar[1][1][1];

Now you cannot accidentally do:

   foo * some_foo_ptr = ...;
   bar_func(some_foo_ptr);

Even though internally, 'bar_func' might very well simply convert its 
parameter to a 'foo *' type.  That conversion was the original cause of 
alignment concern, here.

An advantage to such an obfuscation, in my opinion, is that there can be 
no padding concerns while copying or iterating through an array of such 
types -- sizeof (bar) == sizeof (foo); contrast with 'struct's and 
'union's (however unlikely).

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


Thread

Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-06-29 23:11 -0500
  Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-06-29 23:13 -0500
  Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-01 09:57 -0700
  Re: Alignment of foo[1][1][1][1] lawrence.jones@siemens.com - 2011-07-02 14:56 -0400
    Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-02 14:16 -0700
      Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-06 10:07 -0400
        Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-06 08:55 -0700
          Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-06 13:53 -0400
            Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-06 11:34 -0700
              Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-06 15:48 -0400
                Re: Alignment of foo[1][1][1][1] Lauri Alanko <la@iki.fi> - 2011-07-06 21:46 +0000
                Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-07 09:31 -0400
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-06 14:50 -0700
                Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-07 09:43 -0400
    Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-03 15:57 -0700
      Re: Alignment of foo[1][1][1][1] lawrence.jones@siemens.com - 2011-07-06 00:06 -0400
        Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-06 13:25 -0400
        Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-07 00:21 -0700
          Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-07 23:24 -0400
            Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-08 09:11 -0700
              Re: Alignment of foo[1][1][1][1] Seebs <usenet-nospam@seebs.net> - 2011-07-08 17:21 +0000
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-08 11:55 -0700
                Re: Alignment of foo[1][1][1][1] Seebs <usenet-nospam@seebs.net> - 2011-07-08 23:02 +0000
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-08 16:41 -0700
                Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-09 10:32 -0700
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-09 13:22 -0700
                Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-09-04 12:23 -0700
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-09-04 13:29 -0700
                Re: Alignment of foo[1][1][1][1] Keith Thompson <kst-u@mib.org> - 2011-09-04 15:40 -0700
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-09-04 22:15 -0700
                Re: Alignment of foo[1][1][1][1] Keith Thompson <kst-u@mib.org> - 2011-09-05 00:12 -0700
                Re: Alignment of foo[1][1][1][1] James Kuyper <jameskuyper@verizon.net> - 2011-09-04 18:45 -0400
                Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-09 10:00 -0700
              Re: Alignment of foo[1][1][1][1] Shao Miller <sha0.miller@gmail.com> - 2011-07-08 23:55 -0400
              Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-09 08:55 -0700
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-09 09:19 -0700
                Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-09 10:48 -0700
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-07-09 13:25 -0700
                Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-09-04 11:00 -0700
                Re: Alignment of foo[1][1][1][1] Keith Thompson <kst-u@mib.org> - 2011-09-04 13:34 -0700
                Re: Alignment of foo[1][1][1][1] Harald van Dijk <truedfx@gmail.com> - 2011-09-04 13:06 -0700
  Re: Alignment of foo[1][1][1][1] Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-03 15:23 -0700

csiph-web