- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 C 中的链表和指针,并且正在尝试实现一个简单的程序,让用户可以在整数的(单)链表中插入、删除或搜索整数。当我尝试插入数字时,我相当确定我的插入功能正常工作。
但是,我有一个将列表打印到终端的函数,在该函数的执行过程中,插入的节点以某种方式被修改,因此它的整数('n')变成了一些垃圾值,并且它的' next' 指针被更改为指向它自己!然后打印函数继续打印这个节点的“n”值,因为它的“下一个”指针总是指向它自己,但是“n”值不断变化,在第三次迭代中我得到一个段错误(见底部的 gdb session )。
这是我对节点的定义:
typedef struct node
{
int n;
struct node* next;
} node;
这是我的主要功能:
int main(void)
{
// declare a linked list
node* first = NULL;
int command;
int n;
while (true)
{
printf("MENU\n\n");
printf("1 - delete\n");
printf("2 - insert\n");
printf("3 - search\n");
printf("0 - quit\n\n");
printf("Enter a command: ");
scanf("%i", &command);
switch(command)
{
case 0:
printf("Hope u had fun (:\n");
return 0;
case 1:
printf("Number to delete: ");
scanf("%i", &n);
deleteNode(n, &first);
break;
case 2:
printf("Number to insert: ");
scanf("%i", &n);
insertNode(n, &first);
break;
case 3:
printf("Number to search for: ");
scanf("%i", &n);
if (searchList(n, first))
{
printf("Found %i in list!\n", n);
}
else
{
printf("Did not find %i in list :(\n", n);
}
}
printList(first);
}
}
这是我的 insertNode() 函数:
void insertNode(int n, node** first)
{
// declare our new node
node myNode;
myNode.n = n;
myNode.next = NULL;
// initialize curNode to a pointer to the first node in the list
node* curNode = *first;
// initialize a pointer that will point to the previous node in the list if we need it
node* prevNode = NULL;
while (curNode != NULL)
{
if (n <= curNode->n)
{
// if prevNode is null, there's one element in the list
// and we're inserting before it (i.e. at first position)
if (prevNode == NULL)
{
*first = &myNode;
myNode.next = curNode;
return;
}
// else, we're inserting between prevNode and curNode
else
{
prevNode->next = &myNode;
myNode.next = curNode;
return;
}
}
// if n > curNode->n, move on to next node
else
{
curNode = curNode->next;
prevNode = curNode;
}
}
// curNode is null down here, so we're either at the end of the list, or the list is empty
if (prevNode == NULL)
{
// empty list, only have to update first
*first = &myNode;
}
else
{
// end of the list, only have to update previous node
prevNode->next = &myNode;
}
}
这是我的 printList() 函数:
void printList(node* ptr)
{
printf("\nLIST IS NOW: ");
while (ptr != NULL)
{
printf("%i ", ptr->n);
ptr = ptr->next;
}
printf("\n\n");
}
这是一个说明错误的 gdb session :
35 printf("Enter a command: ");
(gdb)
Enter a command: 36 scanf("%i", &command);
(gdb)
2
38 switch(command)
(gdb) n
49 printf("Number to insert: ");
(gdb)
Number to insert: 50 scanf("%i", &n);
(gdb)
1
51 insertNode(n, &first);
(gdb) s
insertNode (n=1, first=0x22fe48) at linked_list.c:78
78 myNode.n = n;
(gdb) n
79 myNode.next = NULL;
(gdb)
82 node* curNode = *first;
(gdb) p &myNode
$1 = (node *) 0x22fdf0
(gdb) n
85 node* prevNode = NULL;
(gdb)
87 while (curNode != NULL)
(gdb) p *first
$2 = (node *) 0x0
(gdb) p curNode
$3 = (node *) 0x0
(gdb) n
116 if (prevNode == NULL)
(gdb)
119 *first = &myNode;
(gdb)
126 }
(gdb) p *first
$4 = (node *) 0x22fdf0
(gdb) n
main () at linked_list.c:52
52 break;
(gdb)
66 printList(first);
(gdb) p first
$5 = (node *) 0x22fdf0
(gdb) p *first
$6 = {n = 1, next = 0x0}
(gdb) s
printList (ptr=0x22fdf0) at linked_list.c:200
200 printf("\nLIST IS NOW: ");
(gdb) p ptr
$7 = (node *) 0x22fdf0
(gdb) p *ptr
$8 = {n = 1, next = 0x0}
(gdb) n
LIST IS NOW: 202 while (ptr != NULL)
(gdb) p ptr
$9 = (node *) 0x22fdf0
(gdb) p *ptr
$10 = {n = 4210908, next = 0x22fdf0}
(gdb) n
204 printf("%i ", ptr->n);
(gdb)
4210908 205 ptr = ptr->next;
(gdb)
202 while (ptr != NULL)
(gdb)
204 printf("%i ", ptr->n);
(gdb)
1397312522 205 ptr = ptr->next;
(gdb)
202 while (ptr != NULL)
(gdb)
204 printf("%i ", ptr->n);
(gdb)
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401864 in printList (ptr=0x2500203a574f4e20) at linked_list.c:204
204 printf("%i ", ptr->n);
正如您在上面看到的,节点在 printList() 函数的中间确实发生了变化。这是怎么/为什么会发生???
最佳答案
我没有仔细查看您写的每一行,就注意到您没有为列表动态分配内存。在 insertNode 函数中,您定义了一个元素,该元素将驻留在堆栈中:
node myNode;
如果函数离开了,内存就“消失了”。这意味着您无权访问它。但是你用
把它传回主上下文*first = &myNode;
函数必须自己分配内存(例如使用 malloc)。为了使函数更简单,不要将双指针传递给该函数。
相反,delteNode 函数必须将内存还给操作系统(例如使用 free)。同样在这里:不要传递指针的地址,而只传递应删除元素的位置(指针)。
关于C - 链表中的节点以某种方式修改,导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370447/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!