C++ Delete Pointer Issue, Can Still Access Data

c++ delete pointer issue, can still access data

Deleting a pointer doesn't zero out any memory because to do so would take CPU cycles and that's not what C++ is about. What you have there is a dangling pointer, and potentially a subtle error. Code like this can sometimes work for years only to crash at some point in the future when some minor change is made somewhere else in the program.

This is a good reason why you should NULL out pointers when you've deleted the memory they point to, that way you'll get an immediate error if you try to dereference the pointer. It's also sometimes a good idea to clear the memory pointed to using a function like memset(). This is particularly true if the memory pointed to contains something confidential (e.g. a plaintext password) which you don't want other, possibly user facing, parts of your program from having access to.

C++ delete - It deletes my objects but I can still access the data?

Is being able to access data from beyond the grave expected?

This is technically known as Undefined Behavior. Don't be surprised if it offers you a can of beer either.

Values still accessible after I call delete, c++

If I call delete shouldn't the memory on heap be delete and when print is called not output anything for BankAccount?

No.

Deleting the object at that location in memory means it does not exist any more, so you're not allowed to access it.

It does not mean your program will magically print "nothingness" to protect you from this mistake.

Your program therefore has undefined behaviour; you must make sure you do not dereference an invalid pointer!

C++ pointer array is still accessible after delete[] is called

The memory is free, which means it isn't attributed anymore, but the compiler's not going to take the extra effort to wipe it back to 0 everytime something's deleted.

It's also not going to take the effort to check that the memory is properly allocated before you access it - it'd reduce performance, and it assumes you don't do so. (Although tools like valgrind or debuggers can detect those wrong calls)

So it just changes the range of the memory as 'unassigned' internally, which means another call to new can use that same memory range. Then whatever data in that memory would be overwritten, and foo[90] won't return the same thing anymore.

c++ delete pointer and then access the value of it points to

You first leaked a pointer

int *p = new int;
p = # // You just leaked the above int

then illegally deleted something you did not new

delete p;  // p points to num, which you did not new

Why can still access class instances after deleting them

If I can still access the instance members after I deleted it how do I know if it was properly deleted?

Just trust the runtime library: if you call delete, the thing is deleted, period. Once you tell the system that you no longer need the memory, you gave it up - it is no longer yours. The system counts it with its available memory to redistribute, so it can give it to other parts of your program upon request. You can still access that memory, and may even find what was there before you gave it up, but that's undefined behavior.

Is my code going to cause a memory leak?

No, because you release the memory that you allocated.

I guess the ultimate question is: am I doing something wrong?

Yes - accessing the memory of deallocated object is wrong (undefined behavior). Other than that, it looks fine.

Why access deleted pointer won't crash the program?

When you have a pointer to a class, and call a non-virtual function on it, whatever the address is at the pointer will be considered the this pointer. Even if it is zero. As long as you don't try to access members at that address, you should have no problem printing the this poniter.

struct A {
void printThis() {
printf("%d\n", this);
}
};

int _tmain(int argc, _TCHAR* argv[])
{
A * a = (A*) 777;
a->printThis(); // will print 777

a = NULL;
a->printThis(); // will print 0

return 0;
}

When you delete a pointer and don't set its address to null, the previous address value is kept.

Accessing a deleted pointer is undefined behavior. It is not required to crash. It just may be that the random data that you are pointing to has some meaning somewhere else in the program. It may even be your old data. Deleting a pointer tells the system it can reuse that memory, but it may not have had time to reuse it yet, and so the old data could still be visible.

Finally your program crashes when you uncomment your line because that sets ptr = 0, and &(0x0000000->l) is an invalid memory reference.

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.

C++ after delete pointer

int* ptr = new int(6); reserves some memory where ptr will be pointing to, that memory will be good to store one int, 6 or any other, it cannot be used to do anything else, you can reliably store the data there and access it later.

After you delete it you tell the system that the memory is available and the program can use it for whatever else it wants. ptr may still be pointing to the same address(the value of the pointer) and you can still print it, but that memory no longer belongs to ptr, accessing that memory through it (e.g.: dereferencing the pointer), amounts to undefined behavior.

The value of the pointer(which is the address it's pointing to) normally remains unchanged until you change it yourself.

A somewhat common practise is to assign nullptr to a pointer that doesn't point to any valid memory location, that way you can easily check if it can be dereferenced or not.



Related Topics



Leave a reply



Submit