简体   繁体   English

Windows中的简单C程序崩溃

[英]Simple c program crashes in windows

I am new to cI Have the following code which creates a double linked list. 我是cI的新手,有以下代码可以创建双链表。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct Dnode
{
    char c;
    struct Dnode *left;
    struct Dnode *right;
}Dnode;
void insert(Dnode *,char);
void unshift(Dnode *,char);
void travel(Dnode *);
int main(){
    Dnode *cur =  (Dnode *)malloc(sizeof(Dnode));
    Dnode *head = NULL;
    head = cur;

    cur -> c = 'a';
    printf("Cur -> c: %c\n",cur->c);
    cur ->left = NULL;
    cur -> right = (Dnode *)malloc(sizeof(Dnode));
    cur->right->c = 'b';
    cur->right->left = cur;
    cur = cur->right;
    travel(head);
    system("pause");
    return 0;
}
void reset(Dnode *h){
    while(h->left)
        h=h->left;
}
void travel(Dnode *head){
    printf("Traversing all nodes of list:\n");
    while(head->right){
        printf("Received char from node %c\n",head->c);
        head = head->right;
    }
    //reset(head);
}
void insert(Dnode * d,char c){
    Dnode *t = d;
    while(t ->right)
        t=t->right;
    t->right = (Dnode *)malloc(sizeof(Dnode));
    if(t->right){
        t->right->c = c;
        t= t->right;
        t->right = t->left = NULL;
    }
}
void unshift(Dnode *d,char cc){
    Dnode *t =(Dnode *)malloc(sizeof(Dnode));
    t =  d->right;
    t->left =NULL;
    d->left = t;
    d = t;
}       

The problem is that after travel() is called all the nodes are printed out. 问题在于,在调用travel()之后,将打印所有节点。 It prints "Received char from node a" and "Received char from node b" 打印“从节点a接收到的字符”和“从节点b接收到的字符”

but then Windows is giving me an error that says that the program has crashed.Any ideas?I would like a detailed answer so as to avoid similar problems in the future. 但是Windows却给我一个错误,指出程序已崩溃。有任何想法吗?我想提供详细的答案,以避免将来出现类似的问题。

After initialization your list looks like: 初始化后,您的列表如下所示:

   head ------>  X  ------> Y ------> uninitialized
               / ^         /
              /  |        /
             /   \       /
     null<---     -------

So in the travel function you start by printing Xc in the first loop and then use the uninitialized pointer in the second loop. 因此,在travel函数中,您首先在第一个循环中打印Xc ,然后在第二个循环中使用未初始化的指针。 That cause a program crash. 这会导致程序崩溃。

So you need to add: 因此,您需要添加:

cur->right->c = 'b';
cur->right->left = cur;
cur->right->right = NULL   // Add this line

to the initialization. 进行初始化。

Also notice that you don't free the allocated resources. 另请注意,您不会free分配的资源。 So in main (just before return ) you should do: 因此,在main (就在return之前),您应该执行以下操作:

Dnode *tmp;
while(head){
    tmp = head;
    head = head->right;
    free(tmp);
}

BTW - Don't cast the value returned by malloc . 顺便说一句-不要转换malloc返回的值。 Just do Dnode *cur = malloc(sizeof(Dnode)); 只需执行Dnode *cur = malloc(sizeof(Dnode));

BTW - this line: 顺便说一句-此行:

cur = cur->right;

can be deleted as cur is not used afterwards. 可以删除,因为此后不再使用cur

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM