Deleting Pointers
is dynamically creating a pointer, and then changing a pointer address to something else still deleting the original allocated space?
No. delete
will deallocate the memory to which its operand points. You must delete
the same block of memory that you obtained from new
.
int c = 50;
int * q = new int;
int * r = q;
q = & c;
delete q; // WRONG! q points to something you didn't get from new
delete r; // OK! p points to the block you got from new
To be even more clear, delete
doesn't care about what variable it operates on, it only cares about what that variable points to. In the last line above, r
points to the block that was originally pointed to by q
and allocated by new
, so you can safely delete
it. q
points to statically allocated memory, not something you got from new
, so you can't delete
it.
Instead of changing where q
points, though, you can copy the value you want into the space that q
points to:
int c = 50;
int * q = new int;
*q = c;
delete q; // OK! q still points to the same place
Here you're changing the value stored in the location to which q
points, but you're not changing q
itself. q
still points to the block you got from new
, so it's safe to delete
it.
How to delete the data of a pointer in C?
You can't assume that memory returned from malloc
is initialized to anything. It may be initialized to zero the first time, but you can't rely on that behavior.
You can call memset
to clear the memory returned from malloc
. Or consider using calloc
, which zeros out the memory before returning the pointer to you.
In general programs only use memset
or calloc
if they really need the memory buffer to be initialized to zero. If you're just going to copy a string into it, you don't need to initialize it to zero first; you can just copy the string and (if necessary) append a '\0'
to the end.
Deleting a pointer in C++
1 & 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 call delete only on memory you allocated dynamically (on the heap) using the new
operator.
3.
myPointer = NULL;
delete myPointer;
The above did nothing at all. You didn't free anything, as the pointer pointed at NULL.
The following shouldn't be done:
myPointer = new int;
myPointer = NULL; //leaked memory, no pointer to above int
delete myPointer; //no point at all
You pointed it at NULL, leaving behind leaked memory (the new int you allocated).
You should free the memory you were pointing at. There is no way to access that allocated new int
anymore, hence memory leak.
The correct way:
myPointer = new int;
delete myPointer; //freed memory
myPointer = NULL; //pointed dangling ptr to NULL
The better way:
If you're using C++, do not use raw pointers. Use smart pointers instead which can handle these things for you with little overhead. C++11 comes with several.
What does deleting a pointer mean?
Deleting a pointer (or deleting what it points to, alternatively) means
delete p;
delete[] p; // for arrays
p
was allocated prior to that statement like
p = new type;
It may also refer to using other ways of dynamic memory management, like free
free(p);
which was previously allocated using malloc
or calloc
p = malloc(size);
The latter is more often referred to as "freeing", while the former is more often called "deleting". delete
is used for classes with a destructor since delete
will call the destructor in addition to freeing the memory. free
(and malloc
, calloc
etc) is used for basic types, but in C++ new
and delete
can be used for them likewise, so there isn't much reason to use malloc
in C++, except for compatibility reasons.
C++ delete a pointer (free memory)
The behaviour of your program is undefined. You can only use delete
on a pointer to memory that you have allocated using new
. If you had written
int* b = new int;
*b = 10;
int* c = b;
then you could write either delete b;
or delete c;
to free your memory. Don't attempt to derefererence either b
or c
after the delete
call though, the behaviour on doing that is also undefined.
C++ Why there is error when I delete pointer?
delete pointer;
does not delete the pointer itself, but the data that the pointer is pointing to. In this case, that happens to be memory that's allocated on the stack when the scope of the main()
function is entered, and is freed automatically when the scope of main()
exits. Your program attempts to free this memory using delete
, which is undefined behavior since you're calling delete
for memory that wasn't allocated using new
.
In your case, you should simply remove the delete
. All memory on the stack is freed automatically at the end of main()
: p
, d
and x
(the variable holding the address, not the address it points to) were all allocated on the stack.
Delete pointer and object
Your first code snippet does indeed delete the object. The pointer itself is a local variable allocated on the stack. It will be deallocated as soon as it goes out of scope.
That brings up the second point--if the pointer goes out of scope before you deallocate the object you allocated on the heap, you will never be able to deallocate it, and will have a memory leak.
Hope this helps.
Related Topics
Checking a Member Exists, Possibly in a Base Class, C++11 Version
Convert String Containing Several Numbers into Integers
How to Add a Timed Delay to a C++ Program
Are C++ Enums Signed or Unsigned
How to Correctly and Standardly Compare Floats
Faster Bulk Inserts in SQLite3
How to Read File Content into Istringstream
Static Variables in an Inlined Function
Difference Between C++03 Throw() Specifier C++11 Noexcept
How to Use Standard Library (Stl) Classes in My Dll Interface or Abi
How Is a Vector's Data Aligned
How to Check If an Object's Type Is a Particular Subclass in C++
Simple Object Detection Using Opencv and MAChine Learning
When and How to Use Gcc's Stack Protection Feature
Why Isn't the [] Operator Const for Stl Maps