Removing a pointer in C ++

Context: I'm trying to wrap my head around pointers, we just saw them a couple of weeks ago at school, and, practicing today, did I come across stupidity? problem, it may be very simple for you, but I have little programming experience.

I saw quite a few questions in SO about deleting pointers, but they all seem to be related to deleting a class, not a "simple" pointer (or whatever rule it may be), here I'm trying to run the code:

#include <iostream>; using namespace std; int main() { int myVar, *myPointer; myVar = 8; myPointer = &myVar; cout << "delete-ing pointers " << endl; cout << "Memory address: " << myPointer << endl; // Seems I can't *just* delete it, as it triggers an error delete myPointer; cout << "myPointer: " << myPointer << endl; // Error: a.out(14399) malloc: *** error for object 0x7fff61e537f4: // pointer being freed was not allocated // *** set a breakpoint in malloc_error_break to debug // Abort trap: 6 // Using the new keyword befor deleting it works, but // does it really frees up the space? myPointer = new int; delete myPointer; cout << "myPointer: " << myPointer << endl; // myPointer continues to store a memory address. // Using NULL before deleting it, seems to work. myPointer = NULL; delete myPointer; cout << "myPointer: " << myPointer << endl; // myPointer returns 0. } 

So my questions are:

  • Why won't the first case work? Seems to be the easiest use to use and delete a pointer? The error says that the memory was not allocated, but "cout" returned the address.
  • In the second example, the error does not start, but does the cout of the myPointer value still return the memory address?
  • Does # 3 really work? It seems to work for me, the pointer no longer stores the address, is this the correct way to delete the pointer?

Sorry for the long question, I wanted to make it as clear as possible, and also repeat, I have little experience in programming, so if someone could answer it using non-professional questions, it would be very useful!

+47
c ++ pointers delete-operator
Nov 04
source share
6 answers

1 and 2

 myVar = 8; //not dynamically allocated. Can't call delete on it. myPointer = new int; //dynamically allocated, can call delete on it. 

The first variable was allocated on the stack. You can only cause deletion in memory that you allocate dynamically (on the heap) using the new operator.

3.

  myPointer = NULL; delete myPointer; 

There was nothing above at all . You didnโ€™t release anything because the pointer pointed to NULL.




Do not do the following:

 myPointer = new int; myPointer = NULL; //leaked memory, no pointer to above int delete myPointer; //no point at all 

You specified it in NULL, leaving the skipped memory (new, selected by you). You must free the memory you pointed to. There is no way to access the allocated new int anymore, hence a memory leak.




The right way:

 myPointer = new int; delete myPointer; //freed memory myPointer = NULL; //pointed dangling ptr to NULL 



The best way:

If you use C ++, do not use raw pointers. Use smart pointers that can handle these things for you with little overhead. C ++ 11 comes with several .

+101
Nov 04
source share

I believe that you do not quite understand how pointers work.
When you have a pointer pointing to some kind of memory, you have to understand three different things:
- the pointer (memory) indicates "what is indicated"
- this memory address
- not all pointers should delete their memory: you only need to delete the memory that was dynamically allocated (the new operator is used).

Imagine:

 int *ptr = new int; // ptr has the address of the memory. // at this point, the actual memory doesn't have anything. *ptr = 8; // you're assigning the integer 8 into that memory. delete ptr; // you are only deleting the memory. // at this point the pointer still has the same memory address (as you could // notice from your 2nd test) but what inside that memory is gone! 

When did you

 ptr = NULL; // you didn't delete the memory // you're only saying that this pointer is now pointing to "nowhere". // the memory that was pointed by this pointer is now lost. 

C ++ allows you to try the delete pointer pointing to null , but actually does nothing, just does not give any error.

+11
Nov 05 '12 at 0:00
source share

Pointers are like regular variables because they do not need to be deleted. They are deleted from memory at the end of the execution of functions and / or at the end of the program.

However, you can use pointers to allocate a โ€œblockโ€ of memory, for example, as follows:

 int *some_integers = new int[20000] 

This will allocate memory space for 20,000 integers. Useful because Stack has a limited size, and you might want a large load of "ints" without error.

Whenever you call new, you must โ€œdeleteโ€ at the end of your program, because otherwise you will get a memory leak, and some allocated memory will never be returned for other programs. For this:

 delete [] some_integers; 

Hope this helps.

+10
Nov 04 '12 at 22:15
source share

In C ++, there is a rule, for every new one there is delete .

  • Why does the first case not work? Seems to be the easiest use to use and delete a pointer? The error says that the memory was not allocated, but "cout" returned the address.

new is never called. Thus, the address that cout prints is the address of the memory cell myVar or the value assigned by myPointer in this case. By writing:

 myPointer = &myVar; 

you speak:

myPointer = Address where data is stored in myVar

  1. In the second example, the error does not start, but does cout running myPointer still return the memory address?

It returns an address pointing to a remote memory location. Because first you create a pointer and assign its value myPointer, and then you delete it, the third you print it. Therefore, unless you set a different value for myPointer, the remote address will remain.

  1. Does # 3 really work? It seems to work for me, the pointer no longer stores the address, is this the correct way to delete the pointer?

NULL is 0, you delete 0, so do not delete anything. And it is logical that it prints 0, because you did:

 myPointer = NULL; 

which is equal to:

 myPointer = 0; 
+7
Nov 04
source share
  • You are trying to delete a variable allocated on the stack. You cannot do this.
  • Removing the pointer does not actually destroy the pointer, but only the occupied memory is returned back to the OS. You can access it until the memory is used for another variable or in some other way. Therefore, it is recommended that you delete the pointer to NULL (0) after deletion.
  • Removing a NULL pointer does not delete anything.
+4
Nov 04 '12 at 22:11
source share
 int value, *ptr; value = 8; ptr = &value; // ptr points to value, which lives on a stack frame. // you are not responsible for managing its lifetime. ptr = new int; delete ptr; // yes this is the normal way to manage the lifetime of // dynamically allocated memory, you new'ed it, you delete it. ptr = nullptr; delete ptr; // this is illogical, essentially you are saying delete nothing. 
+1
Nov 04 '12 at 22:15
source share



All Articles