🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2321

Array hack (was Re: how do I typedef an array of pointers to a structure?)

#2321
From: S. Lee
Date: Mon, 18 Mar 1996 13:41
42 lines
1736 bytes
In article <4ii34r$nm7@solutions.solon.com>,
Kurt Watzka <watzka@stat.uni-muenchen.de> wrote:
>
>timur@vnet.ibm.com (Timur Tabi) writes:
>
>>typedef struct {
>>   unsigned uNumLinks;
>>   NODE *pnode[1];
>>} NODE_LINK;
>
>>Actually, the second field can be an array of any length, as long as it's at
>>least one element long.  In most cases, the array will end up being a few
>>elements long.
>
>In this case, you might run into problems, because the code generated
>by your compiler might have severe problems accessing all but the first
>element of that array. If you want what is conceptually a vector of
>pointers to NODE, you _should_ use
>
>   NODE **ppnode;
>
>instead of relying on the "array hack".

How about:

typedef struct {
   unsigned uNumLinks;
   NODE *pnode[REALLY_LARGE_NUMBER];
} NODE_LINK;

NODE_LINK nl;

nl = malloc(offsetof(NODE_LINK, pnode) + 5 * sizeof(NODE));
/* allocate 5 elements */

This saves 1 malloc call and the code to manage the extra pointer.

[And is technically undefined, because you can't reference a partial object,
 but I don't believe anyone's ever found an implementation it fails on. -mod]

sl14@cornell.edu
Witty .sig under construction.
Technical Details
Message-ID: <4ijp6t$19h@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4i804b$46u@solutions.solon.com> <4ii34r$nm7@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: sl14@crux1.cit.cornell.edu (S. Lee)
Newsgroups: comp.lang.c.moderated
Subject: Array hack (was Re: how do I typedef an array of pointers to a structure?)
Date: 18 Mar 1996 07:41:49 -0600
Organization: Nekomi Institute of Technology
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4ijp6t$19h@solutions.solon.com>
References: <4i804b$46u@solutions.solon.com> <4ii34r$nm7@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com