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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:43:38+00:00 2026-06-11T22:43:38+00:00

In my application I am creating and returning an array filled with dynamically allocated

  • 0

In my application I am creating and returning an array filled with dynamically allocated objects from a derived class like this:

void someGetter(std:vector<DerivedClass> & returnV)
{
    BaseClass* base = object->clone();  // "object" is a "unique_ptr<BaseClass>"

    DerivedClass* derived = dynamic_cast<DerivedClass*> (base);

    if (derived != nullptr)
    {
        returnV.push_back(*derived);
    }
    else
    {
        delete base;
    }
}

This obviously creates a memory leak (valgrinds helps me here) because derived is never deleted.

I have tried to free the allocated memory like this:

delete &returnV[0];

It doesn’t give any compilation errors/warnings and the code still runs fine. but valgrind reports a few additional errors (invalid read, invalid free) on that line of code and the leak is still there.

Is there any way to free the memory returned like this? Or should i return unique_ptr’s instead of the objects ?

  • 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-11T22:43:39+00:00Added an answer on June 11, 2026 at 10:43 pm

    First off, the design seems very questionable to me: You have at once a polymorphic hierarchy, and also a container that holds values of a specific member of that hierarchy. There’s no end to the problems you’re inviting. It would seem far more sensible to have a std::vector<std::unique_ptr<Base>>.

    Anyway, here’s a moderately safe and efficient way to insert into the container only those objects whose dynamic type matches precisely. It assumes that every class in the hierarchy has an accessible copy constructor.

    void someGetter(std:vector<DerivedClass> & returnV)
    {
        if (typeid(*object) != typeid(DerivedClass)) { return; }
    
        returnV.insert(static_cast<DerivedClass&>(*object));
    }
    

    The semantics of this are slightly different from yours, because your code would allow the case where *object is of a strictly more derived type than DerivedClass, and the copying into the vector would slice the object. The present code does not suffer from this problem.


    Update (after your comment): If DerivedClass is indeed final (and please mark it as such!), then the following does without typeid:

    void someGetter(std:vector<DerivedClass> & returnV)
    {
        if (DerivedClass * p = dynamic_cast<DerivedClass *>(object.get()))
        {
            assert(typeid(*p) == typeid(DerivedClass));   // beware of slicing!
    
            returnV.insert(*p);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating application which is having functionality like 1 person can view video
I'm creating an application in Objective-C and I need to get the metadata from
I have a situation where I'm returning json objects to my application which are
Im creating application in java using XML. XML- <?xml version=1.0 encoding=UTF-8?> <songlist id=slist> <song
I am creating application where in i have used preferences for changing font settings.
I'm write installer for my web site. Installer creating application pool, virtual directory and
i creating an application with movie animation , i have a animation some group
am creating one application for student. I need to show digital clock with remaining
I'm creating an application that allows the user to store information about their classes.
I am creating an application and I need to connect to a database. The

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.