ma740988 wrote:
class X
{
std::string kdx;
public:
X() { }
};
int main()
{
X *ptr_x = new X();
std::size_t Jdx = sizeof (ptr_x);
std::cout << Jdx << std::endl;
std:vector<X*> my_vec;
my_vec.push_back(ptr_x);
std::cout << my_vec.size() << std::endl;
std::cout << my_vec.capacity() << std::endl;
delete ptr_x;
}
The output
4
1
1
on my platform. Now my_vec is a single byte.
How did you deduce that?
my_vec is certainly not a single byte.
That said, I envision
it would require for me then to store a 'slew' of pointers (beyond the
capacity of the vectory - whatever number that is) to my_vec for me to
run out of memory. Correct?
Right. But capacity has nothing to do with it. capacity is just the number
of array entries the vector has allocated right now. size() returns the
number of entries which are actually in use. If you continue to push_back
things to the vector, its size will grow until finally it reaches capacity()
(That is: all allocted entries have been used). When this happens, the vector
tries to increase capacity by allocating a larger junk of memory and continue.
It is clear that this cannot happen forever. Eventually the vector wil have
used up all available memory and ....
Look. Don't make things so complicated. A far amount of programming concepts
are taken directly from real live. Say you want to write some things down on paper.
What do you do? You take a notebook and start writing. Say you have written
(pushed back) 2 pages. So the capacity of the notebook is (lets say) 40 pages
(the total number of pages, some of them are unused) and the size of it (the
part that is used) is 2 pages. If you continue to write in your notebook, the size
increases to the capacity (the book gets full).
When this happens, what do you do? You increase the capacity by buying a notebook
with more pages. Since you don't want more then 1 notebook around, you first
copy the content from the old notebook to the new notebook. Then you continue
writing until the size has reached up to that capacity, etc., etc.
Eventually you will be in need for a notebook with so many pages, that you have
used up all the paper on the planet earth. When this happens, your notebook dealer
will tell you that you brought him into troubles getting such a large notebook, he
will throw an exception.
See the similarities?
--
Karl Heinz Buchegger
kb******@gascad.at