对结构使用 malloc 时出错
Error when using malloc for a struct
我正在为家庭作业创建哈希 [=28=]。我正在使用 Xcode 并且它没有任何即时错误但是当我尝试 运行 它时,当涉及到最初的函数时,我得到了其中一个 Thread 1:EXC_BAD_ACCESS 错误创建散列 table 并为其分配。特别是使用 malloc 的行:
Node **ht_create(void)
{
Node **hashtable[htsize];
int Node_size = sizeof(Node);
if( htsize < 1 )
{
printf("Error: please enter adequate size for hashtable");
return NULL;
}
for (int i = 0; i<=htsize;i++)
{
hashtable[i] = malloc(Node_size);
}
if( ( **hashtable) == NULL )
{
printf("Error: could not allocate memory for hashtable");
return NULL;
}
return *hashtable;
}
我对 C 还是很陌生,我 2 个月前才开始学习它,所以如果有明显的问题而我只是个白痴,请多多包涵。 htsize
是在别处声明的整数命令行参数。我不知道它是否有帮助,但这是定义我的 'Node' 结构的代码:
struct NodeType
{
char *key;
int value;
struct NodeType *next;
};
typedef struct NodeType Node;
在黑暗中拍摄:这与我在结构中有指针这一事实有关吗? struct NodeType *next;
部分?
任何帮助将不胜感激,提前致谢。
编辑:我现在有了它,所以它使用 for 循环为 table 中的每个节点分配,但它返回我的 "Error: could not allocate memory for hashtable" 所以我的散列 table 现在 = NULL ?
**hashtable = malloc(Node_size * htsize);
这是两次取消引用哈希表,然后将其分配给由 malloc returned 的指针。您应该将指针哈希表分配给由 malloc 编辑的指针 return。
例如
hashtable = malloc(Node_size * htsize);
编辑: 我重读了你的代码。您不应该将哈希表声明为数组。数组的存储时长是本地的;一旦函数 returns,数组被释放。无论如何,您只是 return 第一个元素,所以这可能只是一个错字。
你也应该 return hashtable
,而不是 return *hashtable
更新: 您已经更改了为哈希表数组的每个元素分配一个节点的方法,但您只是 returning 的第一个元素那个数组。
Node **hashtable[htsize];
应该改为 Node **hashtable;
您不能 return 具有自动存储的数组 class,因此将其声明为数组似乎是一个错误。
此外,您只是 return 这个数组的第一个元素,所以这会导致内存泄漏 return *hashtable
等同于 return hashtable[0]
。
我认为这就是您要寻找的行为:
Node **ht_create(void)
{
Node **hashtable;
if(htsize < 1)
{
printf("Error: please enter adequate size for hashtable");
return NULL;
}
hashtable = malloc(htsize * sizeof(Node*)) //allocate the array
if(hashtable == NULL )
{
printf("Error: could not allocate memory for hashtable");
return NULL;
}
for (int i = 0; i < htsize; i++)
{
hashtable[i] = malloc(sizeof(Node)); //allocate each node in the array
if(hashtable[i] == NULL) //you have to null check these too
{
printf("Error: could not allocate memory for hashtable");
return NULL;
}
}
return hashtable; //return the array of node pointers
}
如果分配失败,您可能应该取消分配所有其他节点,除非您此时无论如何都要退出程序。您可以随心所欲地解决这个问题,因为这取决于您如何实现其余代码。
不完全清楚 OP 的意图,但我希望 Node **ht_create(void)
创建 return 指向 Node *
数组的指针,所以 return 类型 Node **
.然后数组元素将被初始化为 NULL
因为每个散列 table 桶的开头是一个空列表。
提示:分配内存的简单方法。
Some_type *pointer = malloc(sizeof *pointer * array_size);
示例代码:
Node **ht_create(size_t htsize) { // pass in size
if (htsize < 1) {
printf("Error: please enter adequate size for hashtable");
return NULL;
}
//allocate hashtable
Node **hashtable = malloc(sizeof *hashtable * htsize);
if (hashtable == NULL) {
printf("Error: could not allocate memory for hashtable");
return NULL;
}
// Initialize values
for (size_t i=0; i<htsize; i++) {
hashtable[i] = NULL;
}
return hashtable;
}
我正在为家庭作业创建哈希 [=28=]。我正在使用 Xcode 并且它没有任何即时错误但是当我尝试 运行 它时,当涉及到最初的函数时,我得到了其中一个 Thread 1:EXC_BAD_ACCESS 错误创建散列 table 并为其分配。特别是使用 malloc 的行:
Node **ht_create(void)
{
Node **hashtable[htsize];
int Node_size = sizeof(Node);
if( htsize < 1 )
{
printf("Error: please enter adequate size for hashtable");
return NULL;
}
for (int i = 0; i<=htsize;i++)
{
hashtable[i] = malloc(Node_size);
}
if( ( **hashtable) == NULL )
{
printf("Error: could not allocate memory for hashtable");
return NULL;
}
return *hashtable;
}
我对 C 还是很陌生,我 2 个月前才开始学习它,所以如果有明显的问题而我只是个白痴,请多多包涵。 htsize
是在别处声明的整数命令行参数。我不知道它是否有帮助,但这是定义我的 'Node' 结构的代码:
struct NodeType
{
char *key;
int value;
struct NodeType *next;
};
typedef struct NodeType Node;
在黑暗中拍摄:这与我在结构中有指针这一事实有关吗? struct NodeType *next;
部分?
任何帮助将不胜感激,提前致谢。
编辑:我现在有了它,所以它使用 for 循环为 table 中的每个节点分配,但它返回我的 "Error: could not allocate memory for hashtable" 所以我的散列 table 现在 = NULL ?
**hashtable = malloc(Node_size * htsize);
这是两次取消引用哈希表,然后将其分配给由 malloc returned 的指针。您应该将指针哈希表分配给由 malloc 编辑的指针 return。
例如
hashtable = malloc(Node_size * htsize);
编辑: 我重读了你的代码。您不应该将哈希表声明为数组。数组的存储时长是本地的;一旦函数 returns,数组被释放。无论如何,您只是 return 第一个元素,所以这可能只是一个错字。
你也应该 return hashtable
,而不是 return *hashtable
更新: 您已经更改了为哈希表数组的每个元素分配一个节点的方法,但您只是 returning 的第一个元素那个数组。
Node **hashtable[htsize];
应该改为 Node **hashtable;
您不能 return 具有自动存储的数组 class,因此将其声明为数组似乎是一个错误。
此外,您只是 return 这个数组的第一个元素,所以这会导致内存泄漏 return *hashtable
等同于 return hashtable[0]
。
我认为这就是您要寻找的行为:
Node **ht_create(void)
{
Node **hashtable;
if(htsize < 1)
{
printf("Error: please enter adequate size for hashtable");
return NULL;
}
hashtable = malloc(htsize * sizeof(Node*)) //allocate the array
if(hashtable == NULL )
{
printf("Error: could not allocate memory for hashtable");
return NULL;
}
for (int i = 0; i < htsize; i++)
{
hashtable[i] = malloc(sizeof(Node)); //allocate each node in the array
if(hashtable[i] == NULL) //you have to null check these too
{
printf("Error: could not allocate memory for hashtable");
return NULL;
}
}
return hashtable; //return the array of node pointers
}
如果分配失败,您可能应该取消分配所有其他节点,除非您此时无论如何都要退出程序。您可以随心所欲地解决这个问题,因为这取决于您如何实现其余代码。
不完全清楚 OP 的意图,但我希望 Node **ht_create(void)
创建 return 指向 Node *
数组的指针,所以 return 类型 Node **
.然后数组元素将被初始化为 NULL
因为每个散列 table 桶的开头是一个空列表。
提示:分配内存的简单方法。
Some_type *pointer = malloc(sizeof *pointer * array_size);
示例代码:
Node **ht_create(size_t htsize) { // pass in size
if (htsize < 1) {
printf("Error: please enter adequate size for hashtable");
return NULL;
}
//allocate hashtable
Node **hashtable = malloc(sizeof *hashtable * htsize);
if (hashtable == NULL) {
printf("Error: could not allocate memory for hashtable");
return NULL;
}
// Initialize values
for (size_t i=0; i<htsize; i++) {
hashtable[i] = NULL;
}
return hashtable;
}