www.un.org/Depts/DGACM/index_spanish.htm 我有99种例行公事,需要临时储存各种不同的数据类型,并满足不同的统一要求。 目前,我打电话<条码>>,多度使用(a) 介绍大量间接费用和(b) ,并不能保证我的士们有良好的记忆。 我不能把温室装成单一结构,因为规模要求只是常年才知道。
A possible (but unlikely) answer for a concrete example: Say I want to allocate enough space for a char[3]
, a double[m]
with 16-byte alignment, an int
, and a float[n]
with 16-byte alignment and that I require them in memory in that order. Please ignore that the order is stupid and the example contrived. My actual use case is a a control struct followed by several temporary arrays of mixed integer/numeric types with alignments allowing SSE operations.
利用。 如何只使用标准图书馆来分配一致的记忆? 可以:
// Several unnecessary values (e.g. alignment_c) are holdovers
// from the macros generating this logic.
// ell, m, and n are sizes known only at runtime
const size_t datasize_c = 3*sizeof(char);
const size_t alignment_c = __alignof__(char);
const size_t pad_c = alignment_c - 1;
const uintptr_t mask_c = ~(uintptr_t)(alignment_c - 1);
const size_t datasize_d = ell*sizeof(double);
const size_t alignment_d = __alignof__(double) > 16 ? __alignof__(double) : 16;
const size_t pad_d = alignment_d - 1;
const uintptr_t mask_d = ~(uintptr_t)(alignment_d - 1);
const size_t datasize_i = m*sizeof(int);
const size_t alignment_i = __alignof__(int);
const size_t pad_i = alignment_i - 1;
const uintptr_t mask_i = ~(uintptr_t)(alignment_i - 1);
const size_t datasize_f = n*sizeof(float);
const size_t alignment_f = __alignof__(float) > 16 ? __alignof__(float) : 16;
const size_t pad_f = alignment_f - 1;
const uintptr_t mask_f = ~(uintptr_t)(alignment_f - 1);
const size_t p_parcel = (datasize_c + pad_c)
+ (datasize_d + pad_d)
+ (datasize_i + pad_i)
+ (datasize_f + pad_f) ;
void * const p = malloc(p_parcel) ;
char * c = (void *) (((uintptr_t)(p ) + pad_c & mask_c));
double * d = (void *) (((uintptr_t)(c + ell) + pad_d & mask_d));
int * i = (void *) (((uintptr_t)(d + m ) + pad_i & mask_i));
float * f = (void *) (((uintptr_t)(i + n ) + pad_f & mask_f));
// check if p is NULL, use (c, d, i, f), then free p
我认为,这种可能性在功能上是正确的,但我不禁要问,是否有更好、更清洁、更短的途径?
我认为,使用浮舱的方法是行不通的,因为我只能保证使用单一构造的小型水体对一个阵列进行统一。 我仍然需要三个<代码>。 要求有三处不同的障碍。
最后,我很高兴能够提供如果任何人想要的话,会产生这些东西的宏观。