Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ vector of pointer to object - max_size()

I have a class apples

class apples
{
  private:
      double x;
      double y;
      double z;
  public:
      //some methods
};

I want to store the pointer to apples objects in a vector. I am doing this so that i create any object in any file & use any object in any file. I used the following code to determine the max no of pointers i can store in that vector

int _tmain(int argc, _TCHAR* argv[])
{
vector<apples *> myvector;
cout<<"max :"<<myvector.max_size();
return 0;
}

it gave me:

1073741823

now, my question is that can i really store 1073741823 no of pointers in that vector or is this the memory limitation (i.e. 1073741823 bytes) of the vector?

so if there are 2 vectors

vector<int> A
& 
vector<double> B

can A have 1073741823 elements & B also have 1073741823 elements? i am asking this to clarify that, the max no of elements that a vector can store does not depend on the the type of entity (int or double) being stored? (this has nothing to do with the current capacity of the vector!) Also, what would be the size of a pointer to apples object (not asking the size of apples object!)? Thank You.

like image 533
Cool_Coder Avatar asked Dec 05 '22 19:12

Cool_Coder


1 Answers

This is library limitation for storing elements in a vector.

vector::max_size() :

Returns the maximum number of elements that the vector container can hold.

This is not the amount of storage space currently allocated to the vector (this can be obtained with member vector::capacity), but the maximum potential size the vector could reach due to system or library implementation limitations.

So, you can not store more than it (and in practice maybe less than it due to system limitations)

In other words even if you have best resource (memory, CPU, ...) abilities and your elements have minimum size, you can not store more than max_size()

And according to the comment on max_size() in the header file: Returns the size of the largest possible %vector.

like image 144
masoud Avatar answered Dec 31 '22 07:12

masoud