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


Groups > comp.lang.c > #7782

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 15:48 -0400
Organization A noiseless patient Spider
Message-ID <iv2e2k$lad$1@dont-email.me> (permalink)
References (2 earlier) <96ca5fe2-2997-4c27-a466-a1c22113a316@u26g2000vby.googlegroups.com> <iv1q39$g8q$1@dont-email.me> <f92f3b6a-a679-4176-84ee-02f25bb4a6c2@w24g2000yqw.googlegroups.com> <iv27au$9bu$1@dont-email.me> <4ef96460-ec9c-4959-909d-9d633579a8cd@j15g2000yqf.googlegroups.com>

Show all headers | View raw


On 7/6/2011 14:34, Harald van Dijk wrote:
> On Jul 6, 6:53 pm, Shao Miller<sha0.mil...@gmail.com>  wrote:
>> 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];
>
> Which will be problematic if any bar parameter is to be passed by
> value as a function argument. But depending on the meaning of foo/bar,
> this might not matter.
>

Agreed.  And you're right, the discussion partner was interested in 
pointer parameters only.

>> Now you cannot accidentally do:
>>
>>     foo * some_foo_ptr = ...;
>>     bar_func(some_foo_ptr);
>
> But the more common solution of a struct that you yourself mention:
>
>    typedef struct { int data; } foo;
>    typedef struct { int data; } bar;
>
> works just as well.
>

'foo' and 'bar' needn't be the same size as 'int'.  Their post gave me 
the impression that they already had interface functions for pointers to 
the "inner type," so I sensed that it was "too late" for the strategy 
you show above.  So I'm assuming that there is already a library and the 
request is towards adding a newer piece.

I don't recall if the original "inner type" was a scalar, aggregate or 
union, but could try to find the thread again (it was near the original 
post of this thread, likely).

>> 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.
>
> Distinct unnamed structure types with identical definition are
> effectively required to have the same size, representation and
> alignment.
>

And the path to that conclusion (in the Standard) strikes me as being 
rather round-about, if I recall that path correctly.  Heheheh.

>> 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).
>
> With the structure definitions above, sizeof(foo) == sizeof(bar). The
> reasoning is simple: both are compatible with the same struct { int
> data; } defined in a different translation unit.

Yeahbut what if it's too late and we want 'int' and 'foo' (following 
your example) to be similar?

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