"struct hack" with non-character array[1]

Discussion in 'C Programming' started by Hallvard B Furuseth, Aug 19, 2011.

  1. I seem to remember the "struct hack" is safer if the fake
    dynamic array has character type. If that's right, where/why?
    Maybe in C99, since the struct hack is undefined in C89? Or
    real life implementations, nothing to do with the standard?

    That is,
    typedef <char or int> T;
    struct hack { Foo foo; T dynamic[1]; };
    struct hack h = malloc(sizeof *h) + sizeof(T)*n;
    .... use h->dynamic[0..n-1] ...;
    would be safer with T = char than with T = int.

    The C FAQ does not mention it. And I know about C99
    flexible array members, but that's not the question.

    --
    Hallvard
     
    Hallvard B Furuseth, Aug 19, 2011
    #1
    1. Advertising

  2. Hallvard B Furuseth

    Ian Collins Guest

    On 08/19/11 10:28 PM, Hallvard B Furuseth wrote:
    > I seem to remember the "struct hack" is safer if the fake
    > dynamic array has character type. If that's right, where/why?


    Why should it be?

    > Maybe in C99, since the struct hack is undefined in C89? Or
    > real life implementations, nothing to do with the standard?
    >
    > That is,
    > typedef<char or int> T;
    > struct hack { Foo foo; T dynamic[1]; };


    make that

    struct hack { Foo foo; T dynamic[]; };

    for C99.

    > struct hack h = malloc(sizeof *h) + sizeof(T)*n;
    > .... use h->dynamic[0..n-1] ...;
    > would be safer with T = char than with T = int.


    It wouldn't compile with either. The flexible array member is irrelevant.

    --
    Ian Collins
     
    Ian Collins, Aug 19, 2011
    #2
    1. Advertising

  3. Hallvard B Furuseth

    Eric Sosman Guest

    On 8/19/2011 6:28 AM, Hallvard B Furuseth wrote:
    > I seem to remember the "struct hack" is safer if the fake
    > dynamic array has character type. If that's right, where/why?
    > Maybe in C99, since the struct hack is undefined in C89? Or
    > real life implementations, nothing to do with the standard?
    >
    > That is,
    > typedef<char or int> T;
    > struct hack { Foo foo; T dynamic[1]; };
    > struct hack h = malloc(sizeof *h) + sizeof(T)*n;


    Garbled syntax? ITYM

    struct hack *h = malloc(sizeof *h + sizeof(T)*n);

    > .... use h->dynamic[0..n-1] ...;


    Aside: Since sizeof(*h) already includes storage for one
    T element, you could use h->dynamic[0..n] (or use n-1 in the
    malloc).

    > would be safer with T = char than with T = int.
    >
    > The C FAQ does not mention it. And I know about C99
    > flexible array members, but that's not the question.


    I can't see any reason to think a trailing char[] is any
    more or less risky than a trailing T[]. Both will (almost
    always) work, and neither is guaranteed to work.

    Perhaps the relative riskiness involved a slightly
    different situation, something like

    typedef<char or whatever> T;
    struct hack { int this; double that; }; // no T[]
    #define PAYLOAD(hackptr) (T*)((hackptr) + 1)
    h = malloc(sizeof *h + sizeof(T)*n);
    ... use PAYLOAD(h)[0..n-1]

    This form certainly *is* safer with char T, because you don't
    have alignment issues with the payload. (In fact, this form
    is perfectly well-defined if the alignment issues are avoided
    or dealt with -- but dealing with them is clumsy, hence there's
    a temptation not to do so and incur the risk.)

    --
    Eric Sosman
    d
     
    Eric Sosman, Aug 19, 2011
    #3
  4. Hallvard B Furuseth

    James Kuyper Guest

    On 08/19/2011 06:28 AM, Hallvard B Furuseth wrote:
    > I seem to remember the "struct hack" is safer if the fake
    > dynamic array has character type. If that's right, where/why?
    > Maybe in C99, since the struct hack is undefined in C89? Or


    The struct hack is no better defined in C99 than in C89. The struct hack
    has undefined behavior under both versions of the standard, because it
    addresses elements of an array beyond it's declared length. It worked
    fine on most, and probably all, c90 compilers, but it was never required
    to work. With C99, flexible array members have been added to the
    language, so there's really no reason to rely upon the struct hack anymore.

    > real life implementations, nothing to do with the standard?
    >
    > That is,
    > typedef <char or int> T;
    > struct hack { Foo foo; T dynamic[1]; };
    > struct hack h = malloc(sizeof *h) + sizeof(T)*n;


    struct hack *h ...

    > .... use h->dynamic[0..n-1] ...;
    > would be safer with T = char than with T = int.


    I know of no reason why that should be so.
    --
    James Kuyper
     
    James Kuyper, Aug 19, 2011
    #4
  5. Ian Collins <> writes:

    > On 08/19/11 10:28 PM, Hallvard B Furuseth wrote:
    >> I seem to remember the "struct hack" is safer if the fake
    >> dynamic array has character type. If that's right, where/why?

    >
    > Why should it be?


    No idea, so I asked here. It's just something I remember, but I
    can't remeber from where. Maybe from long ago, since the hack
    was shot down by stages.

    >> Maybe in C99, since the struct hack is undefined in C89? Or
    >> real life implementations, nothing to do with the standard?
    >>
    >> That is,
    >> typedef<char or int> T;
    >> struct hack { Foo foo; T dynamic[1]; };

    >
    > make that
    > struct hack { Foo foo; T dynamic[]; };
    > for C99.


    Nope, not my question.

    >> struct hack h = malloc(sizeof *h) + sizeof(T)*n;
    >> .... use h->dynamic[0..n-1] ...;
    >> would be safer with T = char than with T = int.

    >
    > It wouldn't compile with either. The flexible array member is irrelevant.


    Yes, should be malloc(sizeof *h + sizeof(T)*n) like Eric said.

    --
    Hallvard B Furuseth tlf: +47-22 85 28 13
    USIT
     
    Hallvard B Furuseth, Aug 19, 2011
    #5
  6. Hallvard B Furuseth <> writes:
    > I seem to remember the "struct hack" is safer if the fake
    > dynamic array has character type. If that's right, where/why?
    > Maybe in C99, since the struct hack is undefined in C89? Or
    > real life implementations, nothing to do with the standard?


    I've never heard of that, and I have no reason to believe it.
    It's probably either somebody's bad guess, or something that isn't
    actually the struct hack, as Eric Sosman speculates.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 19, 2011
    #6
  7. In article <>,
    Ian Collins <> wrote:
    >make that
    >
    >struct hack { Foo foo; T dynamic[]; };
    >
    >for C99.


    What's the market penetration for C99? Is it 100%?

    T dynamic[1]

    Compiles with 100% of compilers.

    (Yeah, I'm not much of an early adopter. For a reason.)

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
     
    Edward A. Falk, Aug 19, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,851
    Joerg Jooss
    Jan 19, 2006
  2. Capstar

    struct hack question

    Capstar, Feb 3, 2004, in forum: C Programming
    Replies:
    7
    Views:
    385
    Dave Thompson
    Feb 9, 2004
  3. Hallvard B Furuseth

    padding bytes in struct hack

    Hallvard B Furuseth, Oct 19, 2009, in forum: C Programming
    Replies:
    3
    Views:
    1,181
    user923005
    Oct 20, 2009
  4. Mark

    struct hack

    Mark, Mar 28, 2012, in forum: C Programming
    Replies:
    3
    Views:
    375
    James Kuyper
    Mar 28, 2012
  5. Jonathan Lee

    safe "struct hack"?

    Jonathan Lee, Aug 14, 2013, in forum: C++
    Replies:
    11
    Views:
    265
    James Kanze
    Aug 16, 2013
Loading...

Share This Page