链接列表指针打印内存泄漏

时间:2014-12-30 19:15:51

标签: c pointers struct linked-list

错误是它打印出内存而不是每个节点的值。我尝试了所有指针组合和不同的打印样式,但它们都显示为内存泄漏。

这是我的代码:

#include <stdio.h>//headers
#include <stdlib.h>


struct node{
    int val;
    struct node *next;
};//linked list

struct node *curr = NULL;//list pointers
struct node *prev = NULL;
struct node *head = NULL;

int main(){

    int i;

    struct node *curr = (struct node*) malloc(sizeof(struct node));
    head=curr;//sets head node

    for (i=1;i<=5;i++){

        curr->val=i;//sets data
        struct node *prev = (struct node*) malloc(sizeof(struct node));
        curr->next=prev;
        curr=prev;//links to previous node

        printf("%d\n", *curr);//prints out data
    }
    return 0;
}

5 个答案:

答案 0 :(得分:3)

嗯,你的程序中没有调用free,所以是的,每次调用malloc在技术上都是泄密。也就是说,我认为你在这里混淆了你的条款。

如果要打印出节点的值,请使用cur->val,而不是*cur*cur会返回struct node,您告诉printf将其解释为int

答案 1 :(得分:1)

您应该像这样直接打印访问它的值

printf("%d\n", curr->val);

答案 2 :(得分:1)

另外,您没有在任何时候设置prev值,因此curr = prev;然后printf("%d\n", curr->val);只会打印垃圾。

答案 3 :(得分:0)

除了您访问val字段的方式外,还有一些问题,评论时应该是curr->val

您的程序经过如此更正后会打印垃圾,因为您只有在将curr指向prev后才会打印,并且val未初始化。

curr->val = i;
...
curr = prev;
printf ("%d\n", curr->val);

此外,您已在全局和函数内声明了currprev

我简化了这一点,在创建后打印列表。如果您想要不同的序列,请反转i循环。注意我已删除prev

#include <stdio.h>
#include <stdlib.h>

struct node{
    int val;
    struct node *next;
};

int main(){
    int i;
    struct node *curr, *head = NULL;

    // build the list
    for (i=1; i<=5; i++) {
        curr = malloc(sizeof(struct node));
        curr->val = i;
        curr->next = head;
        head = curr;
    }

    // print the list
    curr = head;
    while (curr) {
        printf ("%d\n", curr->val);
        curr = curr->next;
    }
    return 0;

}

在使用之前,您应该检查malloc()的返回值。

答案 4 :(得分:0)

the following code compiles cleanly 
caveat: I have not run this code

#include <stdio.h>//headers
#include <stdlib.h>


struct node{
    int val;
    struct node *next;
};//linked list

struct node *curr = NULL;//list pointers
struct node *prev = NULL;
struct node *head = NULL;

int main()
{

    int i;


    for (i=1;i<6;i++)
    {
        // get block
        if( NULL == (curr = malloc(sizeof(struct node)) ) )
        { // then, malloc failed
            perror( "malloc failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, malloc successful

        // fill in fields
        curr->val = i;
        curr->next = NULL;

        // display val
        printf("%d\n", curr->val);//prints out data

        if( NULL == head)
        { // then, first node
            head = curr;
            prev = curr;
        }

        else
        { // else, after first node
            // link in new node
            prev->next = curr; // link in new node
        } // end if
    } // end while

    prev = head;
    curr = prev;
    while( curr )
    { // then, node to free
        curr = prev->next;
        free(prev);
        prev = NULL;
    } // end while

    return 0;
} // end function: main