Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, July 14, 2016

what happens to unused memory in C++

what happens to unused memory in C++


I was going through some practice coding problems , and i came across one -

Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.

EXAMPLE:
Input: the node ?c? from the linked list a->b->c->d->e
Result: nothing is returned, but the new linked list looks like a->b->d->e

The solution to this is to simply copy the data from the next node into this node and then delete the next node.

The above solution is keeping java in mind, as the programming language. I am wondering what would happen to the content of deleted node? Will its fate be different in java and c++? And also, i think the perfect answer to this question should also deallocate the memory of the deleted node. How do we do that in c++?

Answer by phihag for what happens to unused memory in C++


In Java, the memory could eventually be freed by the garbage collector. There is no guarantee of when (and therefore, if) that happens. (In practice, it will usually happen very quickly).

In C++ with smart pointers, the deallocation of memory is guaranteed as soon as the object cannot be addressed anymore. In effect, it works pretty much like garbage collection in Java as long as you don't have any circular reference graphs.

If you are not using smart pointers, you'll have to manually call delete c (or free(c)). If you don't do this, your program will have allocated memory it can never use again.

Answer by orlp for what happens to unused memory in C++


Well, that's not entirely what you do in C++. What you should do/what happens:

  1. Delete the memory pointed to by c.
  2. Copy the pointer to the memory from d to c.
  3. Copy the pointer to e from d to c.

Now effectively we took d out of the list and made c behave like it were d. And finally, if d itself is allocated somewhere too and must be deleted seperately you delete it after 3.

Why 1? Because in Java if an object is no longer used it is automatically garbage collected. In C++ this is not the case.

Answer by hammar for what happens to unused memory in C++


In Java it will eventually get collected as garbage. In C++ you have to deallocate it explicitly using the delete keyword.

Once the memory has been freed, the actual bits will stay intact but might be reallocated and overwritten at any time.

Answer by DuncanACoulter for what happens to unused memory in C++


In response to the Question Title "What happens to unused memory in C++" the memory is marked as unallocated by the memory manager but is probably not returned to the Operating System at that point (if ever) for efficiency's sake. The deallocation is made via the delete keyword

For your specific example (hastily cobbled together) :

Assume a node Such as

struct Node  {      Node* next;  } head;       //Later...    Node* pred = head; //Where head is the start of the list  while((pred->next != NULL) && (pred->next != c))  {     pred  = pred->next;            }    Node* toDelete = pred->next;  Node* newSuccessor = toDelete->next;    /*    This is the moment when the memory is considered freed.     free() should only be used if the node was allocated with malloc()  */  delete toDelete;     pred->next = newSuccessor;   

Answer by Mud for what happens to unused memory in C++


should also deallocate the memory of the deleted node.

Yes.

How do we do that in c++?

With the delete operator.

You probably getting ahead of yourself if you're working on link lists and don't know about the delete operator. You don't want to skip steps when learning C or C++; it will bite your head off later.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.