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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:05:58+00:00 2026-05-25T03:05:58+00:00

I am new to C++ but I have some basic memory allocation knowledge in

  • 0

I am new to C++ but I have some basic memory allocation knowledge in C. I am writing a class Card, which stores the card number and a list of class Activity object.

class Card {
    public:
    Card();
    ~Card();
    vector<Activity> activities;
    int cardNo;
}

Currently, I initialize the Activity object using code like:

Activity a = Activity("a"); 

and push them to the vector defined in the Card object.

But I found people tend to initialize using Activity *a = new Activity(“a”) instead (dynamically allocation?), and the objects declared in the former way (statically allocated?) will be freed when the function declares them terminated.

Then, if I initialize Activity objects the same way I did before, but initialize Card using the “new Card()” way, is it possible that the Activity objects may have been de-allocated before Card object freed? Should I switch to use “new Activity()” to initialize objects stored in Card?

  • 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-25T03:05:59+00:00Added an answer on May 25, 2026 at 3:05 am

    No, what you’re doing is fine. When you push an object onto a vector, a copy is made. So when your function returns, your a is destroyed, but the vector you added it to still has its own seperate copy.

    One reason someone might allocate an instance of a class dynamically and push it onto a vector would be that copying objects of that particular class around is expensive (and vector does a lot of copying around internally) and they want to avoid that, so they store pointers instead of objects so that only copies of the pointers are made, not of the objects (which is would not be nearly so expensive). That all depends on the class though; generally you can use vectors of objects without any performance issues.

    Note: a shortcut1 for Activity a = Activity("a"); is Activity a("a"), or better, do what Benjamin suggested and do activites.push_back(Activity("a")) if you’re not performing some operations on the Activity before you push it.

    1 It’s not really a shortcut because it does something different, but for your intents and purposes, it is.

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

Sidebar

Related Questions

I'm quite new to C++ but have some basic C knowledge. In my past
I'm new with C#, I have some basic knowledge in Java but I can't
I'm new to NAnt but have some experience with Ant and CruiseControl. What I
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
I am using the new ASP control Chart, but I have some problems with
Hi I am quite new to php but i have been following some tutorials
Hey, i have some experience with MVC. but I'm new to rails. I'm using
I am new to MySQL, but I have used Oracle for some time. I
Hi I'm very new to sql but have been passed a job in which
I am quite new on mvc3 and have some basic and ridiculous questions that

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.