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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:04:12+00:00 2026-05-31T11:04:12+00:00

The following is the best minimum working example I can construct for now. I

  • 0

The following is the best “minimum working example” I can construct for now. I would like to understand if the following code leaks memory.

// Class CTest
class CTest {
  vector<int> Elements;
  CTest (vector<int>&);
  ~CTest ();
};
CTest::CTest (vector<int>& Elements_) {
  this->Elements = Elements_;
}
CTest::~CTest () {
}

// main
int main (int argc, char *argv[]) {
  vector<CTest> V;
  for (auto i = 0; i < 10; i++) {
    vector<int> U;
    for (auto j = i; j < i + 5; j++) U.push_back (j);
    V.push_back (*(new CTest (U)));
  }
  // Do whatever
  return 0;
}

Am I correct in thinking that since there isn’t a corresponding invocation of delete for each invocation of new, this programme does indeed leak memory?

  • 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-31T11:04:13+00:00Added an answer on May 31, 2026 at 11:04 am

    Yes, you are correct. Moreover, your code is of the type “trying very hard to get it wrong”, since vector is already a dynamic container and you have no reason to perform another dynamic allocation for your element (just to have it copied).

    There are many more ways to screw up. None of those are a particular design problem of C++, but there’s simply a limit to what the language can stop you from doing. Some more examples:

    int main(int argc, char **argv)
    {
        new double;  // leak
        delete static_cast<void*>(&argc);  // doesn't belong to you
        int a = *static_cast<int const *>(0x42); // not a valid pointer
    
        { T x; x.~T(); }  // call destructor of non-existent object
    
        { T y; y.~T(); new (static_cast<void*>(&y) T(); }
                       // impossible to recover from exception in T::T()
    }
    

    Fortunately, it is almost always fairly obvious that you’re doing something you’re not supposed to.

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

Sidebar

Related Questions

Given the one-table design given below how would the following best be queried The
I am a AS3 novice learning PureMVC and want to write code following best
How would the following scenario best be implemented: There is a standardized user interface
Following on from this question what would be the best way to write a
Would following the table below be the best way of determining the access type
I'm trying to take my code to the next level. Following some best practices
Following best practices, I'm using Thread.currentThread().getContextClassLoader().getResourceAsStream to load resources in a web application (like
I would like to hear opinions on below code snippet. Is there anything that
The following piece of code attempts to create a map that shows the minimum
If I have the following class, what's the best way of getting the exact

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.