Search code examples
ccompiler-errorsfree

getting a "free(): invalid pointer" in c


i am new at writing in c, my program is for parsing a txt file and save the first word in a struct named File.

for exmple: for a txt file containing: file1: file2,file3 file66: file7

i would like to save file1 and file2 in a struct when file1->name=file1 fiel1->relation=[file2,file3]

but this just for a bit explantion of the program.

the problem is: because i dont know the size of the array of chars that would represent the name i were trying to use dynamic memory using malloc and free in the process i am using strtok for the parsing part the problem start in the 4 last lines (marked in a comment) and i keep getting the error "free(): invalid pointer: 0x00007ffe6accfdd0 ***" (i looked in the website for answers but i because the lack of understanding in pointer it was hard for me to get the idea of the problem).

someone can explain me why? thank you from advance

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++;
        }

Solution

  • I'm just going to focus on what is causing your error (I didn't read most of your code).

    Pointers are one of the concepts in C that take a long time to learn, and you have a long way to go.
    A pointer is just an address in memory, nothing else. You can think of *pointer as being a function call that says, "take the number stored in pointer, go out to memory, and return the value that you find at the address corresponding to that number".


    When you say:

    char* pointer = fileStruct->name;  
    

    You aren't connecting those two variables in any way. It would be like saying:

    int foo = 3;
    int bar = foo;
    

    For now they have the same value, but if you change foo, bar doesn't change.

    In your code, you aren't actually using pointer anywhere, so you can just gt rid of it and call free(fileStruct->name) when you are done using it.


    That being said, you need more practice/reading/learning about how pointers work. If you are just starting to program in general, you might want to avoid pointers all together until you are are comfortable with the basics.