getting a "free(): invalid pointer" in c
<p>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.</p>

<p>for exmple:
for a txt file containing:
file1: file2,file3
file66: file7</p>

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

<p>but this just for a bit explantion of the program.</p>

<p>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).</p>

<p>someone can explain me why? 
thank you from advance</p>

<pre><code>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-&gt;relation = (int *)malloc(100 * sizeof(int));//todo free your mind
    char file[100];
    while (token != NULL)
    {
        if (marker == 0)
        {

            char* pointer = fileStruct-&gt;name;
            size_t size = strlen(token);
            //creating a dynamic array of chars for name
            fileStruct-&gt;name = (char *)malloc(size + 1);
            fileStruct-&gt;name = token;//**getting the error**
            free(pointer);
            marker++;
        }
</code></pre>