【发布时间】:2021-03-17 00:14:52
【问题描述】:
我的 calloc 有问题,但我不知道为什么。这是我的代码:
void ens_init(ensemble* node, ullong value, uint i){
// *node = malloc(sizeof(struct ensemble)); # Doesn't work
// *node = calloc(1, sizeof(struct ensemble)); # Doesn't work
node = calloc(1, sizeof(struct ensemble));
if (*node == NULL){
printf("Caloc error\n");
exit(-1);
}
(*node)->key = value;
(*node)->index = i;
(*node)->left = NULL;
(*node)->right = NULL;
}
这是我的整体结构:
typedef unsigned int uint;
typedef unsigned long long int ullong;
struct ensemble{
ullong key;
uint index;
struct ensemble* left;
struct ensemble* right;
};
typedef struct ensemble* ensemble;
在研究非确定性有限自动化(法语中的 NFA 或 AFN)时,这就是我使用这种结构的原因。我的老师想编写一个确定 NFA 的函数,在这个函数中我们必须使用树。
这是我如何调用这个函数来测试它
int main(int argc, char *argv[]){
ensemble B = NULL;
ens_ajouter(&B, 5, 1);
return 0;
}
感谢您的帮助:)
【问题讨论】:
-
两条 cmets 行中的任何一条都是正确的。未注释的不是。您需要定义“不起作用”的含义,并且您需要使用显示问题的minimal reproducible example 更新您的问题。
标签: c struct segmentation-fault calloc nfa