Script execution order
Jul 29, 2011 at 5:29pm UTC
Well I am kind of confused with the execution order in the following script, I want to know if the delete function is executed within each condition or sometimes it is skipped :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
void Students::addSubject(std::istream& in)
{
Custom* sys = new Custom;
std::string last;
in>>last;
if (last == "x" )
{
if (Hw.empty())
{
std::cout<< "\n>>" <<"You must at least enter 1 homework subject." << std::endl;
std::cout << "\nEnter subject: " ;
in.clear();
addSubject(in);
}
else
{
std::cout<< ">>" <<" Subjects data completed." << std::endl;
}
}
else if (last != "x" )
{
std::string editedSubj = sys->toLower(last);
if (Hw.find(editedSubj) != Hw.end())
std::cout<< "\n>>" <<last << " is a duplicated subject." << std::endl;
else
{
Hw[editedSubj] = (double )-1;
std::cout<< "\n>>" <<last << " subject was added to homework list.\n" << std::endl;
std::cout<< std::string(80,'-' )<<std::endl;
}
std::cout << "\nEnter subject ( 'x' to finish) : " ;
in.clear();
addSubject(in);
}
delete sys;
}
Jul 29, 2011 at 11:06pm UTC
Custom sys;
Now you don't have to worry.
The delete will not be executed if an exception is thrown.
Jul 30, 2011 at 4:31am UTC
I know i can use that :p , but i want to know just in case. So In the above the delete will be executed within all condition, what if there is a return statement in one of the conditions. like that?
1 2 3 4 5 6 7 8 9 10 11 12 13
void function()
{
Class* thing = new Class;
if {
...
return ;
}
else {
....
return ;
}
delete thing ;
}
Last edited on Jul 30, 2011 at 4:33am UTC
Jul 30, 2011 at 4:42am UTC
Then you have to put a delete
in front of the return. However, even if you do that, you are still screwed if someone throws an exception.
Topic archived. No new replies allowed.