473,770 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4792
* 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<MyC lassmyArray( 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.c omwrote in message
news:11******** *************@j 8g2000cwa.googl egroups.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.c omwrote in message
news:11******** *************@j 8g2000cwa.googl egroups.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(ArrayEle ment)? 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(d ouble trouble)
{
BigStruct big_mess[80000]; // dump eight million bytes onto stack
...
}

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

int SmartFunction(d ouble 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(ArrayEle ment)? 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(d ouble trouble)
{
BigStruct big_mess[80000]; // dump eight million bytes onto stack
...
}

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

int SmartFunction(d ouble 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<int big_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.c omwrote:
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::vecto r".

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<MyC lassMyContainer (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<MyC lassMyContainer (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*)mallo c(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

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

Similar topics

0
2048
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 the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
4
4492
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 from source). I have few big tables with BLOBS and regular table 4.2 and 2.7 Gb respectively, plust several smaller tables. Every time I run query against this tables MySQL uses all available memory on server (I have 3Gb RAM on server) and it...
32
3864
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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
17
4814
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 easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to try to see where the leak is located. For all the leaky instances, I can see the following (I...
16
2392
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. Example: After about 5 hours with a concurrent user base of about 150 users the application raises the aspnet_wp memory usage by almost 500MB. If our server guys modify the web.config this data is released and the workerprocess goes back to a...
7
6936
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 the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem is on the ManagementObjectSearch, upon we Dispose the object it seems that is not releasing the...
9
9227
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 website. The problem is: it's eating more memory than a black hole. It eats the current limit of 256MB set in php.ini, in an application that would hardly consume 4MB if written in C. I don't care if this application takes much longer to run...
9
4217
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 starts off at a nice 4% of memory, then slowly grows up to 50% and beyond. This translates to around 2 gigs of physical memory, and that's really way more memory than this program should be taking up.
17
8488
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 file under linux python 2.4 (same problem with windows 2.5, tried with the first example) : #Python interpreter memory usage : 1.1 Mb private, 1.4 Mb shared #Using http://www.pixelbeat.org/scripts/ps_mem.py to get memory information
27
2967
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 not delete a. try { a = new int ;
0
10260
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, 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...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9910
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 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...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
3
2850
bsmnconsultancy
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...

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.