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 6099897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:19:32+00:00 2026-05-23T13:19:32+00:00

After reading some tutorials I am still unclear on some points about memory management

  • 0

After reading some tutorials I am still unclear on some points about memory management in C++.

1.
when a class declared with the new operator goes out of scope is its destructor called and memory freed?
Is it necessary to call the delete operator to free the class’ memory and have its destructor called?

class Test{};

void newTest(){
    Test *t = new Test;
}

int main()
{
    newTest();
    return 0;
}

2.
Are variables (such as a vector) declared with the new keyword freed when the class they reside within is destroyed?
Do those variables have to be explicitly deleted in the class’s destructor?

class Test{
    vector<int> *vec;
public:
    Test();
};

Test::Test(){
    *vec = new vector<int>;
}

void newTest(){
    Test t;
}

int main()
{
    newTest();
    return 0;
}

3.
Same as question 2, but with normal variables declared on the stack.
In the following example, is the vector vec deleted when t goes out of scope?

class Test{
    vector<int> vec;
};

void newTest(){
    Test t;
}

int main()
{
    newTest();
    return 0;
}

4.
Finally, which is the better way to declare a vector in a class, normally (on the stack) or with the new keyword (on the heap)?

  • 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-23T13:19:32+00:00Added an answer on May 23, 2026 at 1:19 pm

    It sounds like you may come from a Java background, so things with new work a bit differently in C++.

    1. The memory allocated with new must be explicitly destroyed with delete. This example has a memory leak.

    2. Similar to question 1, you must delete the memory in the class destructor to reclaim it.

    3. In this case the vector will be destructed automatically.

    4. Within a class, always prefer to store members by value (as opposed to with new) unless you have a specific reason to do otherwise. Just because it’s stored by value within the class doesn’t mean it’s on the stack, it’s in the same region of memory that the containing class occupies.

    Finally note that in C++ you can use RAII to make memory management much less error prone. Take a look at shared_ptr, scoped_ptr, and unique_ptr for example. These all handle automatically freeing the newly allocated memory when appropriate.

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

Sidebar

Related Questions

I've always used a *.h file for my class definitions, but after reading some
I am new to JPA & Hibernate. After reading some online materials I now
After reading some reports about the inadequacies of the Mono 2.6 garbage collector, I
After reading some threads on misuses of exceptions (basically saying you don't want to
After reading a couple of answers and comments on some SQL questions here, and
After reading a bit more about how Gnutella and other P2P networks function, I
After reading Practical Common Lisp I finally understood what the big deal about macros
After reading Evan's and Nilsson's books I am still not sure how to manage
after searching up and down, reading all possible articles and tutorials, I grasped the
i am new to iPhone and cocos2d in general, i m reading tutorials and

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.