From what I understand,
int * createArray ( void )
{
int * arr = (int*)malloc(3*sizeof(int));
arr[0] = 69; arr[1] = 69; arr[2];
return arr;
}
int main ()
{
int * myArray = createArray();
free myArray;
return 0;
}
would free all the memory of the array {69, 69, 69}
at the memory address pointed by myArray
, but
void freeArray ( int * A )
{
free A;
}
int main ()
{
int * myArray = (int*)malloc(3*sizeof(int));
myArray[0] = 69; arr[1] = 69; arr[2] = 69;
freeArray(myArray);
return 0;
}
would not do the same. The reason that this confuses me is because in both cases you are dealing with a copy of the original pointer, but deleting the pointed-to object from that copy only works in the first case. This seems like an inconsistency, but maybe I'm wrong an entirely. Can someone clear this up for me?