Reputation: 4273
I have a class which contains a large vector
class myClass
{
public:
myClass(int size)
private:
vector<int> myVector;
}
myClass::myClass(int size)
{
myvector = vector<int>(size);
}
If I call myClass o(100000)
the object is created on the stack. However, what is exactly on the stack ? How much memory do I allocate from the stack ? The contents of the vector should be allocated on the heap, right ?
Can someone explain to me what exactly is on the stack and what is on the heap ?
Upvotes: 1
Views: 71
Reputation: 24249
Essentially you can generalize std::vector
as
template<typename T>
struct vector {
T* data;
size_t size;
size_t capacity;
};
Individual implementations may vary but they'll usually look something like the above.
So it's just this vector-container that is created on the stack, the array in which the data will be held is drawn from the heap.
--- Edit ---
For a given stack variable, you can tell how much stack space it requires with the sizeof
operator, e.g.
myClass o(100000);
std::cout << "o's size is " << sizeof(o) << "\n";
Upvotes: 6