F
fishboy
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)
Thanks in advance!
Jim
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)
Thanks in advance!
Jim