473,498 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

basic memory question

I am a stats man that uses C++ to analyze stock data, but I have run
into a problem regarding memory usage.

First, though, I need to give a little background info. I want to
write a program that will open up 132 files containing daily stock
market data. I estimate that each file contains around 91,000 bytes.
Multiplied by 132, this equals 12 million bytes. Given that I have
512 megs of memory, I thought that I would have no problem having all
this data in memory at the same time. But just to be sure before I
wrote all the code, I did a simple test of creating a very large array
to find out what size would cause the system to crash. The test
program's code is below:
int main()
{
float array[259100]={4};
array[258999]=2222;
cout << "\nARRAY 1 " << array[258999];

return 0;

} //end main()

This program crashes if the array size is increased beyond 259,000.
Why? The array only takes up 1.04 million bytes.

So, my question is: what do I need to do to have all that data in
memory at the same time?

Thanks
Jul 19 '05 #1
4 3815
"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
I am a stats man that uses C++ to analyze stock data, but I have run
into a problem regarding memory usage. .... But just to be sure before I
wrote all the code, I did a simple test of creating a very large array
to find out what size would cause the system to crash. The test
program's code is below: .... int main()
{
float array[259100]={4};
array[258999]=2222;
cout << "\nARRAY 1 " << array[258999];

return 0;

} //end main()
This function allocates the array as a local variable
of a fixed size (which typically means it is stored on
the stack if you know waht this is).
Local variables typically cannot be as large as
arrays allocated dynamically (on the heap) using
'new' or a standard container.
This program crashes if the array size is increased beyond 259,000.
Why? The array only takes up 1.04 million bytes.

So, my question is: what do I need to do to have all that data in
memory at the same time?


Use a container, such as std::vector:

#include <vector>
#include <iostream>

int main()
{
std::vector<float> array;
array.resize(1000000); // will just work
array[258999] = 2222;
std::cout << "\nARRAY 1 " << array[258999];
return 0;
}

If you find this code hard to understand,
I would recommend the book "Accelerated C++".
It should help for the type of thing you
are doing...
I hope this helps,
Ivan
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++
Jul 19 '05 #2
solartimba wrote:
I am a stats man that uses C++ to analyze stock data, but I have run
into a problem regarding memory usage.

First, though, I need to give a little background info. I want to
write a program that will open up 132 files containing daily stock
market data. I estimate that each file contains around 91,000 bytes.
Multiplied by 132, this equals 12 million bytes. Given that I have
512 megs of memory, I thought that I would have no problem having all
this data in memory at the same time. But just to be sure before I
wrote all the code, I did a simple test of creating a very large array
to find out what size would cause the system to crash. The test
program's code is below:
int main()
{
float array[259100]={4};
array[258999]=2222;


You are allocating arrays on the stack. Stack space for a program is
(usually) limited by the compiler. You must either increase the stack size
(look at your compiler's documentation for that) or allocate the array on
the heap. The best and safest way to do that is to use std::vector.

--
Unforgiven

A: Top Posting!
Q: What is the most annoying thing on Usenet?

Jul 19 '05 #3
solartimba wrote:
I am a stats man that uses C++ to analyze stock data, but I have run
into a problem regarding memory usage.

First, though, I need to give a little background info. I want to
write a program that will open up 132 files containing daily stock
market data. I estimate that each file contains around 91,000 bytes.
Multiplied by 132, this equals 12 million bytes. Given that I have
512 megs of memory, I thought that I would have no problem having all
this data in memory at the same time. But just to be sure before I
wrote all the code, I did a simple test of creating a very large array
to find out what size would cause the system to crash. The test
program's code is below:

well probably ypour floating point number does not fit in a byte right?
probaly it fits in a word of four bytes. If you then multiply by four
you'll get the right amount right?

Jul 19 '05 #4
In article <1a**************************@posting.google.com >,
ka*****@hotmail.com says...
I am a stats man that uses C++ to analyze stock data, but I have run
into a problem regarding memory usage.

First, though, I need to give a little background info. I want to
write a program that will open up 132 files containing daily stock
market data. I estimate that each file contains around 91,000 bytes.
Multiplied by 132, this equals 12 million bytes. Given that I have
512 megs of memory, I thought that I would have no problem having all
this data in memory at the same time. But just to be sure before I
wrote all the code, I did a simple test of creating a very large array
to find out what size would cause the system to crash.


First of all, you almost certainly want to use memory mapping to handle
this. It's theoretically not portable, but nearly every reasonably
modern OS supports it.

The problem you're running into is that many systems limit the stack
size to (considerably) less than all of memory. There are a couple of
ways of handling this: you can allocate the space statically or on the
heap, for a couple of the most obvious methods -- The easiest way is
probably to use std::vector instead of an array, in which case the space
will be allocated on the heap automatically.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4350
by: John | last post by:
I have a problem, it's not with any code I have because... there is no code. When I run a blank visual basic 6 form, it opens up just fine. When I add a text box, a caption, and a button... it...
6
4109
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
14
2482
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
3
5850
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I still think I don't quite grasp what these settings...
8
1444
by: kathy | last post by:
If I have 2D array like: int **p; p = new int*; for(int i=0;i<10;i++) { p = new int; }
21
1464
by: Roland | last post by:
The following issue is puzzling me: There are 2 ways of writing the code below: .... Dim fnt as Font = New Font(...) DrawString(myText, fnt,...) fnt.dispose(). or DrawString(myText, New...
12
4267
by: Avalon1178 | last post by:
Hi, I have an application that periodically uses a std::string variable which is assigned a VERY VERY large string (15000000+ bytes long). This application is essentially a daemon, and it polls...
16
1802
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all...
4
1215
by: Tony Johansson | last post by:
Hello! I know this might not be the appropriate forum for xml question. But xml is so important for .NET I hope my basic question can be answered in this forum. I just wonder does every xml...
0
7124
marktang
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,...
0
6998
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...
0
7163
Oralloy
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,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
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...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
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 ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.