Solved

stack/heap question

Posted on 1998-06-25
10
253 Views
Last Modified: 2010-04-10
Is there a limit to the amount of memory (besides the obvious physical one) on the stack or heap? I have a fairly large global data structure and I'd like to know if it would make a difference whether it is allocated on the stack or on the heap.
0
Comment
Question by:VEngineer
  • 4
  • 4
  • 2
10 Comments
 
LVL 2

Author Comment

by:VEngineer
Comment Utility
Edited text of question
0
 
LVL 8

Accepted Solution

by:
trestan earned 10 total points
Comment Utility
Automatic created variable will be stored at stack. The dynamicely created variable will be in heap. When a variable is created in heap, an additional space will also be occupied to store the related imformation.
0
 
LVL 8

Expert Comment

by:trestan
Comment Utility
If your data structure is global, it is better to be put in stack. You can try to detect the available space by using new.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> is there a limit to the amount of memory (besides the obvious physical one) on the stack or heap?
Yes.  But it depends on the implimentation you ae running on.  

For example,  In a DOS application you stack will be limited to 64K but will probably be much smaller.  Your heap could be larger than 64 K depending on you memory model.  
In a 32 bit windows program your stack and heap can both be larger (and likely will be) than 64 K.  Under windows the size is a lot less limited because virtual memory can be used to allow the stack and heap to expand (not indefinitly).

>> I  have a fairly large global data structure and I'd like to know if it would make a difference whether it is allocated on the stack or on the heap.

Probably better on the heap.  Heaps tend to be larger (considerably) and are expected to grow more.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> When a variable is created in heap, an additional space will also be occupied to store the related imformation.

But for a single large object this isn't significant.  The additional information is proably a pointer and a size, say 8 bytes.  If the object in question is 8K, we are talking about a 0.1% increase in memory consumption.  That is not a big deal.  Now if you are lots of small objects, then this exta information can get significant.

>> You can try to detect the available space by using new.
That's not a good idea these days (used to be fine).  On an implimentation that supports virtual memory, this can cause your heap to grow to huge sizes before new fails.  This can have a drastic impact on performance of your application and others.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:trestan
Comment Utility
Another consideration is whether you want to consume more time on compiling or on running. Using new will considerably increase the running time. It depends on the purpose of the software. If you develop the program and other people use it, it is prefered to spend more time on compiling and the client will not wait long time when it runs. You can try both. I do not see the much difference between the size of the stack and the size of the heap. Although some books say that it is better to create large object on heap, but my former programming, it seems that either the stack or the heap may use up the memory of the same limit.  
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> Using new will considerably increase the running time

Dynamic allocations do take a lot longer than allocating on the stack.  But on a modern computer, you can do thousands of dynamic allocations in a second.   So if we are talking about one or two objects, it isn't relevant.  
0
 
LVL 8

Expert Comment

by:trestan
Comment Utility
Just try to allocate a Array[10000000], you will see how long it will take. I think the memory management concept is different before and after window95 and whether it is a pentium machine.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Allocating a huge array doesn't take really longer than allocating a small array.  The time spent in allocating is the time required to search the heap for a free block (which is not done on the stack).  For a larger block you may have a longer search until you find a block big enough, but not much longer.   You can still search a typical heap thousands of times a second.  Thus if you are allocating one or two huge arrays, you will not see a performance difference between allocating them on the stack or allocating them on the heap.  The difference will be a matter of milliseconds.  If you were allocating thousands of these arrays, then you would notice a difference.
0
 
LVL 2

Author Comment

by:VEngineer
Comment Utility
This discussion helps a lot.  I'll think about my design more and keep these issues in consideration.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now