STL container and stack frame size

I

itt ium

Group,
This might be a very obvious question but a simple google search did
not reveal much. I am designing a software where different functions
make local copies of vectors, map and hash map containing big size
structures. I was concerned about following

if the vector is of big size, making a local variable will occupy lot
of stack space and may result in stack overflow.

With map, it seems problem may not be likely since map is a binary
search tree so only head pointer will be pushed to the stack.

I am not able to visualize the local hash map variable with respect to
stack frame.

Please let me know your thoughts on it.

thanks
ittium
 
A

Alf P. Steinbach

Group,
This might be a very obvious question but a simple google search did
not reveal much. I am designing a software where different functions
make local copies of vectors, map and hash map containing big size
structures. I was concerned about following

if the vector is of big size, making a local variable will occupy lot
of stack space and may result in stack overflow.

No.

But you are still into using lots of memory.

With map, it seems problem may not be likely since map is a binary
search tree so only head pointer will be pushed to the stack.

It's the same with vector etc.


I am not able to visualize the local hash map variable with respect to
stack frame.

Please let me know your thoughts on it.


Cheers & hth.,

- Alf
 
J

Juha Nieminen

itt ium said:
if the vector is of big size, making a local variable will occupy lot
of stack space and may result in stack overflow.

Nope. There's a very simple way of seeing how much stack space an
object takes, namely:

void foo()
{
std::vector<int> v(10000);
std::cout << "v is taking " << sizeof(v) << " bytes of stack space.\n";
}

Here it says "v is taking 12 bytes of stack space."

The rest is allocated on the heap.
 
I

ittium

Nope. There's a very simple way of seeing how much stack space an
object takes, namely:

void foo()
{
std::vector<int> v(10000);
std::cout<< "v is taking "<< sizeof(v)<< " bytes of stack space.\n";
}

Here it says "v is taking 12 bytes of stack space."

The rest is allocated on the heap.

This means, we really do not have any situation, where we should define
STL containers with pointers. Container with normal variables are
sufficient for most of the cases.

Ittium
 
I

Ian Collins

This means, we really do not have any situation, where we should define
STL containers with pointers. Container with normal variables are
sufficient for most of the cases.

Except where the object is "large", dynamically allocated, isn't
copyable or you don't want to copy it...
 
A

Asger-P

Hi ittium

This means, we really do not have any situation, where we should define
STL containers with pointers.

Ooh, there is plenty of situations, for instance if do a lot of:
sorting, inserting or deleting in the middle. And You still need
the by Index acces of the vector.

std::vector is quite slow though, for pinter only, having a pointer
only vector template works a lot faster.


Best regards
Asger-P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top