丶这道路有点黑 丶这道路有点黑
威不足则多怒
关注数: 34 粉丝数: 187 发帖数: 3,440 关注贴吧数: 48
各位大佬,我的这个程序为什么一直报错啊??! 各位大佬我的程序莫名报错,调了一个上午,把能改的地方都改了,也不知道怎么回事求大佬们看看帮帮忙啊!! 程序逻辑很简单,就是读取文件,每行存到一个堆上的字符串里,最后free内存,分配堆内存函数多了个统计一共分配了多少内存的功能。 程序执行到main函数中的while块分配内存语句时,有时报错有时不报错(符号未加载、出发了一个断点、引起了一个异常等错误),在最后的free_pstring中肯定报错! 有没有大佬能帮帮忙啊,哭了ToT // global.h extern size_t mem; // 内存分配总量 extern size_t cmpnum; // 比较次数 // global.c size_t mem = 0; // 内存分配总量 size_t cmpnum = 0; // 比较次数 // provided.h #include "pstring.h" void* bupt_malloc(size_t size); int byte_cmp(char a, char b); // provided.c #include "provided.h" #include "global.h" #include "pstring.h" #include<stdio.h> #include<string.h> void* bupt_malloc(size_t size){ if (size <= 0) return NULL; mem += size; return malloc(size * sizeof(char)); } int byte_cmp(char a, char b){ ++cmpnum; return (a - b); } // pstring.h #include<string.h> typedef struct Pstring{ char *pstring; }Pstring; void init_pstring(Pstring* ps, const char *str); void free_pstring(Pstring *ps); // pstring.c #include "provided.h" #include "pstring.h" #include<string.h> void init_pstring(Pstring* ps, const char *str){ int len = strlen(str) + 1; ps->pstring = (char*)bupt_malloc(sizeof(char) * len); strcpy(ps->pstring, str); } void free_pstring(Pstring *ps){ free(ps->pstring); } // Main.c #include<stdio.h> #include<string.h> #include "provided.h" #include "pstring.h" #include "global.h" int main(){ FILE* fp = fopen("test.txt", "r"); char line[256]; memset(line, 0, 256); Pstring *ps = (Pstring*)bupt_malloc(10 * sizeof(Pstring)); int t = 0; size_t size_of_pstring = sizeof(Pstring*); while (!feof(fp)){ fgets(line, 255, fp); init_pstring(ps+t*size_of_pstring, line); ++t; } for (int j = 0; j < 10; ++j){ free_pstring(ps + j * size_of_pstring); } free(ps); fclose(fp); return 0; }
1 下一页