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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:32:35+00:00 2026-06-15T04:32:35+00:00

Lets say i want to resize an array of int pointers, i have a

  • 0

Lets say i want to resize an array of int pointers, i have a function that looks like this

    template<typename T>
static void Resize(T* arr, UINT oldsize, UINT newsize) {
    T* ret = new T [newsize];
    memcpy(ret, arr, sizeof(arr[0]) * oldsize);
    delete[] arr;   
    arr = ret;
};

The problems begin when i try and resize an array of elements that were created with the “new” keyword (even though the data itself inside the class is POD) because the delete[] triggers their deconstructor which then leaves the new array with pointers to objects that dont exist anymore. So.. even though the objects were created with “new”, cant i just use the free command to get rid of the old array? or somehow delete the array without triggering the deconstructor of each member?

  • 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-15T04:32:36+00:00Added an answer on June 15, 2026 at 4:32 am

    Use a std::vector.


    EDIT: by popular demand, an explanation of why the OP’s code does not work.

    The code:

    template<typename T>
    static void Resize(T* arr, UINT oldsize, UINT newsize) {
        T* ret = new T [newsize];
        memcpy(ret, arr, sizeof(arr[0]) * oldsize);
        delete[] arr;   
        arr = ret;
    };
    

    Here arr is a pointer passed by value. Assigning to arr at the end only updates the local copy of the actual argument. So after this the actual argument, in the calling code, points to an array that has been deleted, whith pretty catastrophic result!

    It could be sort of rescued by passing that pointer by reference:

    template<typename T>
    static void Resize(T*& arr, UINT oldsize, UINT newsize) {
        T* ret = new T [newsize];
        memcpy(ret, arr, sizeof(arr[0]) * oldsize);
        delete[] arr;   
        arr = ret;
    };
    

    But this is still pretty fragile code.

    For example, the caller needs to keep track of the array size.

    With a std::vector called a, the resize call would instead look like

    a.resize( newSize )
    

    and in contrast to the DIY solution, when newSize is larger, those extra elements of the vector are nulled (which is a bit safer than leaving them as indeterminate values).

    A std::vector can be indexed just like a raw array. See your C++ textbook about more details of how to use it. If you don’t already have a C++ textbook, do get one: for most people it’s just an impractical proposition to learn C++ from articles and Q/A on the web.

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

Sidebar

Related Questions

Lets say I have a multi-threaded application that needs to resize some image files.
I would appreciate any help on this issue. Lets say I want to load
lets say i want an array with a tag name so when i want
lets say we want to do a simple app for android and for this
Lets say I want to include a DLL in a Visual Studio project that
I have a main container div that I want to resize dynamically. In a
Let's say i have following class: template <typename T> class CModule{ public: virtual void
Lets say I want to make a class myClass with two slots A and
lets say you want to get all the columns of a table, but exclude
Lets say I want to define 2 styles. .color_red { color: Red; } .banner

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.