[英]allocating memory to a pointer of an array of structures
I am trying to allocate memory to a pointer of an array of structures but when I compile the terminal sends me segmentation fault , this is my code:我正在尝试将 memory 分配给结构数组的指针,但是当我编译终端向我发送分段错误时,这是我的代码:
typedef struct codif {
unsigned char simbolo;
int nbits;
unsigned int codigo;
} codificacion;
codificacion **matcod;
*matcod = malloc(256 * sizeof((*matcod)[0]));
This pointer这个指针
codificacion **matcod;
is uninitialized and has an indeterminate value.未初始化并且具有不确定的值。 So dereferencing it like
*matcod
results in undefined behavior.因此,像
*matcod
那样取消引用它会导致未定义的行为。
Maybe you mean the following也许你的意思是以下
codificacion *matcod;
matcod = malloc( 256 * sizeof( *matcod ) );
Or something like the following或类似以下内容
codificacion *p;
codificacion **matcod = &p;
*matcod=malloc(256*sizeof((*matcod)[0]));
For example if you have a function defined like例如,如果您有一个 function 定义为
void f( codificacion **matcod )
{
*matcod=malloc(256*sizeof((*matcod)[0]));
//...
}
then it can be called like那么它可以被称为
codificacion *matcod;
f( &matcod );
You are probably expected to write a function that takes a pointer to a pointer to this array of structures as an argument.您可能需要编写一个 function ,它将指向该结构数组的指针的指针作为参数。 So
codificacion **matcod
is the argument of the function, not a local undefined variable which causes undefined behavior when you dereference it to store the address of the allocated array.所以
codificacion **matcod
是 function 的参数,而不是本地未定义变量,当您取消引用它以存储分配的数组的地址时会导致未定义的行为。
Here is a modified version:这是修改后的版本:
typedef struct codif {
unsigned char simbolo;
int nbits;
unsigned int codigo;
} codificacion;
int allocate_array(codificacion **matcod) {
*matcod = malloc(256 * sizeof((*matcod)[0]));
return *matcod != NULL;
}
And call this function passing the address of an actual pointer:并调用此 function 传递实际指针的地址:
codificacion *mat;
if (!allocate_array(&mat)) {
printf("allocation error\n");
}
You should always declare your variables with something.你应该总是用一些东西来声明你的变量。 Compilers will not always zero-out uninitialized variables.
编译器不会总是将未初始化的变量清零。 I'm also a big fan of using
calloc
when declaring arrays -- it's a stylistic choice, but especially with arrays of pointers, ensures everything is zeroed out.我也是在声明 arrays 时使用
calloc
的忠实粉丝——这是一种风格选择,但尤其是使用 arrays 指针时,可以确保一切都归零。 Uninitialized data can be hell to debug.未初始化的数据可能很难调试。
codificacion **matcod = NULL;
matcod = calloc(256, sizeof(codificacion*));
Note that we have created a 256 element array of pointers, not whole structs, and because **
is an array of pointers to structs and not an array of structs you then need to allocate each struct:请注意,我们创建了一个 256 元素的指针数组,而不是整个结构,并且因为
**
是指向结构的指针数组而不是结构数组,所以您需要分配每个结构:
for(int index=0; index<256; index++)
matcod[index] = malloc(sizeof(codificacion));
You would then reference your elements with matcod[index]->nbits
.然后,您将使用
matcod[index]->nbits
引用您的元素。
Now, what you should do is just implement a flat array of structs and then pass the pointer to that around.现在,您应该做的只是实现一个平面结构数组,然后将指针传递给它。 Using static allocation, you even get to avoid the
calloc
call.使用 static 分配,您甚至可以避免
calloc
调用。
codificacion matcod_array[256] = { 0 };
codificacion *matcod = (codificacion *)&matcod_array;
Because you're only passing a pointer to an array of structs as opposed to a pointer to an array of pointers to single structs, you would then reference elements in the array using matcod[index].nbits
.因为您只传递指向结构数组的指针,而不是指向指向单个结构的指针数组的指针,所以您将使用
matcod[index].nbits
引用数组中的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.