473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is this a memory problem?

hi, folks,

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.

-Steve

Jul 4 '06 #1
15 4743
* 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.

--
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?
Jul 4 '06 #2

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? The local variable should be in
heap, right? 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? Also what I know to allocate array
dynamically is to use "new" and "delete". I wonder if you can recommend
any good reference (books or online resources) to me. Thanks.

Jul 4 '06 #3
* 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<MyClassmyArray( 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?
Jul 4 '06 #4

"syang8" <sy****@gmail.comwrote in message
news:11*********************@j8g2000cwa.googlegrou ps.com...
>
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? The local variable should be in
heap, right? 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? Also what I know to allocate array
dynamically is to use "new" and "delete". I wonder if you can recommend
any good reference (books or online resources) to me. Thanks.
Do you bust an int at 32767? bfx
Jul 4 '06 #5
Hi syang,
when u declare a variable as local, it never gets allocated in the
heap.
as someone has suggested, it's better to use new while allocating
memory for ur requiremnet.
namespace is something specific to c++;

hope this helps

BilfFord X wrote:
"syang8" <sy****@gmail.comwrote in message
news:11*********************@j8g2000cwa.googlegrou ps.com...

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? The local variable should be in
heap, right? 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? Also what I know to allocate array
dynamically is to use "new" and "delete". I wonder if you can recommend
any good reference (books or online resources) to me. Thanks.
Do you bust an int at 32767? bfx
Jul 5 '06 #6
"syang8" wrote:
I use Kdevelop to build some scientific simulation on Linux.
What's "Kdevelop", and what does it have to do with C++? If
this is some sort of software-writing software, be aware that
such programs are very mechanistic and may do stupid things
that a human programmer wouldn't do.
If I set the size of an array N = 8000, the program works fine.
If it has a large stack, yes. Arrays of what, by the way?
What is sizeof(ArrayElement)? If you have a 100-byte struct,
and you make a local array of 8000 of that, you're piling
800,000 bytes of data onto the stack.
However, if I set the array N some number greater than
10000 (actually, what I need is 80000)
EWWWWW. Don't do that.
the program has segmentation error.
Of course.
The intersting thing is that the positions reporting segmentation
error are different if I set N to be different values.
Of course.
What problem is this usually?
You're almost certainly overflowing your stack with a huge local
array, like so:

int DorkyFunction(double trouble)
{
BigStruct big_mess[80000]; // dump eight million bytes onto stack
...
}

Instead, declare your array "static" to get it off the stack:

int SmartFunction(double trouble)
{
static BigStruct big_mess[80000];
...
}

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
>

Jul 5 '06 #7
hi, Alf, sandy and Robbie,

The suggestions that you gave to me is really helpful. Now I have a
better understanding of local variables and stacks. However, I still
have a problem. I don't know exactly what the size of the array should
be. Therefore, "N" is designed as a parameter in the function call as

fuction(int N, ...)
{
int big_array[N];
}

I cannot change the array to be static. Maybe a good way is to
allocated the array dynamically, any other suggestions?

Robbie Hatley wrote:
"syang8" wrote:
I use Kdevelop to build some scientific simulation on Linux.

What's "Kdevelop", and what does it have to do with C++? If
this is some sort of software-writing software, be aware that
such programs are very mechanistic and may do stupid things
that a human programmer wouldn't do.
If I set the size of an array N = 8000, the program works fine.

If it has a large stack, yes. Arrays of what, by the way?
What is sizeof(ArrayElement)? If you have a 100-byte struct,
and you make a local array of 8000 of that, you're piling
800,000 bytes of data onto the stack.
However, if I set the array N some number greater than
10000 (actually, what I need is 80000)

EWWWWW. Don't do that.
the program has segmentation error.

Of course.
The intersting thing is that the positions reporting segmentation
error are different if I set N to be different values.

Of course.
What problem is this usually?

You're almost certainly overflowing your stack with a huge local
array, like so:

int DorkyFunction(double trouble)
{
BigStruct big_mess[80000]; // dump eight million bytes onto stack
...
}

Instead, declare your array "static" to get it off the stack:

int SmartFunction(double trouble)
{
static BigStruct big_mess[80000];
...
}

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 5 '06 #8
syang8 wrote:
hi, Alf, sandy and Robbie,

The suggestions that you gave to me is really helpful. Now I have a
better understanding of local variables and stacks. However, I still
have a problem. I don't know exactly what the size of the array should
be. Therefore, "N" is designed as a parameter in the function call as

fuction(int N, ...)
{
int big_array[N];
}

I cannot change the array to be static. Maybe a good way is to
allocated the array dynamically, any other suggestions?
#include <vector>
void function(int N, ...)
{
std::vector<intbig_vector(N);

//use big_vector like an array, e.g. big_vector[4] is the 5th element

//no need to deallocate it - std::vector's destructor deletes any
//"heap" memory it has allocated.
}

Tom
Jul 5 '06 #9

"syang8" <sy****@gmail.comwrote:
hi, Alf, sandy and Robbie,

The suggestions that you gave to me is really helpful. Now I have a
better understanding of local variables and stacks. However, I still
have a problem. I don't know exactly what the size of the array should
be. Therefore, "N" is designed as a parameter in the function call as

fuction(int N, ...)
{
int big_array[N];
}

I cannot change the array to be static. Maybe a good way is to
allocated the array dynamically, any other suggestions?


Yes. I have 3 suggestions for you, in descending order of
advisibility:

Suggestion #1 (the "C++" way):

Use "std::vector".

If you want the vector to still exist between calls to the function
in which it is declared, then you'll have to declare it "static":

#include <vector>
class MyClass {/* stuff */};
MyClass& MyFunction(int ContainerSize)
{
static std::vector<MyClassMyContainer (ContainerSize);
// ... a bunch of code ...
return MyContainer;
} // MyContainer still exists at this point.

But if MyContainer doesn't need to exist between calls to
your function, then you can leave off the "static":

#include <vector>
class MyClass {/* stuff */};
int MyFunction(int ContainerSize)
{
std::vector<MyClassMyContainer (ContainerSize);
// ... a bunch of code ...
return 0;
} // MyContainer dies here.

Suggestion #2 (a more-dangerous "C++" way):

Use "new[]" and "delete[]".

This is dangerous. Memory leaks can result if you "new[]" some
memory and forget to "delete[]" it, and program crashes can
result if you "delete[]" some memory which you didn't "new[]".

But on the other hand, it is a very flexible way of handling
memory.

// Make a dynamic array of MyClass objects:
MyClass* Ptr = new MyClass[ContainerSize];

// Assign stuff to the memory you now own:
Ptr[3756] = Whatever;

// Free-up the memory, later:
delete[] Ptr;

Suggestion #3 (the "C" way):

Dynamically allocate a block of raw memory using malloc().
When you're done with it, free it using free().

Warning: this is VERY dangerous. If you do it wrong, you'll
cause bugs, memory leaks, system crashes, etc.

But it's conceptually the simplest, lowest-level way of handling
memory, so it's alluring because of that:

// Get memory block to hold N copies of MyClass:
MyClass* Ptr = (MyClass*)malloc(ContainerSize * sizeof(MyClass));

// Assign stuff to the memory you now own:
Ptr[3756] = Whatever;

// Free-up the memory, later:
free(Ptr);

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 5 '06 #10
Thank you guys. Problem solved :-).

