我试图在c语言中实现一个单链链表,我想能够使用这个链表的多个实例,我想在主函数中创建这个链表。这就是为什么我选择用我的方式来实现它。
代码工作得很好,但我担心的是valgrind创建的输出。另外,我试图在一个嵌入式系统上的项目中使用这些代码,结果出现了奇怪的错误。
valgrind的输出是:
启动。。。
==3570==条件跳转或移动取决于未初始化的值
==3570==0x100000E8E:推前(in./map_test)
==3570==by 0x100000D4F:main(in./map_测试)
==3570==堆分配创建了未初始化的值
==3570==0x100008EBB:malloc(in/usr/local/celler/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==3570==by 0x100000E80:推前(in./map_test)
==3570==by 0x100000D4F:main(in./map_测试)
==3570==
…完成
它还告诉我,我正在失去一个街区。我在哪里犯了错误
==3570==泄漏摘要:
==3570==绝对丢失:1个块中有16个字节
==3570==间接丢失:0块中的0字节
==3570==可能丢失:1个块中2064字节
==3570==仍然可以访问:0块中的0字节
==3570==抑制:186个块中的24525字节
请告诉我哪里出错了。
测试c:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "command_list.h"
int main() {

    printf("starting...\n");

    Clist * command_list = malloc(sizeof(Clist));
    if (command_list == NULL) printf("Malloc Failed\n");
    command_list->head = NULL;

    //push_cback(command_list, 0);
    push_cfront(command_list,1);

    free_clist(command_list);
    free(command_list);
    printf("\n...finished\n");
    return 0;
}

命令列表.h:
#ifndef __COMMAND_LIST_H
#define __COMMAND_LIST_H

typedef struct cnode {
    uint8_t command;
    struct cnode * next;
} Cnode;

typedef struct clist {
    Cnode * head;
} Clist;

void push_cback(Clist * list, uint8_t command);
void push_cfront(Clist * list, uint8_t command);
void free_clist(Clist * list);

#endif

命令列表.c
void push_cfront(Clist * list, uint8_t command){
    Cnode * new_node;
    new_node = malloc(sizeof(Cnode));
    if (new_node->next == NULL) {
        return;
    }
    new_node->command = command;
    new_node->next = list->head;
    list->head = new_node;
}

void free_clist(Clist * list){
    if (list->head == NULL){
        return; //already empty
    }
    Cnode * current = list->head;
    while (current->next != NULL){
        Cnode* temp = current->next;
        free(current);
        current = temp;
    }
    free(current);
    list->head = NULL;
}

最佳答案

你有一些问题。您正在检查new_node->next(malloc的内存中未初始化的数据),而不是new_node(malloc的返回值)。至少在我的计算机上,这也会导致内存无法释放,因为碰巧new_node->next为空,所以返回时不释放new_node。另外,如果你想支持推到链表的后面,你应该考虑一个循环链表,因为它允许这个操作而不必遍历整个链表。
最后,一些提示:使用valgrind是很好的,但是如果使用-g编译以启用调试符号,那么valgrind将告诉您行号,这将更有帮助。另外,当我创建链表时,我喜欢对某些操作使用一个虚拟头节点,以避免空列表或单例列表出现特殊情况。对于插入到已排序的链接列表中,该技术如下所示:

int sorted_insert(Clist *list, char new_command){
    Cnode _head = {NULL, list->head}, *head = &_head, *prev = head, *tmp;//head is an auto dummy node obviating null checks.
    int ord = -1;//If there are no existing nodes, newObj would be less than all objects.
    while(prev->next && (ord = (int)newObj - prev->next->command)) > 0){//Iterate by prev->next not curr to use only one pointer.
        prev = prev->next;//Looping while there is a next node and its data compares less than new_command.
    }
    if((!ord) || !(tmp = malloc(sizeof(Cnode))){//newObj is already in the list or allocation failed.
        return 0;
    }
    *tmp = (Cnode){.next=prev->next, .command=new_command};
    prev->next = tmp;
    list->head = head->next;//un- add head which is then deallocated by stack frame cleanup.
    return 1;
}

10-06 01:27