Groups | Search | Server Info | Login | Register


Groups > comp.lang.c.moderated > #462

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

From Árpád Goretity <arpad.goretity@gmail.com>
Newsgroups comp.lang.c.moderated
Subject Portable/strictly conforming alternative to the "struct hack" (?)
Date 2013-09-02 04:08 -0500
Organization Usenet Fact Police
Message-ID <clcm-20130902-0003@plethora.net> (permalink)

Show all headers | View raw


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: clcm@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.

Back to comp.lang.c.moderated | Previous | NextNext in thread | Find similar


Thread

Portable/strictly conforming alternative to the "struct hack" (?) Árpád Goretity <arpad.goretity@gmail.com> - 2013-09-02 04:08 -0500
  Re: Portable/strictly conforming alternative to the "struct hack" (?) Jasen Betts <jasen@xnet.co.nz> - 2013-09-06 23:24 -0500
    Re: Portable/strictly conforming alternative to the "struct hack" (?) Jasen Betts <jasen@xnet.co.nz> - 2013-09-11 17:26 -0500
      Re: Portable/strictly conforming alternative to the "struct hack" (?) Árpád Goretity <arpad.goretity@gmail.com> - 2013-10-03 13:37 -0500
        Re: Portable/strictly conforming alternative to the "struct hack" (?) Keith Thompson <kst-u@mib.org> - 2013-10-08 16:38 -0500

csiph-web