This is the most likely problem:
lineToken = malloc(sizeof(1));
Here you allocate the size of an integer literal, but lineToken
needs at least sizeof(*lineToken)
(or sizeof(char *)
) bytes. The size of an integer and the size of a pointer may not be the same, especially on a 64-bit platform (where int
is four bytes and pointers are eight bytes).
So when you do
lineToken[0] = strtok(string, " ")
you write beyond what's allocated (buffer overflow) and overwrite data put there by the allocator.
There's also a problem with the realloc
call which will allocate i + 1
bytes. So even when you're on a 32-bit platform (where the size of int
just happens to be the same as the size of a pointer) you will reallocate the pointer from being four bytes to two bytes in the first iteration of the loop.
You also have other problems, like these loops:
for(i = 1; i; i++)
Remember that in C all non-zero values are true, and when will i
ever be zero in that loop?