如何修复由 malloc 创建的链表中设置结束节点导致分段错误的问题?
我正在学习动态内存管理,并且正在开发一个程序,该程序可以让您设置数组的大小,然后生成它并打印出来。
这是代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int number;
struct node *next;
}node;
int main(void)
{
node *ptr;
int n, i;
printf("How big should the array initially be? ");
//scanf("%i",&n);
printf("\n\n");
// for debugging purposes
n = 8;
ptr = (node *)malloc(n * sizeof(node));
for (i = 0; i < n; i++)
{
ptr[i] = (struct node)
{
.number = i + 1,
.next = &ptr[i + 1]
};
}
struct node *listptr = &ptr[0];
ptr[n - 1].next = NULL;
/*while (listptr->next != NULL)
{
printf("ptr[%i].number = %i, ptr[%i].next->number = %i"
,i,ptr[i].number,i,ptr[i].next->number);
listptr = listptr->next;
i++;
}*/
for (i = 0; i < n; i++)
{
printf("ptr[%i].number = %i, ptr[%i].next->number = %i\n"
,i,ptr[i].number,i,ptr[i].next->number);
}
printf("How much bigger would you like this array to be? ");
printf("\n\n");
free(ptr);
}
我尝试将 malloc 更改为 calloc 但仍然遇到相同的错误。如何将结束node.next设置为NULL并终止程序? 当我运行代码时,我得到以下输出:
How big should the array initially be?
ptr[0].number = 1, ptr[0].next->number = 2
ptr[1].number = 2, ptr[1].next->number = 3
ptr[2].number = 3, ptr[2].next->number = 4
ptr[3].number = 4, ptr[3].next->number = 5
ptr[4].number = 5, ptr[4].next->number = 6
ptr[5].number = 6, ptr[5].next->number = 7
ptr[6].number = 7, ptr[6].next->number = 8
zsh: segmentation fault ./file
I am learning dynamic memory management and am working on a program that lets you set the size of the array, and then generates it, and prints it out.
here is the code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int number;
struct node *next;
}node;
int main(void)
{
node *ptr;
int n, i;
printf("How big should the array initially be? ");
//scanf("%i",&n);
printf("\n\n");
// for debugging purposes
n = 8;
ptr = (node *)malloc(n * sizeof(node));
for (i = 0; i < n; i++)
{
ptr[i] = (struct node)
{
.number = i + 1,
.next = &ptr[i + 1]
};
}
struct node *listptr = &ptr[0];
ptr[n - 1].next = NULL;
/*while (listptr->next != NULL)
{
printf("ptr[%i].number = %i, ptr[%i].next->number = %i"
,i,ptr[i].number,i,ptr[i].next->number);
listptr = listptr->next;
i++;
}*/
for (i = 0; i < n; i++)
{
printf("ptr[%i].number = %i, ptr[%i].next->number = %i\n"
,i,ptr[i].number,i,ptr[i].next->number);
}
printf("How much bigger would you like this array to be? ");
printf("\n\n");
free(ptr);
}
I have tried changing malloc to calloc but I still get the same error. How do I set the ending node.next to NULL and terminate the program?
when I run the code I get this output:
How big should the array initially be?
ptr[0].number = 1, ptr[0].next->number = 2
ptr[1].number = 2, ptr[1].next->number = 3
ptr[2].number = 3, ptr[2].next->number = 4
ptr[3].number = 4, ptr[3].next->number = 5
ptr[4].number = 5, ptr[4].next->number = 6
ptr[5].number = 6, ptr[5].next->number = 7
ptr[6].number = 7, ptr[6].next->number = 8
zsh: segmentation fault ./file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此 for 循环中
,当
i
等于 tpn - 1
时,此表达式ptr[i].next->number
尝试使用以下方式访问内存空指针ptr[i].next
。同样在这个 for 循环中
你必须写
In this for loop
when
i
is equal tpn - 1
this expressionptr[i].next->number
tries to access memory using the null pointerptr[i].next
.Also in this for loop
you have to write
今天的第一个链接列表。这很简单,
因为列表中的最后一个条目
ptr[i].next
是 NULL,所以这失败了如何修复,
这会打印 0 而不是崩溃。问题是 - 您想为最后一个条目打印什么
first linked list today. This ones easy
For the last entry in the list
ptr[i].next
is NULL, so this failsHow to fix,
this prints 0 instead of crashing. Question is - what do you want to print for the last entry