Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #9548 > unrolled thread
| Started by | Hallvard B Furuseth <h.b.furuseth@usit.uio.no> |
|---|---|
| First post | 2011-08-19 12:28 +0200 |
| Last post | 2011-08-19 09:41 -0700 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.c
"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
| From | Hallvard B Furuseth <h.b.furuseth@usit.uio.no> |
|---|---|
| Date | 2011-08-19 12:28 +0200 |
| Subject | "struct hack" with non-character array[1] |
| Message-ID | <hbf.20110819eide@bombur.uio.no> |
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
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-19 23:15 +1200 |
| Message-ID | <9b6uv8Fr4mU1@mid.individual.net> |
| In reply to | #9548 |
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
[toc] | [prev] | [next] | [standalone]
| From | Hallvard B Furuseth <h.b.furuseth@usit.uio.no> |
|---|---|
| Date | 2011-08-19 15:12 +0200 |
| Message-ID | <hbf.20110819hgzi@bombur.uio.no> |
| In reply to | #9549 |
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
[toc] | [prev] | [next] | [standalone]
| From | falk@rahul.net (Edward A. Falk) |
|---|---|
| Date | 2011-08-19 16:41 +0000 |
| Message-ID | <j2m3o9$qo8$1@blue-new.rahul.net> |
| In reply to | #9549 |
In article <9b6uv8Fr4mU1@mid.individual.net>,
Ian Collins <ian-news@hotmail.com> 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, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-08-19 08:01 -0400 |
| Message-ID | <j2ljdo$j0t$1@dont-email.me> |
| In reply to | #9548 |
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
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-08-19 08:40 -0400 |
| Message-ID | <j2llji$vod$1@dont-email.me> |
| In reply to | #9548 |
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
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-19 09:41 -0700 |
| Message-ID | <ln39gx1hh2.fsf@nuthaus.mib.org> |
| In reply to | #9548 |
Hallvard B Furuseth <h.b.furuseth@usit.uio.no> 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) kst-u@mib.org <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"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web