Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:30:34+00:00 2026-05-30T02:30:34+00:00

So far I have been writing programs in Java. So when I started C++,

  • 0

So far I have been writing programs in Java. So when I started C++, the first thing that came to my mind was how to destruct/delete/finalize objects I don’t need anymore.

With Java I used to set them to null so the garbage collector was taking care of it.
However, I don’t know how things worth with C++. I found this article http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B which solved most of my questions. But there are still a few things I didn’t understand.

1) In Java there is a way to force the garbage collector to clean right on the spot(which is not always useful, since it waits for a few trash to stack up before running). Is there a way to do that with C++?

2) (C++) Also the opposite of the above, how can i make it so that i put the object on a state of “marked to be deleted” and the programm decides when to clean it (like Java)?

3) (C++)Should i force the barbage collector to clean right on the spot(i am pretty sure that’s not the right way but i am asking just to be sure)?

I would appriciate it if you could give a small code example with which code triggers what.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:30:35+00:00Added an answer on May 30, 2026 at 2:30 am

    C++ is very different than Java in this area, so here’s a brief overview:

    allocation: memory is set aside for an object.
    construction: The object is prepared to be used.
    destruction: The object “finishes” everything and disassembles itself.
    deallocation: the memory is given back to the system.

    int main() {
        int myint;  //automatic int object is allocated and constructed
        //stuff
    }   // when main ends, automatic int object is destroyed and deallocated
    
    int main() {
        int* mypointer;  //automatic pointer object is allocated and constructed
        mypointer = new int;  //dynamic int object is allocated and constructed
        //stuff
        delete mypointer; //dynamic int object is destroyed and deallocated 
    }   // when main ends, automatic pointer object is destroyed and deallocated
        // note: Pointers to _not_ delete the object they point to.
    
    class myclass {
        //members
    public:
        myclass() {} //this is the default constructor
        myclass(const myclass& rhs) {} //this is the copy constructor
        myclass& operator=(const myclass& rhs) {return *this} //this is the assignment operator
        ~myclass() {} //this is the destructor
    };
    

    When a function ends, all the variables in the function itself (which we call automatic) have their destructors called, and then they are deallocated automatically. This means for objects local to a function, they automatically clean themselves the instant the function ends. This also applies magically to members of a class. When it is destroyed, each of it’s members will automatically be destroyed. This means most destructors are empty.

    If you allocate stuff manually (with the new keyword), it must be destroyed and deallocated manually with the delete keyword. When you call delete, it will destroy (and deallocate) right there and then, and won’t continue until it is done. If you forget, it WILL NOT EVER GET DEALLOCATED (altough, some operating systems will deallocate it when your program ends).

    Since people make mistakes, the “correct” thing to do when you need dynamic objects is:

    int main() {
        std::unique_ptr<myclass> myptr = new myclass(); //allocate and construct
    } //both the unique_ptr and the dynamic object are destroyed and deallocated
    

    and the unique_ptr is smart enough to automatically clean up the thing it points at, freeing you for bigger concerns.

    The reason C++ does this is because if you have a object F that represents that file, it might have a exclusive lock on that file. In C++, once F is destroyed, you can immediately create an object G that uses that same file. In Java, there’s no guarantee that the finalizer will ever run, meaning that file may remain locked until your program ends. (Unlikely, but possible)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing a small java application (my first!), that does only a
Alright, so I'm writing this program that essentially batch runs other java programs for
I have tried writing program that plays a sound file but have been unsuccessful
I'm writing a web scraping program in C#. So far, I have been able
So far I have been creating Web Portal but recently I had a request
I have been looking for a SSO system. So far I have found CAS.
I have recently downloaded itrade , and so far I have been unable to
Im using JSoup with android and so far I have been successful. In my
Thus far you guys have been wildly helpful with me getting this little ditty
I have been using this so far system 'strings binary-file.dmp | grep search_string' Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.