Hello all,
I am writing a program which needs to run as quickly as possible, but holds
a lot of data in memory (around 1GB for a usual run). Is this too much
memory to even consider putting most/all of it on the stack? Does it
matter?
Any considerations I might take into account when deciding how to allocate
the many data structures? By the way, the system will consist of a handful
of very large data structures (primarily matrices and vectors).
Historically, I have always used the heap. This was more a result of my C
background than anything else. However, I am now writing a system which
will use only C++ and OO practices. Therefore, I feel I have a new choice
to make. Amy assistance to make this design as efficient as possible would
be greatly appreciated.
Thank you,
dan elliott
Jul 22 '05
11 2317
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 "convention al 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.
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<iostre am> void foo() { try{ char* bigarray = new char[50000000]; } catch(std::bad_ alloc) { std::cerr << "Shit, not enough memory" << std::endl; } // use bigarray here... }
int main() { foo(); }
HTH, - J. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kevin Grigorenko |
last post by:
Hello,
I couldn't find an obvious answer to this in the FAQ. My basic question,
is: Is there any difference in allocating on the heap versus the stack? If
heap or stack implementation is not part of the standard, then just
disregard this question. Here's some questions I'm confused about, and if
you can add anything else, please do so!
Is the stack limited for each program?
|
by: Jonas Rundberg |
last post by:
Hi
I just started with c++ and I'm a little bit confused where stuff
go...
Assume we have a class:
class test {
private:
int arr;
};
|
by: balu |
last post by:
Hi Gurus
I need a help. Im developing a C++ libaray. I don't
whether user creates object in stack/heap. Is there a
way to find object location?
Balu.N
|
by: Nick McCamy |
last post by:
I have a question related to allocating on the stack.
In this program below, are my following assumptions true?
- variable a is allocated on the heap since it's static
- variable b is allocated on the stack since it's a value type variable
- variable d is allocated on the stack since it's a value type variable
Where does variable "c" get allocated? It's a value type, but not foundwithin a method.
|
by: gmccallum |
last post by:
General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.
Question:
What if a struct contains a string property(variable). The string would
be a reference type being contained in a value type. Would this filter
up and cause the stack to now be a reference type placed on the heap
or would it still be on the stack with a pointer used internally to point
to a...
| |
by: arcticool |
last post by:
I had an interview today and I got destroyed :(
The question was why have a stack and a heap?
I could answer all the practical stuff like value types live on the
stack, enums are on the stack, as are structs, where classes are on
the heap... when value types go out of scope the memory is re-
allocated, object remain in memory waiting to be cleaned up by the
garbage collector, etc, but he responded 'so why not just put say a
class on the...
|
by: sarathy |
last post by:
Hi all,
I need a few clarifications regarding memory allocaion in C++.
I apologize for the lengthy explanation.
1. In C++, Objects are allocated in heap.
What does heap refer to? Is it an area in RAM/Memory or does it refer
to a data structure being used for storing objects.
2. In C++, functions and its local variables go in stack.
If local variables that are primitives go in stack, it is OK. But what
|
by: coder_lol |
last post by:
Thanks everybody for helping me with the Syntax confusion! The
implicit conversion stuff really got me :) I have one more
question...
Array<int32ia;
Does the above use the default constructor and get me an Array<int32>
with a size 0? The memory used is the stack, right?
ia = Array<int32>(10);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |