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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:30:10+00:00 2026-06-14T15:30:10+00:00

I have a dynamically allocated array of polymorphic objects that I would like to

  • 0

I have a dynamically allocated array of polymorphic objects that I would like to resize without using STL library (vectors, etc). I’ve tried moving the original to a temporary array, then deleting the original, then setting the original equal to the temporary, like this:

int x = 100;
int y = 150;

Animal **orig = new Animal*[x];
Animal **temp = new Animal*[y];

//allocate orig array
for(int n = 0; n < x; n++)
{
    orig[n] = new Cat();
}

//save to temp
for(int n = 0; n < x; n++)
{
    temp[n] = orig[n];
}

//delete orig array
for(int n = 0; n < x; n++)
{
    delete orig[n];
}
delete[] orig;

//store temp into orig
orig = temp;

However, when I try to access the element for example:

cout << orig[0]->getName();

I get a bad memeory alloc error:

Unhandled exception at at 0x768F4B32 in file.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0033E598.
  • 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-14T15:30:11+00:00Added an answer on June 14, 2026 at 3:30 pm
    //delete orig array
    for(int n = 0; n < x; n++)
    {
        delete orig[n];
    }
    

    For this particular case, don’t do this. You are actually deleting the objects not the array. So all the objects in the temp array are pointing to invalid locations. Simply do delete [] orig to deallocate the original array.

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

Sidebar

Related Questions

I would like to extract unique values from my (dynamically allocated) array. I have
I have a dynamically allocated array : myRectangle lastRectanglesArray = new myRectangle[lastMaxLabel]; I would
I am working with a std::vector to hold some objects that have dynamically allocated
I'm building a simple list using a dynamically allocated array in C++. I have
I have a class that contains a dynamically allocated array, say class A {
I have a class called set that is a dynamically allocated array in the
I have a 2 dimensional array dynamically allocated in my C code, in my
Suppose we implement a stack using a dynamically allocated array. If the array fills,
I'm writing a class that wraps a dynamically allocated array and I'm trying to
Okay, I have a multi-dimensional array which is statically-allocated. I'd very much like to

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.