473,786 Members | 2,462 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
15 4793
Thank you guys. Problem solved :-).

Robbie Hatley wrote:
"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 #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(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];
...
}
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.c om/c++-faq-lite/how-to-post.html#faq-5.4>


Brian
Jul 5 '06 #13
"Joe Van Dyk" <jo********@boe ing.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(d ouble trouble)
{
static BigStruct big_mess[80000];
...
}
and never get the array deallocated :(

Jul 7 '06 #15

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

int SmartFunction(d ouble 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(d ouble trouble)
{
static BigStruct big_mess[80000];
...
}

int main(void)
{
...
SmartFunction(3 7.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(d ouble trouble)
{
std::vector<Big Structbig_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
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
4495
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
3866
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
4815
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
9228
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
4218
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
2968
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
9647
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, 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...
0
9496
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,...
0
10164
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...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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
6745
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
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
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.