Reputation: 15405
So I am getting an error at the end of the following code that I completely do not understand:
char* registerPointer = NULL;
// parse the currentLine
char** rt_rs_immediate;
rt_rs_immediate = malloc(3 * sizeof(char*));
if (rt_rs_immediate == NULL ) {
fprintf(outputFilePointer, "no more memory");
exit(1);
}
for (int i = 0; i < 3; i++) {
rt_rs_immediate[i] = malloc(2 * sizeof(char));
if (rt_rs_immediate[i] == NULL ) {
fprintf(outputFilePointer, "no more memory");
exit(1);
}
}
int indexWithin_rt_rs_immediate = 0;
registerPointer = strtok(currentLine, " $,\n\t");
while (registerPointer != NULL ) {
if (registerPointer == NULL || *registerPointer == '#') {
break;
} else {
strcpy(rt_rs_immediate[indexWithin_rt_rs_immediate],
registerPointer);
indexWithin_rt_rs_immediate++;
registerPointer = strtok(NULL, " $,\n\t");
}
}
free(registerPointer);
// write to outputFile
int immediate = atoi(rt_rs_immediate[2]);
writeOutI_TypeInstruction(I_TypeInstruction, rt_rs_immediate[1],
rt_rs_immediate[0], immediate, outputFilePointer);
// free pointers created with malloc
for (int i = 0; i < 3; i++) {
free(rt_rs_immediate[i]); //<====================ERROR HERE!!!!!
}
free(rt_rs_immediate);
Upvotes: 0
Views: 1940
Reputation: 8839
Your while
loop terminates when regsiterPointer
is a NULL
. You may want to check on that loop because you only have three pointers allocated to rt_rs_immediate
. If you try to go past the three pointers in that loop, that could cause an error.
Upvotes: 1