Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: packing a "variable length struct" |
| Date | 2011-06-27 22:48 +1200 |
| Message-ID | <96r5guF2dgU8@mid.individual.net> (permalink) |
| References | <739a8aed-13cb-4cb4-9ac0-0e360bcef865@bl1g2000vbb.googlegroups.com> |
On 06/27/11 09:46 PM, fishboy wrote:
> Hi all,
>
> I'm implementing a performance critical algorithm which contains a
> "variable length struct", by which I mean
>
> typedef stuct
> {
> int level;
> long index;
> } A_t
>
> typedef struct
> {
> A_t meta;
> double *centres;
> } B_t
>
> where the length of the centres array in B_t will be decided at
> runtime.
>
> [yes, I know that this is a fixed sized struct, that's why the quotes]
>
> I have millions of these B_t which I would like to put through a
> generic FIFO
> (generic in the qsort() sense, using void*, explicit object sizes and
> memcpy())
> so it would be nice if I could "pack" the metadata and the centres
> into a single
> block of memory and push those blocks into the FIFO, thus avoiding
> handling
> the millions of mallocs() for the centres separately.
>
> I'm thinking of something like (here n is the centres' array size)
>
> B_t *B = some_function(n);
>
> size_t szA = sizeof(A_t),
> szC = n*sizeof(double);
> void *buffer = malloc(szA + szC);
>
> memcpy(buffer,&(B->meta), szA);
> memcpy(buffer+szA, B->centres, szC);
>
> for the packing.
>
> Is this standards conforming? (C99 if it makes a difference)
The idiomatic approach is to use the "struct hack" which is formalised
in C99 to declare your struct:
typedef struct
{
A_t meta;
double centres[];
} B_t;
--
Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
packing a "variable length struct" fishboy <j.j.fishbat@gmail.com> - 2011-06-27 02:46 -0700
Re: packing a "variable length struct" Ian Collins <ian-news@hotmail.com> - 2011-06-27 22:48 +1200
Re: packing a "variable length struct" Alexander Bartolich <alexander.bartolich@gmx.at> - 2011-06-27 11:01 +0000
Re: packing a "variable length struct" James Kuyper <jameskuyper@verizon.net> - 2011-06-27 07:19 -0400
Re: packing a "variable length struct" Shao Miller <sha0.miller@gmail.com> - 2011-06-27 14:12 -0400
Re: packing a "variable length struct" Shao Miller <sha0.miller@gmail.com> - 2011-06-27 15:08 -0400
Re: packing a "variable length struct" Alexander Bartolich <alexander.bartolich@gmx.at> - 2011-06-27 20:09 +0000
Re: packing a "variable length struct" Shao Miller <sha0.miller@gmail.com> - 2011-06-27 17:45 -0400
Re: packing a "variable length struct" Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-27 07:24 -0400
Re: packing a "variable length struct" Keith Thompson <kst-u@mib.org> - 2011-06-27 09:06 -0700
Re: packing a "variable length struct" BGB <cr88192@hotmail.com> - 2011-06-27 15:35 -0700
Re: packing a "variable length struct" fishboy <j.j.fishbat@gmail.com> - 2011-06-30 23:54 -0700
csiph-web