Dangling pointer is when you delete a reference to memory but you still have a pointer to that location.
Yes, although the definition is a little too specific.
A dangling pointer occurs when the object that is being pointed to becomes invalid. One way it may become invalid is if it is deleted. Another way is if it goes out of scope while the pointer still exists (such as is the case when a pointer to a local variable with automatic storage duration is returned from a function.)
Ex:
1 2 3 4 5
int* get_num(void)
{
int val = 69;
return &val;
}
Lost object is just object inside a certain scope that's destroyed outside.
No. A lost object is an object of dynamic storage duration for which there is no valid pointer or reference. Given the lack of either of those, there is no way to reach the object and the memory it occupies is considered to be leaked.
1 2
int* p = newint(69);
p = nullptr;
The object p points to on line 1 is a lost object when line 2 is executed.
also i know binding links items together but where does this happen?
A dangling pointer occurs when the object that is being pointed to becomes invalid. One way it may become invalid is if it is deleted. Another way is if it goes out of scope while the pointer still exists (such as is the case when a pointer to a local variable with automatic storage duration is returned from a function.)
But then would that not cause a leak in memory in that the location of whatever you're pointing to is deleted, but the object still exists in memory? Unless that specifically gets cleaned up after the program runs, which defeats the whole purpose of having such a term.
It isn't clear what you're referring to.
I took it as @OP referring to linkers (as in how does VS know to link header files and their respective implementation files).
But then would that not cause a leak in memory in that the location of whatever you're pointing to is deleted, but the object still exists in memory?
You could create a local object in the stack (not the heap) in a function and return a reference to it. Because it was local, it will be destroyed when the function returns and thus the pointer returned no longer points at a valid object.
1 2 3 4 5 6 7 8 9
//in main
int *ptr = getIntPtr();
///
int* getIntPtr()
{
int a = 5;
return &a; //oops
}
ptr will be not be pointing to a valid object anymore, but it was not a dynamically-allocated object inside the function so there is no memory leak.
The stack and heap are separate. Dynamically allocated memory lives in the heap. It doesn't have a 'scope', except that the OS will take it back when the program terminates (memory isn't lost FOREVER). But during execution, heap memory must be managed by the program itself.
If you lose an object in the heap, it still exists out there, existing in the void with no way to get it back.
But then would that not cause a leak in memory in that the location of whatever you're pointing to is deleted, but the object still exists in memory?
A dangling pointer is sort of the opposite of a memory leak. With a dangling pointer, you have a pointer to memory that isn't allocated. With a memory leak, you don't have a pointer to memory that is allocated.
int * ptr1 = newint;
int * ptr2 = ptr1; // Both pointers point to same int on the heap
// References to either ptr1 or ptr2 are valid here
delete ptr1; // Delete the allocated int
// ptr1 and ptr2 are technically both dangling pointers because they point to memory that is no longer allocated.
ptr2 is the more common example. ptr1 is allocated and deleted correctly. Sometime during ptr1's lifetime, ptr2 is created. After ptr1 is deleted, it is a common mistake to try and use ptr2 being unaware that ptr1 was deleted.