Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL container and stack frame size

Reply
Thread Tools

STL container and stack frame size

 
 
itt ium
Guest
Posts: n/a
 
      08-04-2011
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-04-2011
On 04.08.2011 12:13, itt ium wrote:
> 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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-04-2011
itt ium <> wrote:
> 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.
 
Reply With Quote
 
ittium
Guest
Posts: n/a
 
      08-09-2011
On 04-08-2011 PM 07:51, Juha Nieminen wrote:
> itt ium<> wrote:
>> 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.


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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-09-2011
On 08/ 9/11 04:42 PM, ittium wrote:
> On 04-08-2011 PM 07:51, Juha Nieminen wrote:
>> itt ium<> wrote:
>>> 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.

>
> 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...

--
Ian Collins
 
Reply With Quote
 
Asger-P
Guest
Posts: n/a
 
      08-09-2011

Hi ittium

On the: 09. of august-2011 At: 06:42 ittium wrote:

>> 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.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57