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


Groups > comp.lang.c > #9556

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

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: "struct hack" with non-character array[1]
Date 2011-08-19 08:40 -0400
Organization A noiseless patient Spider
Message-ID <j2llji$vod$1@dont-email.me> (permalink)
References <hbf.20110819eide@bombur.uio.no>

Show all headers | View raw


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

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


Thread

"struct hack" with non-character array[1] Hallvard B Furuseth <h.b.furuseth@usit.uio.no> - 2011-08-19 12:28 +0200
  Re: "struct hack" with non-character array[1] Ian Collins <ian-news@hotmail.com> - 2011-08-19 23:15 +1200
    Re: "struct hack" with non-character array[1] Hallvard B Furuseth <h.b.furuseth@usit.uio.no> - 2011-08-19 15:12 +0200
    Re: "struct hack" with non-character array[1] falk@rahul.net (Edward A. Falk) - 2011-08-19 16:41 +0000
  Re: "struct hack" with non-character array[1] Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-19 08:01 -0400
  Re: "struct hack" with non-character array[1] James Kuyper <jameskuyper@verizon.net> - 2011-08-19 08:40 -0400
  Re: "struct hack" with non-character array[1] Keith Thompson <kst-u@mib.org> - 2011-08-19 09:41 -0700

csiph-web