I am allocating memory for an array, but I am moving where the pointer points forward a little. Accessing the elements works fine. It started to produce a problem with freeing the allocated memory though. Malloc complains that the pointer being freed was never allocated. The problem can reproduced with this simplified code:
int *pointer = malloc(sizeof(int)) + 1;
free(pointer - 1);
I started experimenting, and found this slight variation of the code to work.
int *pointer = malloc(sizeof(int));
pointer += 1;
free(pointer - 1);
What is the += doing different than just adding 1 to the pointer malloc returns in one line?