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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:53:56+00:00 2026-05-30T22:53:56+00:00

First I want to say that, I have a vector which has thousand of

  • 0

First I want to say that, I have a vector which has thousand of vectors inside. Each of these inside vectors has thousand of numbers inside. I want to keep memory management safe and memory usage at minimum as much as possible.

I want to ask that if I have a code similiar to below

    int size = 10;
vector<vector<double>>* something = new vector<vector<double>>(size);

vector<double>* insideOfSomething;
for(int i = 0; i < size; i++){
    insideOfSomething = &(something->at(i));
    //...
    //do something with insideOfSomething 
    //...
}

I know that ‘something’ will be created in heap. What I don’t understand is where the vectors are placed, ‘insideOfSomething’ points? If they are created in stack, then this means that I have a vector pointer, which points a vector in heap, that has vectors inside which are created in stack? (I’m very confused right now.)

If I have a code similiar to the one below;

    vector<vector<double>*>* something = new vector<vector<double>*>(size);
vector<double>* insideOfSomething;
for(int i = 0; i < size; i++){
    something->at(i) = new vector<double>();
    insideOfSomething = something->at(i);
    //...
    //do something with inside insideOfSomething 
    //...
}

right know all of my vectors are stored in heap, right?

Which one is more usefull according to the memory management?

  • 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-05-30T22:53:57+00:00Added an answer on May 30, 2026 at 10:53 pm

    Let’s take a random, simplistic implementation of vector, as I think this will help you.

    template <class T, class Alloc>
    class vector
    {
    private:
        T* buffer;
        std::size_t vector_size;
        std::size_t vector_capacity
        Alloc alloc;
    
    public:
        ...
    };
    

    In this case, if we write:

    vector<int> v;
    v.push_back(123);
    

    … the pointer, buffer, the integrals: vector_size and vector_capacity, and the allocator object, alloc, will all be created on the stack (along with allocating any additional memory necessary for structure padding and alignment).

    However, vector itself will allocate memory on the heap to which this buffer pointer will store its base address. That will always be on the heap and will contain the actual contents of the vector as we think of them.

    This is still more efficient than this:

    vector<int>* v = new vector<int>;
    v->push_back(123);
    ...
    delete v;
    

    … as this would involve a heap allocation/deallocation for the vector itself (including its data members) in addition to the memory vector itself allocates for its internal contents (the buffer). It also introduces an additional level of indirection.

    Now if we have a vector of Somethings (vector of vector or anything else):

    vector<Something> v;
    

    Those Something instances are always going to be allocated within a contiguous heap buffer since they would reside in the dynamically allocated memory blocks that vector creates and destroys internally.

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

Sidebar

Related Questions

First of all i want to say that i have searched each and every
first i want to say that this site has been a really big help
First, I want to say that I have modified the method needsDividerBefore and now
First I want to say that I have looked at stackoverflow and highcharts forum,
First thing i want to say that it's not an easy question to explain,
First off I want to say that I am not using threads or multiple
First of all i want to say that I'm still a beginner in ASP.NET
First, I just want to say that the stackoverflow community is empowering to say
Let’s say that my string is: test! I want to get the first character
I have two vectors, x and y . x is a vector where each

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.