473,399 Members | 3,888 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,399 software developers and data experts.

How does STL report out of memory errors?

My program tries to call the vector::push_back(...) member function in a
loop couple dozen milion times, which is what I expect.

After about 20 millions successfull push_back() calls the program
abnormally aborts with a core dump.
Investigation under debugger showed that this happens because the
internal vector's pointer was beeing decreased from about 0x40000000 to
0x00000009 and next push_back() call caused the core dump.

How am I supposed to detect such a situation in my program?
Can I made STL to throw an out of memory exception maybe?

--
pit3k
Sep 21 '05 #1
6 6060
pit3k wrote:
How am I supposed to detect such a situation in my program?
Can I made STL to throw an out of memory exception maybe?


C++ reports failure to dynamically allocate by throwing a bad_alloc
exception. Of course dealing with that is rather involved.
Sep 21 '05 #2
pit3k wrote:
My program tries to call the vector::push_back(...) member function in a
loop couple dozen milion times, which is what I expect.

After about 20 millions successfull push_back() calls the program
abnormally aborts with a core dump.
Investigation under debugger showed that this happens because the
internal vector's pointer was beeing decreased from about 0x40000000 to
0x00000009 and next push_back() call caused the core dump.

How am I supposed to detect such a situation in my program?
Can I made STL to throw an out of memory exception maybe?


You don't have to make it. It should throw 'std::bad_alloc' if it cannot
allocate.

V
Sep 21 '05 #3
pit3k wrote:
My program tries to call the vector::push_back(...) member function in a
loop couple dozen milion times, which is what I expect.

After about 20 millions successfull push_back() calls the program
abnormally aborts with a core dump.
Investigation under debugger showed that this happens because the
internal vector's pointer was beeing decreased from about 0x40000000 to
0x00000009 and next push_back() call caused the core dump.
This was probably because it had to resize the vector to add the new
element, and since it couldn't just tack the space onto the end of your
existing vector because it ran out of room, it tried to relocate the
entire thing. But something went wrong. It should throw an exception
when such an error occurs.

1. What STL implementation are you using? Is it a checked
implementation in which you can enable debugging features?

2. Could you use vector::reserve to preallocate space and avoid
implicit growing?

3. Are you sure your iterators (or pointers) to the vector are not
being invalidated and then used?

4. Are you sure you're catching any exception that might be thrown?

5. An uncaught exception sometimes appears like this in Unix:
run_my_memory_hog Abort


Cheers! --M

Sep 21 '05 #4
mlimber napisal(a):
pit3k wrote:
My program tries to call the vector::push_back(...) member function in a
loop couple dozen milion times, which is what I expect.
After about 20 millions successfull push_back() calls the program
abnormally aborts with a core dump.

First, I meant std::list, not std::vector. It shouldn't change much
though.
1. What STL implementation are you using? Is it a checked
implementation in which you can enable debugging features?
I have no idea. How can I chek that?
2. Could you use vector::reserve to preallocate space and avoid
implicit growing?
I'm not using vector but list, sorry for mistake in OP.
3. Are you sure your iterators (or pointers) to the vector are not
being invalidated and then used?
I'm positive.
4. Are you sure you're catching any exception that might be thrown?
Yes.
5. An uncaught exception sometimes appears like this in Unix:


Not applies.

I've also noticed, that just before my program core dumps it's using
just about 700MB of memory, while the machine I'm running it on has 4GB
RAM.
//
// Below is what my code looks like:
//

struct CallDataRec
{
int xxx;
char yyy[10];
CallDataRec() { xxx = 0; yyy[0] = '\0'; }
};
void read_all()
{
std::list<CallDataRec> buffer;
CallDataRec cdr;

try
{
while (!stream.eof())
{
stream >> cdr;
buffer.push_back(cdr); // It core dump INSIDE the push_back()
// at about 20 millionth iteration
}
}
catch (...)
{
cerr << "Unknown exception" << endl;
}
}

--
pit3k

Sep 21 '05 #5
pit3k wrote:
mlimber napisal(a):
pit3k wrote:
My program tries to call the vector::push_back(...) member
function in a loop couple dozen milion times, which is what I
expect. After about 20 millions successfull push_back() calls
the program abnormally aborts with a core dump.

