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.

4
  • 1
    Welcome to Stack Overflow! Please see this discussion on why not to cast the return value of malloc() and family in C.. Commented Feb 8, 2016 at 15:54
  • 4
    realloc(taskList, (num+allocsize)*tasksize); --> realloc(*taskList, (num+allocsize)*tasksize); Commented Feb 8, 2016 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. Commented Feb 10, 2016 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) Commented Feb 10, 2016 at 20:13

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. But it makes no difference. Same segmentation fault after the third element :(
@mKay We haven't got your real code. I cannot see other errors. Is tasksize*allocsize at least 8 element?
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.
@user3121023 Yes, it is. Good catch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.