Apurv Patani
Apurv Patani

Reputation: 23

When do stream object get deleted?

Here's the text from the book that I referred to

According to what I read in a book(I have attached an image of the text from the book above), when a stream object goes out of scope, its connection with the program/file or whatever is closed but it still remains in the memory with the buffer that it is associated with. So when does it deleted from the memory?

Any help is appreciated!

Thanks!

Upvotes: 2

Views: 155

Answers (2)

cdonat
cdonat

Reputation: 2822

No, either your book is wrong, or you misunderstood it. When an fstream object is destroyed (i.e. is out of scope), its file descriptor will be closed and its stream_buffer will be deleted ass well.

Just guessing: Maybe the book is mixing pointers to fstream objects with fstream objects them self. When a pointer goes out of scope, the object it points to will not be destroyed.

Upvotes: 1

Paolo M
Paolo M

Reputation: 12777

The wording of your textbook is quite misleading.

When an object goes out of scope, it gets destroyed. When a stream is destroyed, its connection gets closed as if you would have manually called the close() function. Then, all the memory associated with the stream is released.

The author underlines that the opposite does not hold, i.e. if you call the close() method of a stream, you are not destroying it, you are just closing the connection with it.

Upvotes: 5

Related Questions