First, I meant std::list, not std::vector. It shouldn't change much
though.


vector stores all its members as a single contiguous array,
which gets reallocated when it fills up.
list does a new allocation for each new member.
This is a big difference.
struct CallDataRec
{
int xxx;
char yyy[10];
CallDataRec() { xxx = 0; yyy[0] = '\0'; }
};
void read_all()
{
std::list<CallDataRec> buffer;
CallDataRec cdr;

try
{
while (!stream.eof())
This is an error and could lead to a core dump. Please read the
FAQ on how to use eof() correctly.
{
stream >> cdr;
This fails to compile, as there is no operator>> defined for
writing to a CallDataRec.
buffer.push_back(cdr); // It core dump INSIDE the push_back()
// at about 20 millionth iteration
Probably there was heap corruption in the 'stream >> cdr' operation.
}
}
catch (...)
{
cerr << "Unknown exception" << endl;
}
Try catching a std::exception &, as some compilers (eg. Borland) have
been known to have problems with catch(...) .
}


Please post a COMPLETE program that compiles correctly, and
demonstrates the problem. Then, someone here will be able
to give you an exact answer as to what is going on.

Hint: the I/O loop should look like:

while (stream >> cdr)
buffer.push_back(cdr);

Sep 21 '05 #6
Old Wolf napisal(a):
pit3k wrote:
mlimber napisal(a):
pit3k wrote:
My program tries to call the vector::push_back(...) member
function in a loop couple dozen milion times, which is what I
expect. After about 20 millions successfull push_back() calls
the program abnormally aborts with a core dump.
First, I meant std::list, not std::vector. It shouldn't change much
though.


[SNIP]
Please post a COMPLETE program that compiles correctly, and
demonstrates the problem. Then, someone here will be able
to give you an exact answer as to what is going on.


OK, I've just used GNU g++ compiler instead of HP aCC.

The results:
g++ version throws std::bad_alloc exception, when it's memory usage (as
shown by top) reaches 953MB.
aCC version core dumps with "Bus error" when memory usage reaches
870MB.

Does that mean that HP compiler / STL implementation is broken or is
the core dump behaviour also valid?
Results were obtained using the following test program:

#include <list>
#include <iostream>

int main()
{
std::list<int> buffer;

try
{
for(;;)
buffer.push_back(0);
}
catch (std::bad_alloc &e)
{
std::cerr << "Memory allocation error" << std::endl;
}
catch (...)
{
std::cerr << "Unknown exception" << std::endl;
}

return 0;
}

Sep 22 '05 #7

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

Similar topics

31
by: lawrence | last post by:
I'm not sure how this is normally done, on a large site, perhaps one running Phorum. Occassionally a thread will have hundreds of entries, perhaps a meg or two worth of data. You won't necessarily...
2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
9
by: Frank Rizzo | last post by:
I understand the basic premise: when the object is out of scope or has been set to null (given that there are no funky finalizers), executing GC.Collect will clean up your resources. So I have...
3
by: Kevin | last post by:
Allen, I tried creating one of the report list boxes you have on your page (http://allenbrowne.com/ser-19.html). I created the chk box, cmd button, and module (saved as EnumReports). I...
2
by: Lars Eighner | last post by:
What does this me? /usr/ports/lang/php5-cli/work/php-5.0.5/main/streams/streams.c(1209) : Freeing 0x083AC564 (1 bytes), script=/home/webmaster/allphp/main.php...
2
by: TD | last post by:
I've read several posts here that say global variables are reset whenever an unhandled error occurs. I want to use a custom form property instead of a global variable to store a boolean value. My...
4
by: BrianDH | last post by:
Hi I am working on a C# Windows Application that builds an invoice (CR) converts to PDF and attach to email one at a time. This works untill my invoice count get over 40 to 50 invoices then I...
12
by: PLS | last post by:
I don't know where else to report bugs, so maybe someone from Microsoft is reading this group Application crashed on a library thread. The stack is below (most recent at top). I think is a...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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
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
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...
0
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
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...

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.