-1

I have this in the main.cpp body (how is it called? global?)

ofstream s;
...
int main ... {
    s = ofstream("somefile.txt");
...

then another thread uses it once a day to rollover:

s.close();
// do I need to cleanup in between?
s = ofstream("anotherone.txt");

do I need to cleanup before creating new stream?

Boppity Bop
  • 9,613
  • 13
  • 72
  • 151
  • 1
    Are you asking whether there's some general concept of "cleaning up variables" in C++, or are you asking specifically about the mechanics of the `ofstream` class? – Sneftel Apr 09 '22 at 15:49
  • any object - do I need to delete it somehow? wont it leak memory after `s = ofstream("anotherone.txt");`? – Boppity Bop Apr 09 '22 at 15:54
  • I guess it depends on how well the class is defined. But, in simple terms, compare to a basic type: `int* p = new int; *p = 3; /* .do stuff */ *p = 4; delete p;` Do we leak memory here? Do we have to delete `p` and re-create between the two assignments? – Adrian Mole Apr 09 '22 at 16:03
  • NB: this is not class. its just a cpp file with `main` in it and the `s` is declared outside `main`. also you use `new` - I know what to do with it. I dont know what to do with var which is declared simply as `T v` and then `v = T();` <- is this same as `v = new T()` ?? then I should use `delete &v`? not sure it will compile – Boppity Bop Apr 09 '22 at 16:07

1 Answers1

2

close() is a member function defined in the class ofstream. It's not a destructor; it's not special in any way which fundamentally matters to the rules of the C++ language. For that matter, s = ofstream("anotherone.txt") is actually just another way to spell the line of code s.assignment_operator(ofstream("anotherone.txt")), except that instead of assignment_operator it's spelled operator=. (Note that in this case, the value which is passed to the member function is a temporary value, and is destroyed after the function returns. That doesn't necessarily imply anything specific about what happens with opening and closing files; it's just how the language works.)

A global variable exists throughout the entire life of the program; the precise semantics during startup and shutdown are a little tricky, but certainly during the "main phase" they always exist. The design of ofstream may put s into a different sort of state when s.close() is called, but that's entirely down to the design of ofstream, and is something that you'd discover by reading the documentation of that class rather than by reading the rules of the language.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • thank you. but i am still unclear. i need simple yes or no - if i "assign" var like `s = class_a("somefile.txt");` and later `s = class_a("anotherone.txt");` - will there be a memory leak? or c++ compiler cleverly write some deallocation for `somefile` instance of the var before assigning `anotherone` ? – Boppity Bop Apr 09 '22 at 16:18
  • 1
    @BoppityBop **If the class in question is well-written,** your "yes/no" question is the same as asking "If I assign a variable like `int s = 2;` and later `s = 4;` will there be a memory leak?" – JaMiT Apr 09 '22 at 16:28
  • idk how do you compare a class to an integer but ok. yes that what I am asking. – Boppity Bop Apr 09 '22 at 16:31
  • @BoppityBop: How “you compare a class to an integer” is more or less the definition of well-written classes in C++. – Davis Herring Apr 09 '22 at 17:19
  • 1
    @BoppityBop Again, it comes down to how the class in question is implemented. As it happens, `ofstream` is "well-behaved"; it relies on the ["move semantics"](https://stackoverflow.com/questions/3106110/what-is-move-semantics) introduced in C++11 to make assignment "just work". But the Standard Library in general was written by smart people, so it's not *that* easy to screw things up with almost any of its facilities. – Sneftel Apr 09 '22 at 19:19