[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

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

Hallvard B Furuseth

8/19/2011 10:28:00 AM

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
6 Answers

Ian Collins

8/19/2011 11:16:00 AM

0

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

Eric Sosman

8/19/2011 12:02:00 PM

0

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

James Kuyper

8/19/2011 12:40:00 PM

0

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

Hallvard B Furuseth

8/19/2011 1:13:00 PM

0

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

Keith Thompson

8/19/2011 4:42:00 PM

0

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.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

falk

8/19/2011 4:42:00 PM

0

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.blo...