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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:04:18+00:00 2026-05-27T02:04:18+00:00

I have a quick question on the implications of returning a pointer to a

  • 0

I have a quick question on the implications of returning a pointer to a vector or map of pointers from a public member function in a class.

Here is my code:

here is a member function in class B

vector<A*>* ClassB::getfunction(){
    returns m_test;
}

m_test is a private data member of class B of type pointer to a vector of pointers. This object would be initialized on the heap so I would need to delete it in the class destructor (including all the elements in the vector).

I would then use the function as shown below.

B* ex_B = new B();

vector<A*>* ex_ptr_vecA = new vector<A*>;  

ex_ptr_vecA = ex_B->getfunction();

My question:

Since I have two objects that’s memory is both allocated on the heap will I need to delete them both?

Does the order matter and would the following be correct?

B* ex_B = new B();

vector<A*>* ex_ptr_vecA = new vector<A*>;  

ex_ptr_vecA = ex_B->getfunction();

//do something with ex_ptr_vecA
//Then I want to delete the allocate memory to the heap

//delete class B first
delete ex_B;

//and then the vector
//need to loop over vector elements and delete one at a time
for(int i =0; i < ex_ptr_vecA; i++){
     delete ex_ptr_vecA->at(i);
}

delete ex_ptr_vecA;

Would this be correct or would I be trying to delete dangling pointers because the destructor of class B has already de-allocated the heap 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-27T02:04:19+00:00Added an answer on May 27, 2026 at 2:04 am
    B* ex_B = new B();
    vector<A*>* ex_ptr_vecA = new vector<A*>;
    

    Here you allocated 2 blocks of memory and stored the adress of the beginning of those blocks in corresponding pointer variables. All fine.

    ex_ptr_vecA = ex_B->getfunction();
    

    Now you’ve reassigned ex_ptr_vecA with whatever address getfunction() returns. You no longer hold the address that new vector<A*> returned, you can’t free it anymore, hence you’ve got a memory leak.

    If you only need the pointer to ex_B‘s internal vector<A*> then simply say this:

    vector<A*>* ex_ptr_vecA = ex_B->getfunction();
    

    The way you showed in your example code ex_ptr_vecA points to the same vector that you’ve allocated in ex_B class. If ex_B deletes that internal vector in it’s destructor, then delete ex_ptr_vecA; means you’re deleting twice – that’s undefined behaviour.

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

Sidebar

Related Questions

I have a quick question regarding the proper way to access Javascript class member
I have a quick question. If I have an interface from which my class
I have a quick question about fetching results from a weakly typed cursor and
I have a quick question about how to serve data from a repository in
I have a quick question about getting the value from control which I added
I have a quick question. I have a function like this: function clean_num($num){ return
I have a quick question about Java synchronization. Please assume the following code: public
I have a quick question about Java synchronization. Please assume the following code: public
I have a quick question. I'm matching class names of LI elements to turn
I have a quick question regarding getting a specific value from a data grid,

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.