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

  • Home
  • SEARCH
  • 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 8087355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:44:57+00:00 2026-06-05T18:44:57+00:00

I am trying to realize a Vector class that allocates a block of memory,

  • 0

I am trying to realize a Vector class that allocates a block of memory, and eventually re-allocate it if it needs to contain more items.
I am using std::allocator to do this:

#include <iostream>
#include <stdexcept>

using namespace std;

template <class T>
class Vector
{
private:
    T* data;
    allocator<T> data_all;
    int length;
    int _size;
    static const int block_size=10;
    void init()
    {
        length=0;
        _size=block_size;
        data=data_all.allocate(block_size,NULL);
    }
public:
    Vector()
    {
        init();
    }
    int size() const
    {
        return length;
    }
    void push_back(T item)
    {
        length++;
        if(length > _size)
        {
            _size+=block_size;
            data=data_all.allocate(_size,data);
        }
        data_all.construct(&data[length-1],item);
    }
    T& operator[] (int i)
    {
        if(i<0 || i>= length)
            throw out_of_range("The index is out of vector range");
        return data[i];
    }
};

int main(int argc, char** argv)
{
    Vector<int> v;
    for(int i=0; i<20; i++)
        v.push_back(i);
    for(int i=0; i<v.size(); i++)
        cout << v[i] << "\t";
    return 0;
}

The problem is that the item previously allocated are not preserved, it prints:

0   0   0   0   0   0   0   0   0   0   10  11  12  13  14  15  16  17  18  19  

Instead of:

0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  

Why this behaviour? Isn’t there a way in C++ to re-allocate contiguous items like in C with realloc?

  • 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-05T18:45:00+00:00Added an answer on June 5, 2026 at 6:45 pm

    The second argument to allocate is just a hint that the allocator could use to try and return new memory close to the old memory, but is ignored by std::allocator and fairly useless for vector-like containers because all the elements are close to each other anyway, as they’re in a contiguous block.

    You seem to be expecting it to copy the existing data. It won’t. You have to do that, by copying from the old memory block to the new one.

    You’re also leaking the old memory. You need to deallocate it.

    You want something like:

    void push_back(const T& item)
    {
        if (length == _size)
        {
            T* new_data = data_all.allocate(_size+block_size);
            // N.B. if any of the following copies throws new_data will be leaked
            std::uninitialized_copy(data, data+length, new_data);
            std::destroy(data, data+length);
            data_all.deallocate(data, _size);
            data = new_data;
            _size+=block_size;
        }
        data_all.construct(&data[length++],item);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to realize non-editable QTableView with widgets in cells that should contain clickable
I am trying to realize a use-case using git. Use Case : It should
I'm trying to realize portals using OpenGL via the stencil buffer. The technique seems
I'm trying to do something really simple, but starting to realize that dates in
During program optimization, trying to optimize a loop that iterates through a vector, I
I realize that what I am trying to do isn't safe. But I am
guys, I'm trying to realize viewing comments on internet portal and I'm using UITableView.
I'm trying to change some system entities and I realize that on some system
I am rather new to WPF. I'm trying to realize a chat app using
How could I realize a Histogram with corePlot. Actually I am trying that by

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.