I create a dynamic array in C with malloc, ie.:
myCharArray = (char *) malloc(16);
Now if I make a function like this and pass myCharArray
to it:
reset(char * myCharArrayp)
{
free(myCharArrayp);
}
will that work, or will I somehow only free the copy of the pointer to myCharArrayp
and not the actual myCharArray
?
That will be fine and free the memory as you expect.
I would consider writing the function this way:
void reset(char** myPointer) {
if (myPointer) {
free(*myPointer);
*myPointer = NULL;
}
}
so that the pointer is set to NULL after being freed. Reusing previously freed pointers is a common source of errors.