分配几个相同类型的数组
我需要分配几个相同类型和形状的数组。 一开始,我做了类似的事情:
void alloc_arrays_v1(size_t nmemb) { int *a1, *a2, *a3; a1 = malloc(nmemb * sizeof int); a2 = malloc(nmemb * sizeof int); a3 = malloc(nmemb * sizeof int); /* do some stuff with the arrays */ free(a1); free(a2); free(a3); }
为避免多次调用malloc
和free
,我将上面改为:
void alloc_arrays_v2(size_t nmemb) { int *a, *a1, *a2, *a3; a = malloc(3 * nmemb * sizeof int); a1 = a; a2 = a1 + nmemb; a3 = a2 + nmemb; /* do some stuff */ free(a); }
这似乎没问题(在某种意义上,函数在实际情况下的行为方式相同),但我想知道这是否仍然是有效的C代码(未定义的行为?),如果我可以将此方法扩展到复杂数据kind(结构数组等)。
它在C中完全有效。但请记住只释放a
指针。 你的这个方法类似于struct hack
但是我认为这段代码中的一个逻辑问题是,如果你超出a1
或a2
的范围,你将无法注意到它,因为你将访问有效的内存地址,即你不会得到Seg Fault。
但是,在第一种情况下,您“可能”获得SegFault并注意到您的错误。
两者都是有效的,因为您使用malloc
来分配连续的内存。 实际上,在C中对矩阵进行建模时,通常会使用与第二种情况类似的代码。
值得注意的是
int a1, a2, a3, a4; int* a = &a1; int oops = *(a + 1);
是未定义的行为,因为您不能指望堆栈分配是连续的。
这完全有效。
你做的基本上与此相同:
void alloc_arrays_v1(size_t nmemb) { typedef int one_array[nmemb]; // this is one array typedef one_array three_arrays[3]; // this are three arrays one_array * a; int *a1, *a2, *a3; a = malloc(sizeof(three_arrays)); a1 = a[0]; // a[0] is a one_array, which decays into an int * here. a2 = a[1]; // the same a3 = a[2]; // as above /* do some stuff with the arrays */ free(a); }
区别在于计算是由指针和数组算术完成的。