다음 함수에서 연결된 목록의 머리글 구조에 대한 포인터를 반환하고 싶습니다. fread가 struct의 문자열을 읽을 수 없기 때문에 malloc을 사용해야합니다. 몇몇 변수와 newheader-> name 문자열을 가리킨다. 다음 함수에서 임시 변수를 해제하면 반환 된 새 헤더는 null 값만 출력하고, 그렇지 않으면 메모리 누수가 발생하지만 함수는 정상적으로 작동합니다. NULL 값이 아닌 완전한 링크 된 목록의 머리를 반환 할 수 있기를 원합니다.fread는 C의 이진 파일에서 struct의 문자열 값을 읽을 수 없습니다
struct head *read_strings(char *file) {
FILE *my_file;
struct head * newheader = NULL;
char *str = NULL;
newheader = buildhead();
int len, lenStr, lenTotal = 0;
printf("\n\n");
my_file = fopen(filename, "rb");
if (my_file == NULL) {
printf("error\n");
}
fread(&len,sizeof(int),1, my_file);
newheader->name = malloc(len);
fread(newheader->name,sizeof(char),len, my_file);
fread(&newheader->length,sizeof(int),1, my_file);
if (newheader->length != 0) {
if (newheader->length == lenTotal) {
fread(&lenStr,sizeof(int),1, my_file);
str = malloc(lenStr); //char *str = malloc(lenStr);
fread(str,sizeof(char),lenStr, my_file);
create_string(newheader, str);
lenTotal += lenStr;
str = NULL; //free(str);
}
else {
while (newheader->length != lenTotal) {
fread(&lenStr,sizeof(int),1, my_file);
str = malloc(lenStr); //char *str = malloc(lenStr);
fread(str,sizeof(char),lenStr, my_file);
create_string(newheader, str);
lenTotal += lenStr;
str = NULL; //free(str);
}
}
}
printString(newheader);
fclose(my_file);
free(str);
//if i free newheader->name here then I get no memory leaks
//free(newheader->name);
freeStructure(newheader);
return newheader;
}
이진 파일에서 문자열을 읽고 struct에 저장해야합니다. 하지만 구조체의 문자열 변수에 값을 직접 저장할 수 없다. 새로운 문자열을 malloc하고 가리 키지 않는 한. 위 코드에서 newheader-> length를 fread로 읽을 수 있지만 newheader-> name은 읽을 수 없다는 것을 알 수 있습니다. 문자열 배열에 fread 결과를 넣으려고했으나 분할 오류가 발생했습니다.
내 바이너리 파일의 모습입니다. 그것은 모든 문자열과 int 다음에 널 종결자가 있습니다. 그때 나는 오류
*** Error in `./test': munmap_chunk(): invalid pointer:
0x000000000040133e *** Aborted.
다음 얻을 내 freestruct() 함수에서 newheader-> 이름을 확보하는 경우를 해제하지 않는 경우
0000000 021 \0 \0 \0 i t i s a g o o d
0000020 d a y \0 R \0 \0 \0 # \0 \0 \0 I t
0000040 w a s t h e b e s t o f
0000060 o y e h o y e t i m e s . \0
0000100 034 \0 \0 \0 I t w a s t h e b
0000120 l u r s t o f t i m e s . \0
0000140 \a \0 \0 \0 m o n k e y \0 006 \0 \0 \0 p
0000160 a n d a \0 006 \0 \0 \0 s h i f u \0
0000177
는 내가 1 개 블록에 17 바이트를 잃게됩니다.
==24169== HEAP SUMMARY:
==24169== in use at exit: 0 bytes in 0 blocks
==24169== total heap usage: 5 allocs, 6 frees, 1,201 bytes allocated
==24169==
==24169== All heap blocks were freed -- no leaks are possible
==24169==
==24169== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from0)
==24169==
==24169== 1 errors in context 1 of 1:
==24169== Invalid free()/delete/delete[]/realloc()
==24169== at 0x4C29E90: free (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24169== by 0x401298: freestruct (A1.c:267)
==24169== by 0x400F8A: write_strings (A1.c:189)
==24169== by 0x400840: main (test.c:25)
==24169== Address 0x40133e is not stack'd, malloc'd or (recently)
free'd
==24169==
==24169== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from0)
이 내 freestruct에서 코드의 작은 조각이 이진 파일에 캐릭터 라인을 기입 한 후() 함수
if (header->next == NULL) {
header->length = 0;
free(header->name);
free(header);
}
, 나는 전체 구조를 해제하고 read_strings 기능을 다시 만들어
편집 : readString 함수 내에서 newheader-> name을 해제하면 메모리 누수가 발생하지 않지만 newheader를 main으로 반환하고 printString 및 freestruct를 호출하고 싶습니다.
나쁜 문법에 사과드립니다. 내가 제대로 setName
및 getName
기능을 이해한다면
변수 및 값을 모니터링하면서 디버거를 사용하여 코드를 한 줄씩 단계별로 실행하십시오. 또한'setName'이라는 함수를 사용합니다. 어떻게합니까? 그리고'getName'은 무엇을합니까? 구조'name' member *를 두 번 설정해야합니까? –
그리고 관련없는 쿼리는 파일에서 문자열 (및 길이)에 문자열 종결자를 포함합니까? –
@JoachimPileborg setname은 header-> name에 보내진 문자열을 할당하고 getname은 그 값을 반환합니다. –