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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:07:52+00:00 2026-05-31T22:07:52+00:00

I want to store 10 Obj object in objList , but i don’t know

  • 0

I want to store 10 Obj object in objList, but i don’t know when is appropriate use delete in this case. If i use delete Obj; in the line where i note in below code, will the Obj still be stored in objList?

struct Obj {
    int u;
    int v;
};

vector<Obj> objList;

int main() {
    for(int i = 0; i < 10; i++) {
        Obj *obj = new Obj();
        obj->u = i;
        obj->v = i + 1;
        objList.push_back(*obj);
        // Should i use "delete Obj;" here? 
    }
}
  • 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-31T22:07:54+00:00Added an answer on May 31, 2026 at 10:07 pm

    Any object you create on the heap with new needs to be cleared up by delete. In your code, you are actually storing a copy in your collection.

    objList.push_back(*Obj);
    

    What this line is doing step by step is:

    1. Indirecting a pointer to the underlying heap memory Obj occupies (returning the object Obj)
    2. Calling the copy constructor to create a temporary copy
    3. Storing the temporary copy in the collection

    You do not need to create this initial Obj on the heap, a stack allocated local will suffice as @Luchian Grigore has pointed out.

    Obj obj;
    objList.push_back(obj);
    

    You do not need to call delete on the copy in the collection, the STL collection will handle this memory itself when you remove the element, but you still will need to delete the original heap allocated object.

    It would be better if you stored your objects by std::shared_ptr. That way delete would be called when all references to the Obj were removed.

    std::vector< std::shared_ptr< Obj > > vec;
    vec.push_back( std::make_shared( new Obj() ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store a large number of sound files in a database, but
I'm new to Objective-C, but experienced in C++ and C. I want to store
So, I want to define a singleton method for an object, but I want
Let's assume I have some MyObject obj object and want to access a property
I'm using Devart to execute DB object and Store Procedure mapping. Now i want
I want to store the current URL in a session variable to reference the
I want to store the data returned by $_SERVER[REMOTE_ADDR] in PHP into a DB
I want to store a a c# DateTimeOffset value in a SQL Server 2005
I want to store values in a bunch of currencies and I'm not too
I want to store a large result set from database in memory. Every record

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.