0

Dynamically allocating memory in C++ of course can be accomplished using new or delete. If a pointer dynamically allocated using new is no longer needed in the course of a C++ program, delete can be used in order to dynamically free up computer memory. I think, if my memory serves me correctly, that Stroustrep in one of his books on C++ mentions that malloc and alloc in C afford the compiler rather than the programmer the option to "free up" or "create" memory in contrast to the object-oriented "new" and "delete". If I don't delete the pointer, then I'll run into a rather insidious memory leak, which isn't good.

However, merely deleting a pointer--as Carrano, Helman, and Veroff (CHV) note on pages 151-153 in the 2nd edition of Walls and Mirrors--does not get rid of the pointer. Rather, delete empties the contents of the pointer yet leaves the pointer somewhere in the memory space of the computer.

In any event, CHV then say that the pointer must be set to NULL after using delete in order get rid of this "deleted" construct.

Is the assignment operator being overloaded in this case to accept NULL as a Boolean value? In other words, is the compiler telling the computer that it is false that the pointer ought to exist in the memory space, meaning that I'm telling the compiler to physically stop a bunch of electrons occupying a two bit space from running through my computer? Am I missing something here?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
fishermanhat
  • 195
  • 1
  • 8
  • 5
    So... much... confusion... – Matteo Italia Oct 20 '13 at 03:06
  • 5
    This is like one of those troll scientific papers. – ta.speot.is Oct 20 '13 at 03:06
  • I was under the impression that "NULL == 0" would be treated as TRUE, meaning that NULL is synonymous with FALSE, which would imply that a pointer becomes of a "Boolean" type construct once terminated in this way. My thought was that 0 "closes" an electronic gate while "1" implies that a gate is open. Am I mistaken? – fishermanhat Oct 29 '13 at 02:26

4 Answers4

4

merely deleting a pointer does not get rid of the pointer

Indeed not; it destroys the object that the pointer points to, and deallocates the object's memory. The pointer isn't affected, and ends up pointing to invalid memory.

In any event, CHV then say that the pointer must be set to NULL after using delete in order get rid of this "deleted" construct

No, the pointer will still exist after that; but setting it to null will make sure it's not pointing to invalid memory, which is arguably safer. Of course, it's a much better idea to manage dynamic objects with smart pointers, so you never end up with an invalid pointer in the first place.

Is the assignment operator being overloaded in this case to accept NULL as a Boolean value?

No, NULL isn't a boolean value, it's a null pointer constant; a special pointer value which doesn't point to anything.

In other words, is the compiler telling the computer that it is false that the pointer ought to exist in the memory space, meaning that I'm telling the compiler to physically stop a bunch of electrons occupying a two bit space from running through my computer?

Er, what? No, it's just telling the pointer not to point to anything. It certainly doesn't stop any electrons from occupying any spaces.

Am I missing something here?

I think the main idea you're missing is that the pointer and the thing it points to are separate objects, with separate lifetimes. delete will destroy an object with a dynamic lifetime, but not the pointer that's used to refer to it.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

A bit of history might help. First, there was C, with malloc, etc. e.g.:

char *ptr = malloc(100);
strcpy(ptr, "hello, world!");
free(ptr);
printf("%s\n", ptr);   /* BAD - but will PROBABLY print "hello, world!" */

malloc allocates 100 bytes, the strcpy populated it, and the free() deallocates it. What's involved in "deallocate" simply means that that memory will now be available for reuse by another call to malloc.

free() does NOT reset any pointers, nor clear the memory - hence printf above will probably still print the string as nothing will (probably) have changed. Of course, any programmer writing something like this deserves to be fired.

In order to avoid the bad usage, since free() won't reset the pointers, it's good practice for us to reset them instead as soon as we free some memory :

free(ptr);
ptr = NULL;
printf("%s\n", ptr);   /* Now we get a NULL pointer exception - which is what we want */

We WANT the Null-pointer-exception, since after the free() we shouldn't be using the value of ptr.

With C++, all these arguments are the same. delete does a few more things than free - primarily it calls the objects destructors, etc, but the basic's the same. Pointers are not reset, and the memory's still lying around - and you DO want to know if you're accessing memory that's not allocated to you - and the best way of doing that is setting your pointers to null when you delete the object.

racraman
  • 4,988
  • 1
  • 16
  • 16
0

Nulling pointer with nullptr or NULL is used to prevent crashes on double deletes, not to free the memory the pointer actually takes (pointer, not memory it is pointing at). The only time when the actual pointer stops existing is the end of a scope.

N A
  • 656
  • 3
  • 6
0

The lifetime of the pointer itself, like all other variables, is governed by scope.

void someFunc()
{
    int* ptr;

}//ptr dies after this bracket

So always please remember to free the memory a pointer points to before it goes out of scope or you`ll never be able to access it again!

Lloyd Crawley
  • 606
  • 3
  • 13