1

I hope you can help me.

I have a function in c, which takes a file, reads line for line and stores every line as a string. It works in this function

int createDownloadList(FILE **dllistref, dltask* taskList) {
    ...
    taskList = (dltask*) malloc(tasksize*allocsize);
    int num = 0;
    while(getline(&line, &linesize, *dllistref) > 0) {
        ...
        taskList[num] = task;
        num++;
        if(num%8 == 0) {
            taskList = realloc(taskList, (num+allocsize)*tasksize);
        }
    }
    return num;
}

But I want to access the pointer to the taskList outside of the function. I tried it with this change

int createDownloadList(FILE **dllistref, dltask** taskList) {
    size_t linesize = 256;
    char* line = (char*) malloc(linesize);
    size_t tasksize = sizeof(dltask);
    int allocsize = 8;
    *taskList = (dltask*) malloc(tasksize*allocsize);
    int num = 0;

    while(getline(&line, &linesize, *dllistref) > 0) {
        ...
        *taskList[num] = task;
        num++;
        if(num%8 == 0) {
            *taskList = realloc(taskList, (num+allocsize)*tasksize);
        }
    }
    return num;
}

But I get always a segmentation fault after the third task and don't know why. I hope someone can help me, I am clueless, why it won't work. Oh, and that's how I call the second function in the main method:

dltask* taskList = NULL;
numOfTasks = createDownloadList(&fileref_dllist, &taskList)

I only added the "&" in the call, otherwise it's the same call for the first function.

mKay
  • 301
  • 1
  • 3
  • 15
  • 1
    Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Feb 08 '16 at 15:54
  • 4
    `realloc(taskList, (num+allocsize)*tasksize);` --> `realloc(*taskList, (num+allocsize)*tasksize);` – LPs Feb 08 '16 at 15:55
  • the call to `realloc()` can fail. When it fails, the `*tasklist` will contain NULL, so the pointer to the already allocated memory is overlayed, lost. This means the pointer cannot be passed to `free()`. this results in a memory leak. When calling `realloc()`, always use a temporary/local variable, then check (!=NULL) the temporary variable before making the assignment to the `*tasklist` variable. – user3629249 Feb 10 '16 at 20:08
  • regarding the parameter: `dllistref` why pass the address of that pointer? the function is not changing that pointer. So just pass the pointer, then the signature of the function becomes: `int createDownloadList(FILE *dllistref, dltask** taskList)` and the call to it becomes: `int createDownloadList(dllistref, &taskList)` – user3629249 Feb 10 '16 at 20:13

1 Answers1

1

Line

*taskList = realloc(taskList, (num+allocsize)*tasksize);

have to be

*taskList = realloc(*taskList, (num+allocsize)*tasksize);

EDIT

The second error, found out by @user3121023, is:

*taskList[num] = task;

that should be

(*taskList)[num] = task;
LPs
  • 16,045
  • 8
  • 30
  • 61
  • Thank you for your answer. But it makes no difference. Same segmentation fault after the third element :( – mKay Feb 08 '16 at 16:03
  • @mKay We haven't got your real code. I cannot see other errors. Is `tasksize*allocsize` at least `8` element? – LPs Feb 08 '16 at 16:06
  • yes, I can copy the code if you want, but I thought there couldn't be a mistake, because the first function works, same size, same everything and all I changed was the pointer of taskList. But I'll edit it in my question. – mKay Feb 08 '16 at 16:09
  • @user3121023 Yes, it is. Good catch. – LPs Feb 08 '16 at 16:12