I’m working with a (C++) library, where the object needs to be initialized with a stream. The sample code provided with the library uses this code:
// Declare the input stream
HfstInputStream *in = NULL;
try
{
// This sets up the special stream object for the later object (see below)
in = new HfstInputStream("pathToFile.hfsto");
}
// catch errors...
// {omitted}
// Initialize the object
while (not in->is_eof()) {
if (in->is_bad())
{
std::cerr << "ERROR: Stream cannot be read." << std::endl;
exit(1);
}
// Here we initialize the object using the stream
HfstTransducer t(*in);
}
My problem is that this object cannot be used outside of the while loop because of scoping. and I have to declare it with the stream (as far as I can tell), so I can’t declare and then initialize it with the stream inside the loop.
My questions are (1) am I wrong? Can I actually declare it outside the loop somehow? (2) is there another (better) way to do this that avoids the loop altogether. For example, if I were to use try/catch and catch the exceptions.
I’m very new to C++ and looking to find out best practices, so please let me know what’s what. Thanks.
Also, to be clear, I’m looking to make a class that would use a persistant version of this object so I don’t have to constantly create/destroy these objects every time I need to use them.
PS: here’s a link to the documentation for the object if it is relevant
EDIT: If I try to declare the variable outside the loop and then initialize it I get an error
HfstTransducer t;
while (not in->is_eof()) {
t(*in);
}
// ERROR: main.cpp:47:0 main.cpp:47: error: no match for call to '(hfst::HfstTransducer) (hfst::HfstInputStream&)'
Am I trying to initialize it incorrectly?
In order to achieve what you need, you have to have a pointer to the object declared outside of the while scope like that:
This will declare and initialize an object of HfstTransducer type into the heap. This means that the destructor will not be called on its own after you leave scope but that you have to explicitly call it by calling:
EDIT: To answer to your general question about pointer vs normal object:
This is a very important part of C/C++.
An article to better explain this can be seen here.
If I were to explain it in a few words by doing:
You are declaring an object of that type and putting it in the stack. Its life-time lasts ONLY until the end of the scope. You are not able to access it out of scope, since its destructor will be called as soon as the scope ends.
On the other hand
initializes t as an object of HfstTransducer type and places it in the heap. For what the heap is refer to the article above, but it basically is the memory allocated to your program by the operating system. In C++ you ask for memory in the heap with the operators new and free it with the delete operator. In C you achieve the same with the free() and malloc() functions.
So something in the heap is alive for the whole duration of your program unless you explicitly call its destructor. As you can do in the example by invoking delete t;
Failure to do so leads to what all C/C++ programmers have to face, the so called memory leaks. Which is basically memory you thought is free but is not because you forgot to delete/free it.