CS term confusion

does anyone have a clear cut difference between a dangling pointer and a lost object?
i know one of these or both cause a memory leak

also i know binding links items together but where does this happen? in RAM? during pass 1 of the compiler?

Dangling pointer causes memory leak. Dangling pointer is when you delete a reference to memory but you still have a pointer to that location.

https://en.wikipedia.org/wiki/Dangling_pointer

Lost object is just object inside a certain scope that's destroyed outside.

Like if you had a function
1
2
3
4
5
int multiply( void )
{
    int x;
    return x;
}

that integer "x" is no longer valid when the code runs past the function's code. It gets deleted by the compiler or in some cases, the OS.


As for where and when binding happens, I heard it's during first pass of the compiler, but don't quote me on that. Because I'm not sure.
Dangling pointer causes memory leak.

No.

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 = new int(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?

It isn't clear what you're referring to.
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).
Thanks guys
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.
Ah I see. I was mixing my definitions earlier.
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.
dhayden

could u demonstrate code for a dangling pointer
1
2
3
4
5
6
 
int * ptr1 = new int;
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.
Oh okay, I have done that a few times lol.

could you demonstrate a memory leak
1
2
  int * ptr1 = new int;  //  Allocate from heap 
  ptr1 = new int;          

Now have no way to release the original allocation.
Topic archived. No new replies allowed.