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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:32:36+00:00 2026-06-13T22:32:36+00:00

I have int * array=new int[2]; and I would like to free the memory

  • 0

I have

int * array=new int[2];

and I would like to free the memory of the last element, thus reducing the allocated memory to only 1 element. I tried to call

delete array+1;

but it gives error

*** glibc detected *** skuska:
free(): invalid pointer: 0x000000000065a020 *

Can this be done in C++03 without explicit reallocation?

Note: If I wanted to use a class instead a primitive datatype (like int), how can I free the memory so that the destructor of the class is called too?

Note2: I am trying to implement vector::pop_back

  • 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-13T22:32:37+00:00Added an answer on June 13, 2026 at 10:32 pm

    Don’t use new[] expression for this. That’s not how vector works. What you do is allocate a chunk of raw memory. You could use malloc for this, or you could use operator new, which is different from the new expression. This is essentially what the reserve() member function of std::vector does, assuming you’ve used the default allocator. It doesn’t create any actual objects the way the new[] expression does.

    When you want to construct an element, you use placement new, passing it a location somewhere in the raw memory you’ve allocated. When you want to destoy an element, you call its destructor directly. When you are done, instead of using the delete[] expression, you use operator delete if you used operator new, or you use free() if you used malloc.

    Here’s an example creating 10 objects, and destoying them in reverse order. I could destroy them in any order, but this is how you would do it in a vector implementation.

    int main()
    {
        void * storage = malloc(sizeof(MyClass) * 10);
    
        for (int i=0; i<10; ++i)
        {
            // this is placement new
           new ((MyClass*)storage + i) MyClass;
        }
    
        for (int i=9; i>=0; --i)
        {
            // calling the destructor directly
            ((MyClass*)storage + i)->~MyClass();
        }
    
        free(storage);
    }
    

    pop_back would be implemented by simply calling the destructor of the last element, and decrementing the size member variable by 1. It wouldn’t, shouldn’t (and couldn’t, without making a bunch of unnecessary copies) free any memory.

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

Sidebar

Related Questions

i have an array like below int[] array = new array[n];// n may be
I have a shared_array: boost::shared_array myarr(new char[m_length]); I would like to reallocate the array.
I have the following array ArrayList<double[]> db_results = new ArrayList<double[]>(); and I would like
I have an array: int[][] lawn = new int[980][1280]; wich stores the values of
I have a prototype: int[] medianFileter(int[] data); and an array int[] intVal = new
I have an array, for example (in Java) int[] a = new int[N]; I
I have two arrays that I create like this: public int GameBoard[][] = new
I have an int array for which i have allocated space for 100 elements.
I have the following two dimensional array: static int[,] arr = new int[5, 5]
I have a library which expects a array and fills it. I would like

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.