Reputation: 311
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.
Upvotes: 1
Views: 117
Reputation: 16243
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;
Upvotes: 1