Robbie Hatley wrote:
"syang8" <sy****@gmail.comwrote:
hi, Alf, sandy and Robbie,

The suggestions that you gave to me is really helpful. Now I have a
better understanding of local variables and stacks. However, I still
have a problem. I don't know exactly what the size of the array should
be. Therefore, "N" is designed as a parameter in the function call as

fuction(int N, ...)
{
int big_array[N];
}

I cannot change the array to be static. Maybe a good way is to
allocated the array dynamically, any other suggestions?

Yes. I have 3 suggestions for you, in descending order of
advisibility:

Suggestion #1 (the "C++" way):

Use "std::vector".

If you want the vector to still exist between calls to the function
in which it is declared, then you'll have to declare it "static":

#include <vector>
class MyClass {/* stuff */};
MyClass& MyFunction(int ContainerSize)
{
static std::vector<MyClassMyContainer (ContainerSize);
// ... a bunch of code ...
return MyContainer;
} // MyContainer still exists at this point.

But if MyContainer doesn't need to exist between calls to
your function, then you can leave off the "static":

#include <vector>
class MyClass {/* stuff */};
int MyFunction(int ContainerSize)
{
std::vector<MyClassMyContainer (ContainerSize);
// ... a bunch of code ...
return 0;
} // MyContainer dies here.

Suggestion #2 (a more-dangerous "C++" way):

Use "new[]" and "delete[]".

This is dangerous. Memory leaks can result if you "new[]" some
memory and forget to "delete[]" it, and program crashes can
result if you "delete[]" some memory which you didn't "new[]".

But on the other hand, it is a very flexible way of handling
memory.

// Make a dynamic array of MyClass objects:
MyClass* Ptr = new MyClass[ContainerSize];

// Assign stuff to the memory you now own:
Ptr[3756] = Whatever;

// Free-up the memory, later:
delete[] Ptr;

Suggestion #3 (the "C" way):

Dynamically allocate a block of raw memory using malloc().
When you're done with it, free it using free().

Warning: this is VERY dangerous. If you do it wrong, you'll
cause bugs, memory leaks, system crashes, etc.

