Thats right, local objects are not destroyed. Globla objects may be.
Printable View
Thats right, local objects are not destroyed. Globla objects may be.
Yes, I agree that local object are not destroyed as in the following case:
Example 1: Local FOO Using Return
Example 2: Local FOO Using ExitCode:#include <stdlib.h>
class foo {
public:
int x;
foo( int i ) {
x = i;
}
void print( void ) {
printf( "Hello, foo x val=%d\n", x );
}
~foo(){
printf( "foo destructor\n" );
}
};
int main ( void ) {
foo FOO( 1);
FOO.print();
return ( EXIT_SUCCESS );
}
###############OUTPUT###############
# ./foo
Hello, foo x val=0
foo destructor
Now let me clean this up a little to simulate a more realistic approach:Code:#include <stdlib.h>
class foo {
public:
int x;
foo( int i ) {
x = i;
}
void print( void ) {
printf( "Hello, foo x val=%d\n", x );
}
~foo(){
printf( "foo destructor\n" );
}
};
int main ( void ) {
foo FOO( 1 );
FOO.print();
exit ( EXIT_SUCCESS );
}
###############OUTPUT###############
# ./foo
Hello, foo x val=0
Example 3: Global FOO Using Return
Example 4: Global FOO Using ExitCode://FileName: foo.cc
#include "foo2.h"
foo FOO(1); // FOO defined at a global level.
int main ( void ) {
FOO.print();
return ( EXIT_SUCCESS );
}
// File Name: foo2.h
#ifndef _FOO2_H_
#define _FOO2_H_
#include <stdlib.h>
class foo {
public:
int x;
foo( int i );
~foo();
void print( void );
};
#endif
// File Name: foo2.cc
#include "foo2.h"
foo::foo( int i ) {
x = i;
}
void foo::print( void ) {
printf( "Hello, foo x val=%d\n", x );
}
foo::~foo(){
printf( "foo destructor\n" );
}
###############OUTPUT###############
# ./foo
Hello, foo x val=1
foo destructor
And if FOO was created using foo FOO = new foo( 1 ) then niether return nor exit will call the destructor. One would need to do something like if ( FOO ) { delete FOO; } .Code://FileName: foo.cc
#include "foo2.h"
foo FOO(1); // FOO defined at a global level.
int main ( void ) {
FOO.print();
exit ( EXIT_SUCCESS );
}
// File Name: foo2.h
#ifndef _FOO2_H_
#define _FOO2_H_
#include <stdlib.h>
class foo {
public:
int x;
foo( int i );
~foo();
void print( void );
};
#endif
// File Name: foo2.cc
#include "foo2.h"
foo::foo( int i ) {
x = i;
}
void foo::print( void ) {
printf( "Hello, foo x val=%d\n", x );
}
foo::~foo(){
printf( "foo destructor\n" );
}
###############OUTPUT###############
# ./foo
Hello, foo x val=1
foo destructor
So, with this being said, I understand your point and see the validity in your statements. Everything must be put into the context of what you are accomplishing. If the objects are only local object then yes return should be invoked however, if the objects are global or created with new then it really does not matter and should be left to the coders choice. I say this because if the object is global then both return and exit invoke the destructor. If the object is created using new then niether return nor exit will invoke the destructor; the memory must be freed using delete.
This has been a great topic and has led me to look into a few of the elements of C++ that I have taken for granted. I can see the merit in the argument for the use of return.
DeadPoet.
There shouldn't be any global objects. But there may be singletons that use static class members, which results in the same. Anyway, these get cleaned up regardless of the exit type.
However, I disagree on the point of dynamic memory, because I rarely use raw pointers. My code would be:
return calls the smart pointer destructor and thus deletes and destructs the dynamic object, exit doesn't.Code:int main()
{
boost::scoped_ptr<FOO> fooptr(new FOO);
(exit type)(1);
}
wow, this thread has gotten pretty big! can u give me a nice, easy, correctly formated example of a subroutine? that other guy had put all his commands on the same line, which for me, is hard to read. also, please comment it alot. thx so much