원래 free로 메모리 풀어주라고 해서 풀어줬는데 이런 메세지가 떠요.
참고로 시커먼 실행창에서는 원하는 내용은 다 나왔거든요?
이건 왜 뜨는 걸까요?
(수정) 코드 전체 올려봅니다.ㅎㅎ..;; 지송;;
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable:4996)
const char* read_ins(FILE* in) {
static char buf1[1] = { 0 };
int bufcount = 0;
static int readcount = 0;
char buf2 = { 0, };
int endcount = 0;
do {
if (buf1[0] == 10) ++readcount;
fseek(in, readcount, SEEK_SET);
fread(buf1, sizeof(char), 1, in);
if (buf1[0] != 10) {
buf2[bufcount] = buf1[0];
++bufcount;
}
++readcount;
if (buf1[0] == 10 || buf1[0] == 32) { endcount = 1; }
} while (endcount==0);
return buf2;
}
struct Student {
char name;
char sex;
char region;
char subject[16];
float score;
int height;
int weight;
};
int LAD(struct Student* s,const char* buf) {
FILE* fp = fopen(buf, "r");
int done = 0;
struct Student temp;
int i = 0, o = 0, p = 0;
do {
fscanf(fp, "%s %c %s %s %f %d %d", &s[i].name, &s[i].sex, &s[i].region, &s[i].subject, &s[i].score, &s[i].height, &s[i].weight);
done++;
i++;
} while (fscanf(fp, "%s %c %s %s %f %d %d", &s[i].name, &s[i].sex, &s[i].region, &s[i].subject, &s[i].score, &s[i].height, &s[i].weight) != -1);
fclose(fp);
for (o = 0; o < done - 1; o++) {
for (p = o + 1; p < done; p++) {
if (strcmp(s[o].name, s[p].name) > 0) {
temp = s[o];
s[o] = s[p];
s[p] = temp;
}
}
}
return done;
}
void PRT(struct Student* s,int done) {
for (int i = 0; i < done; i++) {
printf("%s %c %s %s %f %d %d ", s[i].name, s[i].sex, s[i].region, s[i].subject, s[i].score, s[i].height, s[i].weight);
}
}
int main(void) {
char CREATE="CREATE";
char SEARCH="SEARCH";
char LOAD="LOAD ";
char PRINT ="PRINT";
char INSERT ="INSERT";
char y;
char buf;
int done;
struct Student *students = malloc(sizeof(struct Student[100]));
printf("실행 하시겠습니까(Y):");
scanf("%c", &y);
if (y ==89 ||y== 121) {
FILE* in = fopen("input.txt", "r");
strcpy(buf, read_ins(in));
if (strcmp(buf, CREATE) == 0) {
printf("CREATE명령어를 실행합니다. ");
struct Student s[100];
students = &s[0];
memset(s, 0, sizeof(s));
};
strcpy(buf, read_ins(in));
if (strcmp(buf,LOAD) == 0) {
printf("LOAD명령어를 실행합니다. ");
strcpy(buf, read_ins(in));
done=LAD(students,buf);
};
strcpy(buf, read_ins(in));
if (strcmp(buf, PRINT) == 0) {
printf("PRINT명령어를 실행합니다. ");
PRT(students, done);
};
fclose(in);
}
else {
printf("다시 입력해주세요.");
}
free(students);
return 0;
}
근데 저거 free(students)빼면 잘 실행되는데 걍 빼줘야 하나요?
참고로 free(students) 빼면..
이렇게 결과만 딱 깔끔하게 잘 나와여.. 어디 찾아보니까 저 중단점 트리거 했다는게 이미 해제된 메모리를 또 해제하려고 하면 나온다던데 전 해제해준 적이 없거든요?
왜 그럴까요?
해당 댓글은 삭제되었습니다.
헤헷 수정 했습니다요~~
아이고 다시 수정함 ㅎㅎ;
엄..말로 잘 설명을 못 하겠는데..
https://gall.dcinside.com/board/view/?id=programming&no=1511959&_rk=zx3&s_type=search_all&s_keyword=C%EB%A6%B0%EC%9D%B4&page=1
여기서 처음 질문을 했을때 동적할당을 통해서 포인터로 s를 받아주면 문제가 해결이 딱 되더라구요..그리고 제가 동적할당을 잘 모르고 쓴 거라 ㅎㅎ;
아하..그렇군요..감사합니당..
걍 free하지마 ㅋㅋ
앜ㅋㅋ 근디 왜 그런지가 넘 궁금해서 ㅎㅎ;