But it's conceptually the simplest, lowest-level way of handling
memory, so it's alluring because of that:

// Get memory block to hold N copies of MyClass:
MyClass* Ptr = (MyClass*)malloc(ContainerSize * sizeof(MyClass));

// Assign stuff to the memory you now own:
Ptr[3756] = Whatever;

// Free-up the memory, later:
free(Ptr);

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 5 '06 #11
Robbie Hatley wrote:
"syang8" wrote:

>>I use Kdevelop to build some scientific simulation on Linux.


What's "Kdevelop", and what does it have to do with C++? If
this is some sort of software-writing software, be aware that
such programs are very mechanistic and may do stupid things
that a human programmer wouldn't do.

>>If I set the size of an array N = 8000, the program works fine.


If it has a large stack, yes. Arrays of what, by the way?
What is sizeof(ArrayElement)? If you have a 100-byte struct,
and you make a local array of 8000 of that, you're piling
800,000 bytes of data onto the stack.

>>However, if I set the array N some number greater than
10000 (actually, what I need is 80000)


EWWWWW. Don't do that.

>>the program has segmentation error.


Of course.

>>The intersting thing is that the positions reporting segmentation
error are different if I set N to be different values.


Of course.

>>What problem is this usually?


You're almost certainly overflowing your stack with a huge local
array, like so:

int DorkyFunction(double trouble)
{
BigStruct big_mess[80000]; // dump eight million bytes onto stack
...
}

Instead, declare your array "static" to get it off the stack:

int SmartFunction(double trouble)
{
static BigStruct big_mess[80000];
...
}
What's too much data to put on the stack? Is there a rule of thumb for
when to use the stack and when not to?

Joe
Jul 5 '06 #12
syang8 wrote:
Thank you guys. Problem solved :-).


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the newsgroup FAQ:

<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>


Brian
Jul 5 '06 #13
"Joe Van Dyk" <jo********@boeing.comwrote:
What's too much data to put on the stack? Is there a rule of
thumb for when to use the stack and when not to?
When your program crashes, you put too much stuff on the stack.
:-) I know that sounds like a very crude answer, but it's the
best. Experiment and see what it takes to crash YOUR system.
This kind of thing is dependent on the CPU, the motherboard,
the amount of RAM, the OS, the compiler, etc.

If you had some way to determine that "programs compiled by
my compiler on my system can safely store exactly X bytes of
local data on the stack", that would give you a more exact
answer; but I don't know how to determine that.

The (ultra-rough) rule of thumb I use is, if I need to store
more than about 10KB of data, then I make sure it's not on
the stack. But this number may be way too small or way to
large for your system.

Here's an extreme example: My company sells circuit boards
with embedded controlllers using a Motorola CPU with 512
bytes of RAM. So, how much data can we safely store on
the stack? About 150 bytes. No, not 150MB. No, not 150KB.
150 bytes.

On the opposite extreme, utility programs I write at home
for the Windows 32 command prompt can probably put about
a half-billion bytes of stuff on the stack, because I've
got a gigabyte of RAM in my machine.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 6 '06 #14

Robbie Hatley wrote:
Instead, declare your array "static" to get it off the stack:

int SmartFunction(double trouble)
{
static BigStruct big_mess[80000];
...
}
and never get the array deallocated :(

Jul 7 '06 #15

"Diego Martins" <jo********@gmail.comwrote:
Robbie Hatley wrote:
Instead, declare your array "static" to get it off the stack:

int SmartFunction(double trouble)
{
static BigStruct big_mess[80000];
...
}

and never get the array deallocated :(
Why do you say that? The array you quote will by deallocated
at the point marked "HERE" in the program:

int SmartFunction(double trouble)
{
static BigStruct big_mess[80000];
...
}

int main(void)
{
...
SmartFunction(37.04);
...
return 0; // big_mess is deallocated HERE!!!
}

Exception: embedded systems. In that case, big_mess is deallocated
when you turn the power off or unplug the battery. :-)

Either way, it DOES get deallocated.

If you want it deallocated when SmartFunction exits,
make it a local std::vector:

int SmartFunction(double trouble)
{
std::vector<BigStructbig_mess (80000);
...
}

(But in that case, don't expect old contents of big_mess to still be
there on re-entry to SmartFunction.)
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/

Jul 7 '06 #16

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Amadeus | last post by:
Hello Everybody! I have a problem with MySQL servers running RedHat 9 (smp kernel 2.4.20) on Intel and MySQL server 4.0.14 (problem also appears on binary distr 4.0.15 and on 4.0.15 I bilt myself...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
16
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. ...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
9
by: Bruno Barberi Gnecco | last post by:
I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM XML), and I ended up using PHP to integrate with the rest of the code that already runs the...
9
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It...
17
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.