Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stack vs. heap. a loaded question.

Reply
Thread Tools

stack vs. heap. a loaded question.

 
 
Daniel L Elliott
Guest
Posts: n/a
 
      11-19-2004
Thank you for the education everyone! These excellent posts will help me
create the best-possible design.

- dan elliott

On Wed, 17 Nov 2004 19:57:17 -0800, E. Robert Tisdale wrote:

> Prash wrote:
>
>> Stack is there for automatic variables and its size is limited.
>> So, I suggest that you use heap for your data storage.

>
> Actually, automatic storage [the program stack]
> and free storage [the heap] share the same virtual memory.
> The typical program stack grows up from the bottom of virtual memory
> and the heap grows downward to the top of the stack.
>
> The stack size may be limited but can be easily increased
> using shell commands or compiler options.
> For example, on a Linux workstation:
>
> > limit stacksize

> stacksize 10240 kbytes
> > limit stacksize unlimited
> > limit stacksize

> stacksize unlimited
>
>> If [you] have performace to consider, then see to it that some time
>> object creation and deletion might take and you should minimize on that;
>> like, instead of allocation 10 small memory chunks,
>> you can create a larger chunk of memory and then divide it by yourself.
>>
>> Also, I'd suggest you not to use variable sized arrays
>> as they are implemented in libraries using lists or something
>> and access to them is definetly slower
>> than the fixed sized conventional arrays.

>
> No!
>
> C99 style variable size arrays
>
> http://gcc.gnu.org/ml/gcc/2004-05/msg00746.html
>
> are allocated from free storage [the stack]
> and have performance characteristics similar to "conventional arrays".
>
>> Also, maybe you can have a look at the GNU Scientific library
>> as an option for components. www.gnu.org/software/gsl/

>
> You may as well check out
> the Vector, Signal and Image Processing Library
>
> http://www.vsipl.org/
>
> or, better yet,
> the High Performance Embedded Computing Software Initiative
>
> http://www.hpec-si.org/
>
> which actually proposes a C++ language binding.


 
Reply With Quote
 
 
 
 
Daniel L Elliott
Guest
Posts: n/a
 
      11-19-2004
Ah yes! Very helpful. Thank you.

- dan elliott

On Fri, 19 Nov 2004 16:23:33 +0100, Jacek Dziedzic wrote:

> Daniel L Elliott wrote:
>
>> In what way is the "stack" limited in size
>> that the "heap" is not? I sorry if this is something I should have
>> learned as an undergrad.

>
> As far as I understand it, upon the start of your program a fixed
> amount of memory is denoted as 'stack' and the rest of the memory
> is denoted as heap. In that way, stack is usually limited to a
> certain amount -- often only 1MB in order not to waste too much
> memory that could be used as heap (because you can't shrink the
> stack if it is not used).
>
> That way you get a limited amount of stack storage. This can
> bite you in a cruel manner if you don't remember about it.
> Under Linux, when the stack is overfilled you usually only
> get "Aborted" and your program dies instantly.
>
> The size of the stack is usually controlled via a compiler
> switch.
>
> You might try to run these two programs:
>
> // program 1
> // This tries to have a 50MB structure on stack
> // Will most likely crash
> void foo() {
> char bigarray[50000000];
> bigarray[0]=0; // use bigarray to make sure the
> // compiler does not optimize it away
> }
>
> int main() {
> foo();
> }
>
> // program 2
> // This tries to allocate a 50MB structure on the heap
> // Will most likely succeed, if you have 50MB spare memory
> #include<iostream>
> void foo() {
> try{
> char* bigarray = new char[50000000];
> }
> catch(std::bad_alloc) {
> std::cerr << "****, not enough memory" << std::endl;
> }
> // use bigarray here...
> }
>
> int main() {
> foo();
> }
>
> HTH,
> - J.


 
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
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
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
java -verbose doesn't show "loaded from" for classes loaded from custom jars in the classpath Udo Corban Java 0 01-23-2004 09:32 AM
Re: how to programatically give assembly loaded from network the same trust as those loaded from local host? Marcelo Birnbach [MS] ASP .Net 0 06-27-2003 11:51 PM



Advertisments