The first problem is that you are not allocating memory for each char*
. mem
is a pointer to a pointer. This means this value will need allocation (as you do):
mem = (char **)malloc(sizeof(char*) * 2048);
You can remove the 512
from your code because this only needs memory for the pointers themselves not the actual characters in the pointers.
Then you need to malloc
each individual char*
. You could do this inside your for
loop:
mem[row] = malloc(513);
This allocates memory for each char
pointer. Keep in mind you need to add space for the null
character at the end.
To free
these you are going to need a loop similar to this:
for(row = 0; row < 2048; row++)
{
mem[row];
}
free(mem);
That will get rid of the individual pointers and the double pointer.
This is enough to get you started but there are a few other issues with your code. I would recommend either using some type of debugger or simply adding print statements to watch what is happening with the code as it runs.
3
solved What is wrong with this C program [closed]