If an object manipulated by a pointer is allocated in the heap, can it be collected by the Garbage Collector?

Asked

Viewed 48 times

1

I am studying C# and I am in the pointers section. I understand that a pointer stores a memory address of an "object" (types by value and Array). The data stored by the pointer (the address) is in the stack, the "object" (value) is in the heap. This means that if my pointer fails to point to that object in the heap, the object is collected by the Garbage Collector?

I also read that the GC when performing a collection can change the address objects, so my pointer can be invalid and point to another value? In that case, my object gets lost in the heap or is collected?

1 answer

1


Concepts

The pointers part is very advanced and almost nobody uses it, it is for very specific situations. What is in the question has nothing really talking about pointers, by chance it has pointer internally, but you do not need to know it and you do not need to have it if the language wanted to do different.

A pointer does not store a memory address, it is a number that is a memory address. Who stores something is a variable.

It points to where an object is among the so-called, types by reference. A array is one of those guys, but the types per value are the opposite of this, they never engage with pointers (even have very specific case of aliasing, but let’s not get that into the story). If you research, especially among my answers, I talk a lot about this.

Data is not stored by pointers, objects are pointed by pointers, see second paragraph.

The pointer, which is an address, stays where it needs to be heap same. If you have a type by reference within an object that is in the heap, where is the pointer of this object? In heap, clear-cut.

I suggest to study a number of misconceptions better since this will make you understand everything else mistakenly. Most of them have answers here on the site same.

Work of the GC

If a location that has a pointer ceases to exist at some point for some reason, and therefore it no longer points to an object, at some point in the lifetime of the application (it may be only at the end) the object will be released from memory. You have no control over when this will occur.

The GC may at some point be fired and it is almost certain that the object will be discarded because it no longer has a pointer pointing to it. But note that there can be no pointer pointing to it, if one pointer failed to point but another continues pointing then the object will not be discarded.

The generational GC of. NET can and does change objects from place for a number of reasons that is not the case here, but I have answered about in several places. Then a pointer to this object needs to be updated to reflect the new address of the object. The GC of . NET does all this without you knowing and safely, so your pointer (which you don’t even need to know exists, remember?) will always be valid. He’ll never point at a wrong object. It doesn’t make sense that it gets lost, that’s GC’s problem, you don’t have to know anything about it when you make an application. You only need to know these details if you are working with GC internally.

Invalidity

There may be invalidity in some situations in context unsafe but that’s what I said at first, almost nobody uses it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.