Solved

stack/heap question

Posted on 1998-06-25
10
289 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 10 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 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