Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Hallvard B Furuseth <h.b.furuseth@usit.uio.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: "struct hack" with non-character array[1] |
| Date | 2011-08-19 15:12 +0200 |
| Organization | University of Oslo, Norway |
| Message-ID | <hbf.20110819hgzi@bombur.uio.no> (permalink) |
| References | <hbf.20110819eide@bombur.uio.no> <9b6uv8Fr4mU1@mid.individual.net> |
Ian Collins <ian-news@hotmail.com> 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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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