c에서 단일 링크 된 연결 목록을 구현하려고했습니다. 나는 목록의 여러 인스턴스를 사용할 수 있기를 원했고 main 함수에서 목록을 만들고 싶었다. 그래서 내가 그랬던 것처럼 그것을 구현하기로 결정했습니다.C malloc valgrind - 단일 링크 된 목록 구현의 초기화되지 않은 메모리 구현
코드는 완벽하게 작동하지만 valgrind 출력으로 인해 걱정이됩니다. 또한 임베디드 시스템에서 프로젝트의 코드를 사용하려고 시도했지만 이상한 오류가 발생합니다.
Valgrind의 출력은 : 시작
...
== 3570 == 조건부 점프 또는 이동 0x100000E8E에서 초기화되지 않은 값 (들)
== 3570 ==에 따라 : 0x100000D4F 의해
== 3570 == (./map_test)에 push_cfront : 주 (./map_test)에
== 3570 == 의 malloc (/usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)에
: 초기화되지 않은 값이 힙 할당 0x100008EBB에서== 3570 == 의해 만들어진
== 0x100000E80 의해 3570 == push_cfront (./map_test)에 의해 0x100000D4F
== == 3570 : 주 (./map_test)에
== 3570 ==
. 완료 됨
또한 하나의 블록을 잃어 버렸다고 나에게 알려줍니다. 나는 그것을 해제 실수를 할 경우
== 3570 == 누출 요약 : 16 바이트를 1 개 블록
== 3570 == 간접적 손실에 : 3570 == 확실히 손실
== : 2064 바이트를 1 개 블록 3570 == 아직 도달
==에 : 0 블록 3570 == 가능성이 손실
== 0 바이트 0 바이트 0 블록
== 3570 == 억압 : 186 블록의 24,525 바이트
어디서 잘못되었는지 힌트를주세요.
TEST.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;
}
command_list.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
command_list.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-> == NULL 옆)'그냥'그 일을 malloc'ed 경우'당신'할; 정렬 된 연결리스트에 삽입은 해당 기술은 같다 'next'는 설정되지 않습니다. 확실히'if (new_node == NULL)'을 사용하기로했다. – WhozCraig
아, Valgrind를 사용하기위한 소품. – WhozCraig
나는'list == NULL'을 확인해야한다고 생각합니다. – niceman