there is a Invalid read of size 4 shows up, but i don't know how to avoid it: this is where the valgrind tells me have the invalid read
Queue* recordQueue = newQueue(NULL, NULL);
FILE* file = fopen(fileName, "r");
char* processName = malloc(8 * sizeof(char));
assert(processName != NULL);
unsigned int arriveTime;
unsigned int serviceTime;
short memoryRequirement;
while (fscanf(file, "%d %s %d %hd", &arriveTime, processName,
&serviceTime, &memoryRequirement) != EOF) {
ProcessStatus status = NOT_READY;
ProcessInfo* processInfo = newProcessInfo(processName, arriveTime,
serviceTime, memoryRequirement, status);
processName = malloc(8 * sizeof(char));
assert(processName != NULL);
inQueue(recordQueue, newNode(processInfo, NULL, NULL));
}
return recordQueue;
and the valgrind shows that:
==237688== Invalid read of size 4
==237688== at 0x48C3AF2: __vfscanf_internal (vfscanf-internal.c:345)
==237688== by 0x48C329C: __isoc99_fscanf (isoc99_fscanf.c:30)
==237688== by 0x10A3AE: readProcessesFronFile (in /home/haozhec/project1/comp30023-2023-project-1/allocate)
==237688== by 0x10A91D: main (in /home/haozhec/project1/comp30023-2023-project-1/allocate)
==237688== Address 0xc0 is not stack'd, malloc'd or (recently) free'd
==237688==
==237688==
==237688== Process terminating with default action of signal 11 (SIGSEGV)
==237688== Access not within mapped region at address 0xC0
==237688== at 0x48C3AF2: __vfscanf_internal (vfscanf-internal.c:345)
==237688== by 0x48C329C: __isoc99_fscanf (isoc99_fscanf.c:30)
==237688== by 0x10A3AE: readProcessesFronFile (in /home/haozhec/project1/comp30023-2023-project-1/allocate)
==237688== by 0x10A91D: main (in /home/haozhec/project1/comp30023-2023-project-1/allocate)
anybody know how to fix it? can anybody tells me how to avoid such problem when using fscanf?