Objects

This is a discussion on Objects within the C++ Programming forums, part of the General Programming Boards category; Hi, How can we delete the objects initialised by the pointers automatically in a program without delete[]/delete keyword. Like for ...

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    Objects

    Hi,
    How can we delete the objects initialised by the pointers automatically in a program without delete[]/delete keyword. Like for example java explicitly runs the garbage collector. Can we do that same way somehow??
    thank you.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448

    nope

    No, in C/C++ you don't have a garbage collector. In these languages you have to do most of the stuff by yourself. That's why they're so powerful (and not very good for begginers).
    Although I think there is some third-party code you can add to your app to make a sort of garbage collector, they're very slow. Wouldn't recommend it. Besides, it's a good practice to tidy up after you, even in programming.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Use something from the standard library like an auto_ptr...
    Code:
    #include <memory> //for auto_ptr
    #include <iostream>
    using namespace std;
    template <class T>
    ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
    {
    
      if (p.get() == NULL) 
           strm<<"NULL";
       else
         strm<<*p;
       return strm;
    }
    
    int main(void)
    {
    int x;
    auto_ptr<int> aPtr(new int); //create an auto_ptr object and allocate mem
    *aPtr=1;
    cout<<aPtr<<endl; //only works with overloaded << operator 
    auto_ptr<int> aPtr2(aPtr); //now aPtr2 is equal to aPtr 
    cout<<aPtr<<endl; //ERROR now aPtr is deleted
    cout<<aPtr2;
    //the end
    cin>>x; //just a quickie pause 
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    MSVC++ .NET has a garbage collector. I haven't had any problems with using it although typically I like to clean up after myself anyway.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    For single objects, use std::auto_ptr or boost::shared_ptr (preferred).

    For arrays, use std::vector (preferred), or boost::shared_array.

  6. #6
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    MSVC++ .NET has a garbage collector.
    Are you talking about managed or unmanaged code? Because if your talking straight C++ there is no garbage collector.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,638
    Here is a simple garbage collector example I wrote on the C board. Unfortunately, it hasn't been compiled, so you're on your own debugging-wise. But the basic concept is simple:

    1) store the address of the pointer.
    2) store the data that the said pointer points to.

    ie:

    char * ptr = new char[10240];
    (1) char ** pa = &ptr;
    (2) char * p = ptr;

    So that:

    if(*pa != p) delete [] p;

    Of course you'll need a list or vector of these memory managers.

    Also, you can set up the garbage collection in a low-priority thread, but in most cases, there's no real justification to do so.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 04:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM

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