Couple of Q's.....

This is a discussion on Couple of Q's..... within the C++ Programming forums, part of the General Programming Boards category; Thats right, local objects are not destroyed. Globla objects may be....

  1. #16
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Thats right, local objects are not destroyed. Globla objects may be.

  2. #17
    Registered User deadpoet's Avatar
    Join Date
    Jan 2004
    Posts
    50

    Cool

    Yes, I agree that local object are not destroyed as in the following case:

    Example 1: Local FOO Using Return
    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();
      return ( EXIT_SUCCESS );
    }
    
    
    ###############OUTPUT###############
    # ./foo
    Hello, foo x val=0
    foo destructor
    Example 2: Local FOO Using Exit
    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
    Now let me clean this up a little to simulate a more realistic approach:

    Example 3: Global FOO Using Return
    Code:
    //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
    Example 4: Global FOO Using Exit
    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
    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; } .

    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.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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:
    Code:
    int main()
    {
      boost::scoped_ptr<FOO> fooptr(new FOO);
      (exit type)(1);
    }
    return calls the smart pointer destructor and thus deletes and destructs the dynamic object, exit doesn't.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  2. Pokemon talk and a couple useless stories
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 02-01-2003, 02:49 PM
  3. Thought I'd registered last couple of months...
    By Grayson_Peddie in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 08-31-2002, 09:34 PM
  4. Couple Code Snippets
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 01:23 AM
  5. Replies: 5
    Last Post: 11-13-2001, 03:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21