안녕하세요, 내 main()
에 char*
배열을 malloc하려고 할 때마다 구문 분석 루프가 파일을 찾을 때마다 extern 함수가 realloc
이됩니다. 모든 코드는 malloc
및 realloc
이 char * files[]
인 경우를 제외하고 모두 작동합니다. 나는 다음과 같은 코드를 실행하면extern 함수 내부에 C 배열을 재 할당
내가이 오류를
*** Error in `./Assignment2': double free or corruption (out): 0x00007ffcefe78e50 ***
======= Backtrace: =========
--Lots of memory locations listed--
Aborted (core dumped)
Main.c 파일
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char **files = malloc(sizeof(char*));
/* parse the command line arguments */
parseArguments(argc, argv,&files);
}
parse.c를받을
#include <stdlib.h>
#include "stddef.h";
#include <stdlib.h>
#include <argp.h>
char* printUsage();
extern int parseArguments(int argc, char *argv[], char *files[]) {
int i = 1;
int amtFiles = 0;
/** Loops through each item turning on and off switches*/
while (argc > 1) { /* i moves left to right 0 is file name so start at 1*/
if (isFile(argv[i]) == 1) { /*if its a file */
*files = realloc(files, amtFiles * sizeof *files);
*files[amtFiles] = argv[i];
/*printf("%s",*files[amtFiles]);*/
amtFiles++;
}
}
i++;/* increment argv[i] to next inputted char*/
argc--; /* decrement the total amount of arguments to go through */
}/*Loop ends */
}
나는 그에게 포인터 오류가 있으리라 믿고있어하지만, 힘들 때 C와 포인터를 처음 접했을 때 알아 내기 힘들다. 조금 혼란 스럽다. 링크 나 설명을 포함시킬 수 있다면 크게 감사하겠습니다. 메인 파일에서
'* files = realloc (files, amtFiles * sizeof * files); '무슨 엉망 이군! – John3136
재배정 할 대상과 반환 할 대상을 확인하십시오. '* files'와'files'는 같은 것이 아닙니다. 다이어그램을 그리는 데 도움이 될 수 있습니다. – Galen
형식이 개선되었지만 여전히'{'가 여전히 어딘가에 없거나 추가'}'이 코딩되어 있습니다. 누락 된 반환 값 – chux