Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

stack/heap question

Posted on 1998-06-25
10
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 2

Author Comment

by:VEngineer
ID: 1166664
Edited text of question
0
 
LVL 8

Accepted Solution

by:
trestan earned 40 total points
ID: 1166665
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
ID: 1166666
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:nietod
ID: 1166667
>> 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
ID: 1166668
>> 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
 
LVL 8

Expert Comment

by:trestan
ID: 1166669
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
ID: 1166670
>> 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
ID: 1166671
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
ID: 1166672
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
ID: 1166673
This discussion helps a lot.  I'll think about my design more and keep these issues in consideration.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

722 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