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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:51:02+00:00 2026-05-11T09:51:02+00:00

Below, I’m not declaring my_ints as a pointer. I don’t know where the memory

  • 0

Below, I’m not declaring my_ints as a pointer. I don’t know where the memory will be allocated. Please educate me here!

#include <iostream> #include <vector>  class FieldStorage { private:     std::vector<int> my_ints;  public:     FieldStorage()     {         my_ints.push_back(1);         my_ints.push_back(2);     }      void displayAll()     {         for (int i = 0; i < my_ints.size(); i++)         {             std::cout << my_ints[i] << std::endl;         }     } }; 

And in here, I’m declaring the field my_ints as a pointer:

#include <iostream> #include <vector>  class FieldStorage { private:     std::vector<int> *my_ints;  public:     FieldStorage()     {         my_ints = new std::vector<int>();         my_ints->push_back(1);         my_ints->push_back(2);     }      void displayAll()     {         for (int i = 0; i < my_ints->size(); i++)         {             std::cout << (*my_ints)[i] << std::endl;         }     }      ~FieldStorage()     {         delete my_ints;     } }; 

main() function to test:

int main() {     FieldStorage obj;     obj.displayAll();     return 0; } 

Both of them produces the same result. What’s the difference?

  • 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. 2026-05-11T09:51:03+00:00Added an answer on May 11, 2026 at 9:51 am

    In terms of memory management, these two classes are virtually identical. Several other responders have suggested that there is a difference between the two in that one is allocating storage on the stack and other on the heap, but that’s not necessarily true, and even in the cases where it is true, it’s terribly misleading. In reality, all that’s different is where the metadata for the vector is allocated; the actual underlying storage in the vector is allocated from the heap regardless.

    It’s a little bit tricky to see this because you’re using std::vector, so the specific implementation details are hidden. But basically, std::vector is implemented like this:

    template <class T> class vector { public:     vector() : mCapacity(0), mSize(0), mData(0) { }     ~vector() { if (mData) delete[] mData; }     ... protected:     int mCapacity;     int mSize;     T *mData; }; 

    As you can see, the vector class itself only has a few members — capacity, size and a pointer to a dynamically allocated block of memory that will store the actual contents of the vector.

    In your example, the only difference is where the storage for those few fields comes from. In the first example, the storage is allocated from whatever storage you use for your containing class — if it is heap allocated, so too will be those few bits of the vector. If your container is stack allocated, so too will be those few bits of the vector.

    In the second example, those bits of the vector are always heap allocated.

    In both examples, the actual meat of the vector — the contents of it — are allocated from the heap, and you cannot change that.

    Everybody else has pointed out already that you have a memory leak in your second example, and that is also true. Make sure to delete the vector in the destructor of your container class.

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

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think this should do it: urlLinks = $(this).attr("href").replace(/(\/(news|blog)\/)/, "$1article/");… May 11, 2026 at 10:48 pm
  • Editorial Team
    Editorial Team added an answer I use toLocaleString() on my site, and I've never had… May 11, 2026 at 10:48 pm
  • Editorial Team
    Editorial Team added an answer You could use OCMock, but you'd have to modify the… May 11, 2026 at 10:48 pm

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
Below is the command I tried executing, without success: exec('ln -s ' . PLUGIN_DIR
Below, I'm not declaring my_ints as a pointer. I don't know where the memory
Below is a Class I created to track the current Person in my glorified

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.