-7

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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 4
    What makes you think it does not do the same thing? Also warning: when calling `sizeof` in `malloc` (and the like) [you should always write it](http://stackoverflow.com/a/17258659/1151654) as `ptr = malloc(sizeof(*ptr) * ...);` instead of `ptr = malloc(sizeof(ptrtype*) * ...);`. Second warning: You [should not cast](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) the return of malloc. – Eregrith May 06 '15 at 12:26
  • 2
    Why do you think the latter cannot be done? – David Rodríguez - dribeas May 06 '15 at 12:26
  • 4
    Start by making your code actually *compilable*. `free A`? – Some programmer dude May 06 '15 at 12:27
  • 3
    @Eregrith: The OP made the mistake of tagging with two different languages. Your comment applies completely to C, but the second half is wrong in C++ (not your problem, but the OP's). Also, I tend to avoid the parenthesis in `sizeof`, which are not needed for values, but are needed for types. If in code I see parenthesis, I remove them, it either builds or has to be changed. `ptr = malloc( sizeof *ptr * ... )` – David Rodríguez - dribeas May 06 '15 at 12:29
  • 1
    Re C++: [not even remotely equivalent](http://stackoverflow.com/questions/4255598/delete-vs-delete). This shouldn't be tagged C++. – Alex Celeste May 06 '15 at 12:29
  • @lundin Sir, with all due respect, is that a vaild dupe? Can you please re-check? It seems OP already knows the fundamental of allocation, but his Q was regarding de-allocation which is not covered in the linked Q. Thanks. – Sourav Ghosh May 06 '15 at 12:31
  • 2
    @Leushenko C++ is just C with classes – Fired from Microsoft May 06 '15 at 12:31
  • 7
    @FiredfromMicrosoft ugh...Please..don't say that. They ___are___ two different languages, and better treated that way. Just having the same type of _syntax_ ___does not___ make two languages _at-par_. – Sourav Ghosh May 06 '15 at 12:32
  • 1
    @FiredfromMicrosoft hey at least your name-tag matches your beliefs. – Mekap May 06 '15 at 12:33
  • 3
    In C++, you shouldn't mix "new"/"malloc" and "delete"/"free". On some platforms, and for some data types, they behave differently and you'll get strange behavior. It's safest to use only one throughout your program. – Jack Whitham May 06 '15 at 12:35
  • @JackWhitham he never asked about new/delete tho, so your comment is unrelated – Creris May 06 '15 at 12:36
  • 1
    @Creris. He said "I'm tagging this with C++ because the above can be equivalently considered with new and delete", so I say, watch out, they're not equivalent. – Jack Whitham May 06 '15 at 12:38
  • 2
    @FiredfromMicrosoft are you just trolling? – Eregrith May 06 '15 at 12:40
  • 1
    There's lot of noise here about C or C++. This is irrelevant. The premise of the question is wrong in both languages. And is wrong for malloc/free as well as new/delete as well as new[]/delete[]. – juanchopanza May 06 '15 at 13:05

2 Answers2

7

would not do the same.

Why'd you think like that?

Maybe, because, after freeArray(myArray);, you are able to access the same, right?

Well, that is a result of undefined behaviour.

BTW, in your first snippet,

free myArray;

should be

free(myArray);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Or perhaps the OP meant `delete myArray;` ? (Which I think would be undefined behavior.) – Leiaz May 06 '15 at 12:36
  • Indeed it would. He'd mean `delete[] myArray`. That is a particularly fun mistake to make, as sometimes `delete myArray` will appear to work. – Jack Whitham May 06 '15 at 12:40
2

From what I understand, [X] would free all the memory of the array {69, 69, 69} at the memory address myArray

Right.

but [Y] would not do the same.

Wrong.

Your understanding is incorrect. Please indicate where you read that, so that we may endeavour to correct this egregious error at source.

[X] and [Y] do the same.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055