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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:33:07+00:00 2026-06-18T14:33:07+00:00

I have a fairly simple question, but cannot wrap my head over it. Consider

  • 0

I have a fairly simple question, but cannot wrap my head over it.

Consider I have this code:

#include <iostream>
#include <vector>
using namespace std;

class B
{
public:
    B(const int& val) {this->val = val;}
    int val;
};

class A
{
public:
    A() {}
    void Set(B& ptb)
    {
        ptBs.push_back(&ptb);
    }
    void Set(const int& val)
    {
        ptBs.push_back(new B(val));
    }
    std::vector<B*> ptBs;
};

int main()
{
    A* ptA = new A();
    ptA->Set(B(10));
    ptA->Set(38);

    for (int i=0; i<ptA->ptBs.size(); i++)
        cout << ptA->ptBs[i]->val << endl;

    delete ptA;

    system("pause");
    return 0;
}

The output result is:

10
38

But I think there is memory leak is going on in void Set(const int& val) if I won’t call delete with array elements, created by this method.

How can I say, which elements of std::vector have been created on heap so I could free memory in ~A() destructor like this:

~A()
{
    for (int i=0; i<ptBs.size(); i++)
        delete ptBs[i];
}

And do I have to delete vector elements, created via temporary new operation call at all?

Probably I don’t see something very simple here, but I really need this functionality in my application.

PS. 10 and 38 are just a simple example. Set function can be called thousands of times with different arguments.

  • 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-18T14:33:08+00:00Added an answer on June 18, 2026 at 2:33 pm

    Fortunately, this line won’t compile:

    ptA->Set(B(10));
    

    This is because B(10) is a constructor cast expression which creates a prvalue temporary of type B; a prvalue cannot bind to the lvalue reference B & parameter to void A::Set(B& ptb). This is the C++ language protecting you from the consequences of storing a dangling pointer to a temporary value.

    It usually makes more sense for A to store its B items by value:

    std::vector<B> Bs;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe this is a fairly simple question but I have no idea where
A fairly simple question for which I have a guess, but I can't find
First I apologize if this is a fairly simple question, but I am a
This is a fairly simple question, but it's one I can't find a firm
I actually have a fairly simple question but I'm unable to find an answer
I have a fairly simple question, but I can't seem to find a simple
I know this must be a fairly simple question, but I haven't managed to
I thought my question would have a fairly simple answer, but my research has
I have a fairly simple system, and for the purposes of this question there
I have a fairly simple question, but I'm failing to find the solution. I

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.