初始化结构指针给出分段故障
我一直在尝试在for循环中初始化链接列表。每次迭代,我都会创建一个指向节点结构的指针,并指向列表中的最后一个节点。但是,发生的奇怪的事情是,当试图将结构的数据字段的值分配给5(for循环中的第二行)时,我一直会遇到细分故障。不知道为什么会发生这种情况。
struct node{
int data;
node* next;
};
void CreateLinkedList(node* start, int numberOfNodes)
{
int i = 0;
node* tempo = start;
for(i=0; i<numberOfNodes; i++){
node* newNode;
newNode->data = 5;
newNode->next = NULL;
while(tempo->next != NULL){
tempo = tempo->next;
}
tempo->next = newNode;
}
}
我尝试过的其他事情是使用“新”操作员,它起作用(再次不确定为什么):
void CreateLinkedList(node* start, int numberOfNodes)
{
int i = 0;
node* tempo = start;
node* newNode;
for(i=0; i<numberOfNodes; i++){
newNode = new node;
newNode->data = 5;
newNode->next = NULL;
while(tempo->next != NULL){
tempo = tempo->next;
}
tempo->next = newNode;
}
}
有什么想法吗?
PS:这是我在这里的第一篇文章!
I've been trying to initialise a linked list in a for loop. Every iteration, I create a pointer to a node struct and point the last node in the list to it. However, something strange that happens is I keep getting a segmentation fault when trying to assign the value of the data field of the struct to 5 (second line in the for loop). Not sure why this is happening.
struct node{
int data;
node* next;
};
void CreateLinkedList(node* start, int numberOfNodes)
{
int i = 0;
node* tempo = start;
for(i=0; i<numberOfNodes; i++){
node* newNode;
newNode->data = 5;
newNode->next = NULL;
while(tempo->next != NULL){
tempo = tempo->next;
}
tempo->next = newNode;
}
}
Something else I tried was using the "new" operator and it worked (again, not sure why):
void CreateLinkedList(node* start, int numberOfNodes)
{
int i = 0;
node* tempo = start;
node* newNode;
for(i=0; i<numberOfNodes; i++){
newNode = new node;
newNode->data = 5;
newNode->next = NULL;
while(tempo->next != NULL){
tempo = tempo->next;
}
tempo->next = newNode;
}
}
Any ideas?
PS: This is my very first post here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个功能都是不正确的。在第一个函数中,您至少使用具有不确定值的非初始化指针newNode来访问内存,
即使在第二个函数中使用新运算符
也不会使该函数正确。
用户可以将零指针传递给该功能(当列表为空时)。在这种情况下,您遇到了使用空指针访问内存的问题,
可以声明并定义以下方式
The both functions are incorrect. In the first function you are using at least the uninitialized pointer newNode with an indeterminate value to access memory that invokes undefined behavior
Nevertheless even using the operator new in the second function
does not make the function correct.
The user can pass a null pointer to the function (when the list is empty). In this case you have the same problem of accessing memory using a null pointer
The function can be declared and defined the following way
我同意弗拉德。首先,当您应该在构造结构之后设置数据时,您只是在创建该结构的新指针。这意味着您的内存尚未分配,您正在尝试进入不可访问的内存区域。为了解决您需要创建某些构造函数(或使用默认值),然后使用新的 +构造函数创建对象。可以是一些幼稚的版本。
另外,正如弗拉德(Vlad)告诉您的那样,请注意该段条款很重要,因为该功能可以收到一些无效的价值,因此也要考虑一下。
I'm agree with Vlad. First of all when you are supposed to set data after construct the struct, and you just are creating a new pointer of that struct. that means that your memory is not already allocated and you are trying to access into a non-accessible region of memory. In order to solve that you need to create some constructor (or use the default), and then create the object with the new + the constructor. Some naïve version can be.
Also as Vlad told you it's important to take care of the while clause since the function can receive some null value, so take care of that too.
C ++具有初始化结构的构造函数,因此它们从不处于某种不确定的状态。所以使用它们。
然后,这简化了功能:
记住列表的尾巴以便您可以直接附加到它会更快。
C++ has constructors to initialize structures so they are never in some undefined state. So use them.
This then simplifies the function:
It would be much faster though to remember the tail of the list so you could append to it directly.