C-将元素添加到链表数组中的奇数索引(但不是偶数)时的内存泄漏
我有一个名为node的简单链表结构、一个head节点数组、一个linkedListManage函数和一个addNode函数,但是我希望您能帮助我诊断我不了解的segfault问题 我在main中向语句添加节点时跟踪了该问题:C-将元素添加到链表数组中的奇数索引(但不是偶数)时的内存泄漏,c,memory-leaks,linked-list,segmentation-fault,C,Memory Leaks,Linked List,Segmentation Fault,我有一个名为node的简单链表结构、一个head节点数组、一个linkedListManage函数和一个addNode函数,但是我希望您能帮助我诊断我不了解的segfault问题 我在main中向语句添加节点时跟踪了该问题: while(1) linkedListManage(*heads); 虽然包含指向头的指针,但每当我将第二个元素(而不是第一个)添加到存储在链接列表头数组中的奇数索引(而不是偶数索引)中的任何链接列表头时,我都会得到一个segfault。我已经用头的数组大小20测试了这个
while(1) linkedListManage(*heads);
虽然包含指向头的指针,但每当我将第二个元素(而不是第一个)添加到存储在链接列表头数组中的奇数索引(而不是偶数索引)中的任何链接列表头时,我都会得到一个segfault。我已经用头的数组大小20测试了这个问题,这个问题仍然只影响奇数索引
#include <stdio.h>
#include <stdlib.h>
typedef struct node_t{
int data;
struct node_t* next;
} node;
void addNode(node* a){
int userInput;
node* current = a;
printf("Enter value: ");
scanf("%d", &userInput);
if (!current->data) current->data = userInput;
else {
while(current->next) current = current->next;
node* newNode = (node*)malloc(sizeof(node));
newNode->data = userInput;
current->next = newNode;
}
}
void linkedListManage(node* heads){
int userInput;
printf("Enter ID of list:\n");
scanf("%d", &userInput);
addNode(&heads[userInput]);
}
int main(){
node* heads[2];
heads[0] = (node*)malloc(sizeof(node));
heads[1] = (node*)malloc(sizeof(node));
while(1) linkedListManage(heads);
return 0;
}
但是,当我删除指向头的指针时:
while(1) linkedListManage(heads);
这将删除segfault错误,并允许正确添加我的列表,但随后会出现两个警告:
main.c:42:27: warning: incompatible pointer types passing 'node *[2]' to parameter of type
'node *' (aka 'struct node_t *') [-Wincompatible-pointer-types]
while(1) linkedListManage(heads);
^~~~~
main.c:27:27: note: passing argument to parameter 'heads' here
void linkedListManage(node* heads){
下面是我的程序,我真的很想知道是什么导致了这个问题,这样我就可以了解更多关于指针问题的信息,这样我就可以停止出现警告了。我完全搞不懂为什么这个问题只会出现在奇怪的指数上
#include <stdio.h>
#include <stdlib.h>
typedef struct node_t{
int data;
struct node_t* next;
} node;
void addNode(node* a){
int userInput;
node* current = a;
printf("Enter value: ");
scanf("%d", &userInput);
if (!current->data) current->data = userInput;
else {
while(current->next) current = current->next;
node* newNode = (node*)malloc(sizeof(node));
newNode->data = userInput;
current->next = newNode;
}
}
void linkedListManage(node* heads){
int userInput;
printf("Enter ID of list:\n");
scanf("%d", &userInput);
addNode(&heads[userInput]);
}
int main(){
node* heads[2];
heads[0] = (node*)malloc(sizeof(node));
heads[1] = (node*)malloc(sizeof(node));
while(1) linkedListManage(heads);
return 0;
}
#包括
#包括
类型定义结构节点{
int数据;
结构节点*next;
}节点;
void addNode(节点*a){
int用户输入;
节点*电流=a;
printf(“输入值:”);
scanf(“%d”、&userInput);
如果(!current->data)current->data=userInput;
否则{
而(当前->下一步)当前=当前->下一步;
node*newNode=(node*)malloc(sizeof(node));
新建节点->数据=用户输入;
当前->下一步=新节点;
}
}
void linkedListManage(节点*头){
int用户输入;
printf(“输入列表的ID:\n”);
scanf(“%d”、&userInput);
addNode(&heads[userInput]);
}
int main(){
节点*头[2];
heads[0]=(node*)malloc(sizeof(node));
heads[1]=(node*)malloc(sizeof(node));
而(1)linkedListManage(负责人);;
返回0;
}
应该是
while(1) linkedListManage(heads[1]);
否则,您将把节点**
传递给需要节点*
的函数
应该是
while(1) linkedListManage(heads[1]);
否则,将
node**
传递给一个函数,该函数需要node*
警告告诉您需要知道的一切。您实际上是在尝试将节点**传递给接受节点*的函数。您的linkedListManage()
函数应该使用节点*heads[2]
或节点**
才能正常工作。此外,如果没有某种健全性检查,您永远不应该接受用户输入。如果用户输入20而不是1或2,你认为会发生什么?哦,我明白了,谢谢!这就解决了问题。也是的,我确实有输入验证,但我已经将代码库分解为只需进行故障排除的错误区域,这篇文章要简化:)警告告诉您需要知道的一切。您实际上是在尝试将节点**传递给接受节点*的函数。您的linkedListManage()
函数应该使用节点*heads[2]
或节点**
才能正常工作。此外,如果没有某种健全性检查,您永远不应该接受用户输入。如果用户输入20而不是1或2,你认为会发生什么?哦,我明白了,谢谢!这就解决了问题。也是的,我确实有输入验证,但我已经将代码库分解为只需要故障排除和简化的错误区域:)