Portable/strictly conforming alternative to the "struct hack" (?)

167 views
Skip to first unread message

Árpád Goretity

unread,
Sep 2, 2013, 5:08:39 AM9/2/13
to
I believe I've found a way to achieve something like the well-known "struct hack". I'm curoius if this strictly conforms to C89/C90 (I'm writing a library and my goal is full, strict C89/C90 conformance.)

The main idea is: I allocate memory large enough to hold an initial struct and the elements of the array. The exact size is (K + N) * sizeof(array_base_type), where `K` is large enough so that `K * sizeof(array_base_type) >= sizeof(the_struct)`, and `N` is the number of array elements.

Then, I use the pointer that `malloc()` returned to store `the_struct`, then I use pointer arithmetic to obtain a pointer to the beginning of an array of `N` elements of the array following the struct.

One line of code is worth more than a thousand words, so here is an implementation:

typedef struct Header {
size_t length;
/* other members follow */
} Header;

typedef struct Value {
int type;
union {
int intval;
double fltval;
} v;
} Value;

/* round up to nearest multiple of sizeof(Value) so that a Header struct fits in */
size_t n_hdr = (sizeof(Header) + sizeof(Value) - 1) / sizeof(Value);

size_t n_arr = /* arbitrary array size here */;
void *frame = malloc((n_hdr + n_arr) * sizeof(Value));

Header *hdr = frame;
Value *stack_bottom = (Value *)frame + n_hdr;

My main concern is that the last two assignments (using `frame` as both a pointer to Header and a pointer to Value) may violate the strict aliasing rule. I do not, however, dereference `hdr` as a pointer to Value - it's only pointer arithmetic that is performed on `frame` in order to access the first element of the value array, so I don't effectively access *the same* object using pointers of different types.

So, is this approach any better than the classic struct hack (which has been officially deemed UB), or is it UB too?
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Jasen Betts

unread,
Sep 7, 2013, 12:24:26 AM9/7/13
to
On 2013-09-02, Árpád Goretity <arpad.g...@gmail.com> wrote:
> I believe I've found a way to achieve something like the well-known "struct hack". I'm curoius if this strictly conforms to C89/C90 (I'm writing a library and my goal is full, strict C89/C90 conformance.)
>
> The main idea is: I allocate memory large enough to hold an initial struct and the elements of the array. The exact size is (K + N) * sizeof(array_base_type), where `K` is large enough so that `K * sizeof(array_base_type) >= sizeof(the_struct)`, and `N` is the number of array elements.
>
> Then, I use the pointer that `malloc()` returned to store `the_struct`, then I use pointer arithmetic to obtain a pointer to the beginning of an array of `N` elements of the array following the struct.
>
> One line of code is worth more than a thousand words, so here is an implementation:
>
> typedef struct Header {
> size_t length;
> /* other members follow */
> } Header;

strange....

> typedef struct Value {
> int type;
> union {
> int intval;
> double fltval;
> } v;
> } Value;
>
> /* round up to nearest multiple of sizeof(Value) so that a Header struct fits in */

> size_t n_hdr = (sizeof(Header) + sizeof(Value) - 1) / sizeof(Value);

that n_hdr expression looks kind of dodgy, basically it evaluates to 1,
anyone who goes to such lengths to caclulate 1 is unlikley to have
discovered something special. After that I stopped trying to understand
your code

> So, is this approach any better than the classic struct hack (which has been officially deemed UB), or is it UB too?

Look at modern C compilers ...
this has been perfectly valid, well-behaved C since the 1999 standard:

struct clump {
int thing ;
sometype data[];
}

-- ⚂⚃ 100% natural

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Jasen Betts

unread,
Sep 11, 2013, 6:26:11 PM9/11/13
to
On 2013-09-07, Jasen Betts <ja...@xnet.co.nz> wrote:
> On 2013-09-02, Árpád Goretity <arpad.g...@gmail.com> wrote:

>> typedef struct Header {
>> size_t length;
>> /* other members follow */
>> } Header;

>> size_t n_hdr = (sizeof(Header) + sizeof(Value) - 1) / sizeof(Value);
>
> that n_hdr expression looks kind of dodgy, basically it evaluates to 1,
> anyone who goes to such lengths to caclulate 1 is unlikley to have
> discovered something special. After that I stopped trying to understand
> your code

I am of course wrong - having ignored the "/* other members follow */"
comment. sorry.

Árpád Goretity

unread,
Oct 3, 2013, 2:37:02 PM10/3/13
to
> anyone who goes to such lengths to caclulate 1 is unlikley to have
discovered something special

Don't insult me.

> that n_hdr expression looks kind of dodgy, basically it evaluates to 1,

How do you know?

> Look at modern C compilers ...
this has been perfectly valid, well-behaved C since the 1999 standard:

Again, I don't like being like an uninformed moron. I know we have C99. I just want to support Visual "Crappy" studio which doesn't. Hence I can't use C99. That's all.

Keith Thompson

unread,
Oct 8, 2013, 5:38:02 PM10/8/13
to
Árpád Goretity <arpad.g...@gmail.com> writes:
>> anyone who goes to such lengths to caclulate 1 is unlikley to have
> discovered something special
>
> Don't insult me.
>
>> that n_hdr expression looks kind of dodgy, basically it evaluates to 1,
>
> How do you know?
>
>> Look at modern C compilers ...
> this has been perfectly valid, well-behaved C since the 1999 standard:
>
> Again, I don't like being like an uninformed moron. I know we have
> C99. I just want to support Visual "Crappy" studio which
> doesn't. Hence I can't use C99. That's all.

When you post a followup, your newsreader should automatically
insert an attribution line, so that readers can tell who wrote what.
Please don't delete it.

You quote an article posted by Jasen Betts on September 6, but the
headers indicate that you replied to another article that he posted
on September 11, in which Jasen wrote:

| I am of course wrong - having ignored the "/* other members follow */"
| comment. sorry.

It's possible you didn't see that; this is a moderated group, which can
do funny things with headers and message-ids (and I'm too lazy to track
it down further).

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Reply all
Reply to author
Forward
0 new messages