在 c 中得到 "free(): invalid pointer"
getting a "free(): invalid pointer" in c
我是 c 语言的新手,
我的程序用于解析 txt 文件并将第一个单词保存在名为 File 的结构中。
例如:
对于包含以下内容的 txt 文件:
文件 1:文件 2、文件 3
文件 66: 文件 7
我想将 file1 和 file2 保存在一个结构中
什么时候
文件1->名称=文件1
fiel1->relation=[file2,file3]
但这只是对程序的一些解释。
问题是:
因为我不知道代表名称的字符数组的大小 我试图使用 malloc 和 free 使用动态内存
在此过程中,我使用 strtok 进行解析
问题从最后 4 行开始(在注释中标记)
我一直收到错误 "free(): invalid pointer: 0x00007ffe6accfdd0 ***"
(我在网站上寻找答案,但由于对指针缺乏理解,我很难理解问题所在)。
谁能解释一下为什么?
提前谢谢你
typedef struct File
{
char *name;
int *relation;
}File;
char *error = BAD_FILE_MSG;
char buffer[MAX_LEN_SIZE];//the file buffer
if (argc != RIGHT_NUM_OF_PARAM) {
fprintf(stderr, UNVALID_PARAMETER_MSG);
return BAD_RET_VAL;
}
char *fileName = argv[1];
FILE *fp = fopen(argv[1], "r"); /* "r" = open for reading */
if (fp == NULL)
{
fprintf(stderr, "%s %s", error, fileName);
return BAD_RET_VAL;
}
if (ferror(fp) != 0)
{
fprintf(stderr, "%s %s", error, fileName);
return BAD_RET_VAL;
}
//int line = 0;//todo magic
while (fgets (buffer, sizeof(buffer), fp))
{
/**
* saving all the line in a char[]
*/
//give memory to an array in file
char *token;
token = strtok (buffer, SIGN_FOR_SEPARATE);
int marker = 0;//todo fix
//creating a struct of a file
File* fileStruct = (File*)malloc(sizeof(File));
//creating a dynamic array of ints for relation
fileStruct->relation = (int *)malloc(100 * sizeof(int));//todo free your mind
char file[100];
while (token != NULL)
{
if (marker == 0)
{
char* pointer = fileStruct->name;
size_t size = strlen(token);
//creating a dynamic array of chars for name
fileStruct->name = (char *)malloc(size + 1);
fileStruct->name = token;//**getting the error**
free(pointer);
marker++;
}
我将重点关注导致您出错的原因(我没有阅读您的大部分代码)。
指针是 C 语言中需要很长时间学习的概念之一,而且你还有很长的路要走。
指针只是内存中的一个地址,没有别的。您可以将 *pointer
视为表示 "take the number stored in pointer, go out to memory, and return the value that you find at the address corresponding to that number" 的函数调用。
当你说:
char* pointer = fileStruct->name;
您没有以任何方式将这两个变量联系起来。这就像在说:
int foo = 3;
int bar = foo;
目前它们具有相同的值,但如果您更改 foo,bar 不会改变。
在您的代码中,您实际上并没有在任何地方使用指针,因此您可以去掉它并在使用完后调用 free(fileStruct->name)
。
话虽如此,您需要更多 practice/reading/learning 了解指针的工作原理。如果您刚刚开始进行一般编程,您可能希望在熟悉基础知识之前完全避免使用指针。
我是 c 语言的新手, 我的程序用于解析 txt 文件并将第一个单词保存在名为 File 的结构中。
例如: 对于包含以下内容的 txt 文件: 文件 1:文件 2、文件 3 文件 66: 文件 7
我想将 file1 和 file2 保存在一个结构中 什么时候 文件1->名称=文件1 fiel1->relation=[file2,file3]
但这只是对程序的一些解释。
问题是: 因为我不知道代表名称的字符数组的大小 我试图使用 malloc 和 free 使用动态内存 在此过程中,我使用 strtok 进行解析 问题从最后 4 行开始(在注释中标记) 我一直收到错误 "free(): invalid pointer: 0x00007ffe6accfdd0 ***" (我在网站上寻找答案,但由于对指针缺乏理解,我很难理解问题所在)。
谁能解释一下为什么? 提前谢谢你
typedef struct File
{
char *name;
int *relation;
}File;
char *error = BAD_FILE_MSG;
char buffer[MAX_LEN_SIZE];//the file buffer
if (argc != RIGHT_NUM_OF_PARAM) {
fprintf(stderr, UNVALID_PARAMETER_MSG);
return BAD_RET_VAL;
}
char *fileName = argv[1];
FILE *fp = fopen(argv[1], "r"); /* "r" = open for reading */
if (fp == NULL)
{
fprintf(stderr, "%s %s", error, fileName);
return BAD_RET_VAL;
}
if (ferror(fp) != 0)
{
fprintf(stderr, "%s %s", error, fileName);
return BAD_RET_VAL;
}
//int line = 0;//todo magic
while (fgets (buffer, sizeof(buffer), fp))
{
/**
* saving all the line in a char[]
*/
//give memory to an array in file
char *token;
token = strtok (buffer, SIGN_FOR_SEPARATE);
int marker = 0;//todo fix
//creating a struct of a file
File* fileStruct = (File*)malloc(sizeof(File));
//creating a dynamic array of ints for relation
fileStruct->relation = (int *)malloc(100 * sizeof(int));//todo free your mind
char file[100];
while (token != NULL)
{
if (marker == 0)
{
char* pointer = fileStruct->name;
size_t size = strlen(token);
//creating a dynamic array of chars for name
fileStruct->name = (char *)malloc(size + 1);
fileStruct->name = token;//**getting the error**
free(pointer);
marker++;
}
我将重点关注导致您出错的原因(我没有阅读您的大部分代码)。
指针是 C 语言中需要很长时间学习的概念之一,而且你还有很长的路要走。
指针只是内存中的一个地址,没有别的。您可以将 *pointer
视为表示 "take the number stored in pointer, go out to memory, and return the value that you find at the address corresponding to that number" 的函数调用。
当你说:
char* pointer = fileStruct->name;
您没有以任何方式将这两个变量联系起来。这就像在说:
int foo = 3;
int bar = foo;
目前它们具有相同的值,但如果您更改 foo,bar 不会改变。
在您的代码中,您实际上并没有在任何地方使用指针,因此您可以去掉它并在使用完后调用 free(fileStruct->name)
。
话虽如此,您需要更多 practice/reading/learning 了解指针的工作原理。如果您刚刚开始进行一般编程,您可能希望在熟悉基础知识之前完全避免使用指针。