Thread: Objects

  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
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 05:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11: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