1

Assume that we have following class:

class Test {
public:
   Test() {}
   std::vector<int>& getIntList() {
      return intList;
   }
private:
   std::vector<int> intList;
};

Also, we have following codes inside main function to declare class array:

int main(void) {
   Test* test[20];

   for (int i = 0; i < 20; ++i) {
      test[i] = new Test();
   }
}

In these cases, test objects are instantiated.

Now if I append multiple items in the vector inside each class randomly,

there might exist chances to collide memory address range of each vector inside each class while resizing their memory size.

In this case, is the entire 'test' object copied into other memory area and vector is resized? Or, is vector STL only copied into other memory area and resized while the class references the vector?

Entirely, is not a good idea to code like this?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
sungjun cho
  • 809
  • 7
  • 18
  • Entirely, is not a good idea to code like this? No it is not, forget about using `new` outside of constructing smartpointers until you really need it (probably never) – 463035818_is_not_an_ai May 29 '19 at 15:53
  • @formerlyknownas What i questioned was that is it good idea to manage vector(memory) like this – sungjun cho May 29 '19 at 15:54
  • 4
    Your question is quite unclear, why do you think that anything might "collide" here? – Jabberwocky May 29 '19 at 15:54
  • 2
    You could write a much less contrived version with two `std::vector` objects in local scope. – Useless May 29 '19 at 15:55
  • i dont really understand. You are not managing the vectors memory, the vector does that for you – 463035818_is_not_an_ai May 29 '19 at 15:55
  • 7
    Anyway, the answer is that the vector object doesn't grow when you add elements to it - the top-level object manages some dynamically-allocated storage _elsewhere_. Your false premise is that increasing `vector.size()` affects `sizeof(vector)`, and it doesn't. – Useless May 29 '19 at 15:56
  • vectors do not store their elements on the stack if thats your source of confusion – 463035818_is_not_an_ai May 29 '19 at 15:56
  • "there might exist chances to collide memory address range of each vector inside each class while resizing their memory size." This part is not at all clear – Aykhan Hagverdili May 29 '19 at 15:56
  • Classes have a fixed compile-time size in C++. Nothing grows or shrinks on the stack. How do you think `sizeof` works without evaluating its parameters? – Aykhan Hagverdili May 29 '19 at 15:58
  • This sounds like an [XY Problem](http://Xyproblem.info), what are you _actually_ trying to achieve? – Jabberwocky May 29 '19 at 15:58
  • @Jabberwocky since vector will be resized while pushing back, vectors inside each class can't resize its area consecutively, which means they have to be copied into other memory area and has to be resized in that area. – sungjun cho May 29 '19 at 15:58
  • Yes the vectors will be resized, so where is the problem? – Jabberwocky May 29 '19 at 15:59
  • *vectors inside each class can't resize its area consecutively,* -- What does one vector have to do with the other? They are separate objects -- one knows nothing or cares about what the other is doing. – PaulMcKenzie May 29 '19 at 15:59
  • 1
    Vector does not *"grow inside each class"* That is not a thing. Vector does not store its data on the stack – Aykhan Hagverdili May 29 '19 at 15:59
  • sry I think I have to edit my question. – sungjun cho May 29 '19 at 16:00
  • 2
    related/maybe dupe: https://stackoverflow.com/questions/55478523/how-does-stdvector-support-contiguous-memory-for-custom-objects-of-unknown-siz – NathanOliver May 29 '19 at 16:01
  • anyway now i understand something that i have missed – sungjun cho May 29 '19 at 16:02

1 Answers1

8

Consider this example:

struct foo {
    std::vector<int> x;
    std::vector<int> y;
};

Now sizeof(foo) is a compile time constant. It does not change when you add elements to the vectors. Also sizeof(std::vector<int>) is constant.

The size of a foo instance is not increasing when the size() of the vector grows. It is similar to having a dynamic array (only for the sake of the example):

struct bar {
   int* c_array;
};

Here, sizeof(bar) is likely to be just sizeof(int*), because it is just a pointer, even though it could point to the first element of a c-style array, or to a single int.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • This is what I want to know. Thank you. So does the vector act as dynamic array initialized with small size(by programmer)? – sungjun cho May 29 '19 at 16:04
  • @sungjuncho Yes, `std::vector` is a dynamic array that keeps its elements on the heap. `std::array` is a static array allocated at compile time, that cannot change its size. And don't bother with C-style arrays in new code. – Jesper Juhl May 29 '19 at 16:10
  • @sungjuncho hm yes, kind of. `std::vector` is much more than that. Just to name one example, it has an `operator==` that does what you want, something that is unnecessarily complicated with c-arrays – 463035818_is_not_an_ai May 29 '19 at 16:13