-3

I'm learning heap and I am told if we have to reassign a pointer to a new address, we have to delete the content in the address of the heap that the pointer points to, or that content becomes garbage and may cause memory leak. Like:

int* x = new int(10);
//delete x
x = new int(12) //without deleting x, the address that holds value 10 becomes garbage.

Therefore I'm thinking although the language doesn't have a GC, why the language is not updated with making a non-pointed heap address be able to be reallocated itself? So when the address is reallocated, the garbage content can be replaced with a new valid value, and therefore garbage collector can be omitted as well?

Thanks, and please point out if I'm wrong :)

Hang
  • 355
  • 3
  • 19
  • By "reallocated itself" do you mean "get released"? BTW, the language is not updated that way because the standard library is already updated that way. See https://en.wikipedia.org/wiki/Smart_pointer –  Sep 26 '17 at 04:23
  • 1
    Possible duplicate of [What is a smart pointer and when should I use one?](https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one) –  Sep 26 '17 at 04:25
  • @NickyC By "relocate itself", I mean after the pointer to that address doesn't point to that address no more, that address should be able to be relocated by other pointers and thus, the garbage value could be reassigned to a new valid value, and I'm thinking in this way, the garbage collection process can be replaced by "replace a garbage value with a new valid value", rather than collect the garbage value and then put in a new value when the new pointer points to that address". Does it make sense to you? But yea, I think releasing itself is also another angle of thinking the same problem. Thx – Hang Sep 28 '17 at 11:48
  • @NickyC Thanks man! I'm looking at it – Hang Sep 28 '17 at 11:49

1 Answers1

4

There would be no way to implement this without some kind of reference-tracking.

Consider the following modification of your code:

int* y;
// somewhere else:
int* x = new int(10);
y = x;
x = new int(12);  // oops: y has now unexpected become invalid

In other words, the memory location pointed to by x has been copied to another pointer, y, and then x has been reassigned. If the reassignment of x automatically destroyed the data that it previously pointed to, then y would also become invalid.

That would be undesirable behavior, so you'd need some kind of reference tracking system to ensure that the data pointed to by x was not deleted unless x was the only reference to that data.

And now you've just reinvented (a type of) garbage collection.

In fact, though, C++ does have an elegant way of achieving this type of reference tracking: via RAII, or what I prefer to refer to as "scope-bound resource management". Specifically for pointers to memory locations, you would use a smart pointer class to automatically manage the allocation and deallocation of memory as necessary.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Wow! Yes! Your answer makes sense to me! This situation does happen for sure in some program, but actually I'm asking a different thing. I might have been ambiguous. In your situation, you seem to assume that when the value is not pointed to anymore, it gets "released" itself, or, destroyed itself, but my thinking is this value doesn't have to be destroyed at the point, but the address contains this value can be, say, added a flag saying "hey, I'm garbage, welcome to replace my value and reallocate me". But yes, although I'm thinking in this way, your above situation will also crash since – Hang Sep 28 '17 at 11:58
  • since if y=x, and after x is reassigned, then the value in that address will be get confused like "Or, am I now a garbage or what?", right? Then that requires additional effort to make sure of the same thing, too. Thanks for your answer, I get it now :) – Hang Sep 28 '17 at 12:00