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

  • Home
  • SEARCH
  • 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 9101253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:04:54+00:00 2026-06-17T01:04:54+00:00

Possible Duplicate: Why does the use of ‘new’ cause memory leaks? I’m fairly new

  • 0

Possible Duplicate:
Why does the use of ‘new’ cause memory leaks?

I’m fairly new to STL, and I’ve read that it is good practice to generally keep vectors of objects rather than vectors of pointers to objects. In an attempt to comply with that creed I ran into the following scenario:

//Approach A
//dynamically allocates mem for DD_DungeonRoom object, returns a pointer to the block.
//then, presumably, copy-constructs the de-referenced DD_DungeonRoom as a 
//disparate DD_DungeonRoom object to be stored at the tail of the vector
//Probably causes memory leak due to the dynamically allocated mem block not being
//caught and explicitly deleted
mvLayoutArray.push_back(*(new DD_DungeonRoom()));

//Approach B
//same as A, but implemented in such a way that the dynamically allocated mem block
//tempRoom can be deleted after it is de-referenced and a disparate DD_DungeonRoom is
//copy-constructed into the vector
//obviously rather wasteful but should produce the vector of object values we want
DD_DungeonRoom* tempRoom = new DD_DungeonRoom();
mvLayoutArray.push_back(*(tempRoom));
delete tempRoom;

first question: In Approach A, is a memory leak created?
second question: assuming A does produce a memory leak, does B solve it?
third question: is there (or more likely, ‘what is’) a better way to add custom class objects (e.g. requiring dynamic allocation via ‘new’ or ‘malloc’) to a vector by value?

thanks,
CCJ

  • 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-06-17T01:04:55+00:00Added an answer on June 17, 2026 at 1:04 am

    first question: In Approach A, is a memory leak created?

    Yes.

    second question: assuming A does produce a memory leak, does B solve it?

    Yes, but it’s a silly solution. And unsafe in the event that the DD_DungeonRoom‘s copy constructor or vector::push_back throws an exception.

    third question: is there (or more likely, ‘what is’) a better way to
    add custom class objects (e.g. requiring dynamic allocation via ‘new’
    or ‘malloc’) to a vector by value?

    No objects in C++ require dynamic memory allocation. Just add objects directly to the vector calling the constructor, sans new.

    mvLayoutArray.push_back(DD_DungeonRoom());
    

    Even better, if your compiler supports the feature (it is new to C++11), would be to use emplace_back which completely bypasses any copies, and constructs your object directly in the vector. Just pass the same arguments to it as you would to your constructor. In our case, that’s none:

    myLayoutArray.emplace_back();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why does the use of ‘new’ cause memory leaks? What is the
Possible Duplicate: When should I use a javascript framework library? All, I'm fairly new
Possible Duplicate: Why does this intentionally incorrect use of strcpy not fail horribly? Below
Possible Duplicate: How does the (function() {})() construct work and why do people use
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: What does “(void) new” mean in C++? I'm not familiar with C++
Possible Duplicate: Does anyone know of a good JSON time server? Is there any
Possible Duplicate: Use of var keyword in C# Being relatively new to C# I
Possible Duplicate: Why does gcc have “â” in all its error messages? New to
Possible Duplicate: Why does it appear that my random number generator isn't random in

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.