c++ - Diffrence between calling function from destructor or giving it to atexit() -
i little confused — please help.
suppose c++ program has single class , single object of class. want perform cleanup related task , have written function that. should prefer calling method destructor or calling through atexit()
?
note: know purpose of c++ class destructors , atexit()
. affect performance? or benefits?
atexit()
register function called when program terminates (e.g. when main()
calls return
or when exit()
explicitly called somewhere).
it helps guarantee procedure performed every time program terminates, regardless of termination invoked.
when exit()
invoked, static objects destroyed (destructor called), not objects in local variable scope , of course dynamically allocated objects neither (those destroyed if explicitly call delete
).
if objects not static, may want register atexit()
function guarantee clean procedure performed, otherwise done automatically.
that is, if program uses exit()
@ all. if code designed main()
terminate normally, can place code there.
Comments
Post a Comment