C++ Delete - It Deletes My Objects But I Can Still Access the Data

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.

Why can still access class instances after deleting them [duplicate]

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.

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++ pointer array is still accessible after delete[] is called [duplicate]

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

The pointer to memory location to the object has been deleted in program but still provides the correct answer and not garbage value [duplicate]

Freeing a memory location does not automatically overwrite it with garbage. By coincidence, that value stored in balance is still the same.

What does delete command really do for memory, for pointers in C++? [duplicate]

Think of memory as a big warehouse with lots of boxes to put things into. When you call "new", the warehouse staff finds an unused box large enough for your needs, records that box as being owned by you (so it's not given to someone else), and gives you the number of that box so you can put your stuff into it. This number would be the "pointer".

Now, when you "delete" that pointer, the reverse happens: the warehouse staff notes that this particular box is available again. Contrary to real warehouse staff they aren't doing anything with the box — so if you look into it after a "delete" you might see your old stuff. Or you might see somebody else’s stuff, if the box was reassigned in the meantime.

Technically, you are not allowed to look into your box once you have returned it to the pool, but this is a somewhat weird warehouse with no keys or guards, so you can still do whatever you want. However, it might cause problems with the new owner of the box, so it's expected that you follow the rules.



Related Topics



Leave a reply



Submit