Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

push_back causes crash at the limits of memory

345 views
Skip to first unread message

kevin

unread,
Apr 2, 2008, 12:09:01 AM4/2/08
to

My program crashes on executing a push_back, triggering the message:

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

I am running the program on a 32bit Windows XP Pro machine. At the time I
start my loop with the push_back, the program has already used up 1.5GB of
memory. Somewhere in the middle of my loop, the push_back crashes the
program with the above message. In this case, the user has data which
exceeds the addressable memory, but the push_back does not die gracefully.
Is there some way to stop it from crashing when it can not allocate memory on
a push_back?

For example, this program dies in a similar manner:

#include<iostream>
#include<vector>

using namespace std;

int main()
{
vector<long long> thevector;
longlong theint;

for(theint=0; theint < 20000000000; theint++)
{
thevector.push_back(theint);
}

return 0;
}

Doug Harrison [MVP]

unread,
Apr 2, 2008, 12:32:52 AM4/2/08
to
On Tue, 1 Apr 2008 21:09:01 -0700, kevin <ke...@discussions.microsoft.com>
wrote:

Try catching the exception that push_back throws.

--
Doug Harrison
Visual C++ MVP

Igor Tandetnik

unread,
Apr 2, 2008, 12:33:57 AM4/2/08
to
"kevin" <ke...@discussions.microsoft.com> wrote in message
news:7B005564-D7DC-495B...@microsoft.com

> My program crashes on executing a push_back, triggering the message:
>
> This application has requested the Runtime to terminate it in an
> unusual way. Please contact the application's support team for more
> information.

It doesn't crash. push_back throws a bad_alloc exception, which your
program doesn't catch. An uncaught exception causes the runtime to
terminate the program.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Arnie

unread,
Apr 2, 2008, 9:04:14 AM4/2/08
to
> It doesn't crash. push_back throws a bad_alloc exception, which your
> program doesn't catch. An uncaught exception causes the runtime to
> terminate the program.
Exactly. Use something like:
try
{
// your code
}
catch ( const std:bad_alloc & e )
{
// Handle out of memory
}
catch ( const std:exception & e )
{
// Something else 'bad' happened
}

- Arnie


Green

unread,
Apr 7, 2008, 12:16:59 PM4/7/08
to
Hi,
vector only support 67108864 elements,
over the number will crash ,and push_back not throw exceptions.

yours,
Tylor


"kevin" <ke...@discussions.microsoft.com> 写入消息新闻:7B005564-D7DC-495B...@microsoft.com...

Green

unread,
Apr 7, 2008, 12:27:03 PM4/7/08
to
Hi,

?
vector::max_size ?

yours,
Tylor

"kevin" <ke...@discussions.microsoft.com> 写入消息新闻:7B005564-D7DC-495B...@microsoft.com...
>

Doug Harrison [MVP]

unread,
Apr 7, 2008, 12:30:09 PM4/7/08
to
On Tue, 8 Apr 2008 00:16:59 +0800, "Green" <green...@hotmail.com> wrote:

>Hi,
>vector only support 67108864 elements,
>over the number will crash ,and push_back not throw exceptions.
>
>yours,
>Tylor

If true, I suspect it depends heavily on virtual memory settings, and in
any case, it would be a bug.

Alex Blekhman

unread,
Apr 7, 2008, 1:06:08 PM4/7/08
to
"Doug Harrison [MVP]" wrote:
>>vector only support 67108864 elements,
>>over the number will crash ,and push_back not throw exceptions.
>
> If true, I suspect it depends heavily on virtual memory
> settings, and in
> any case, it would be a bug.

I think that OP refers to `vector::max_size()' method. The return
value of the `vector::max_size()' method depends on the size of
the type that was used to instatiate its allocator. MS's
implementation returns "(size_t)(-1) / sizeof (_Ty)" value, where
_Ty is the actual type of a vector (and its allocator).

The single restriction that the standard imposes on this value is
that it is "the largest value that can meaningfully be passed to
X::allocate().". Of course, it's nowhere near the true allocation
limit for the vector.

Alex


Doug Harrison [MVP]

unread,
Apr 7, 2008, 3:32:53 PM4/7/08
to
On Mon, 7 Apr 2008 20:06:08 +0300, "Alex Blekhman" <tkfx....@yahoo.com>
wrote:

>I think that OP refers to `vector::max_size()' method. The return
>value of the `vector::max_size()' method depends on the size of
>the type that was used to instatiate its allocator. MS's
>implementation returns "(size_t)(-1) / sizeof (_Ty)" value, where
>_Ty is the actual type of a vector (and its allocator).

If that's what he was thinking, I don't know how he came up with 67,108,864
elements for a vector of 8-byte integers (long long).

>The single restriction that the standard imposes on this value is
>that it is "the largest value that can meaningfully be passed to
>X::allocate().". Of course, it's nowhere near the true allocation
>limit for the vector.

Right, the actual limit is usually a lot less.

Alex Blekhman

unread,
Apr 8, 2008, 11:28:20 AM4/8/08
to
"Doug Harrison [MVP]" wrote:
> If that's what he was thinking, I don't know how he came up with
> 67,108,864
> elements for a vector of 8-byte integers (long long).

It seems that OP measured it once for some type with sizeof = 16
and then remembered it.

Alex


genehowell

unread,
Dec 31, 2009, 11:24:27 AM12/31/09
to

I tried your suggestion with the simple program above and it did not work. That is, I had:

for(theint=0; theint < 20000000000; theint++)
{
try
{
thevector.push_back(theint);


}
catch ( const std::bad_alloc & e )
{

return 0;


}
catch ( const std::exception & e )
{

return 0;
}
}

The program crashed (threw an exception) on the push_back(*) call. Am I missing something simple? Any idea why the exception would not be handled? BTW, the value of theint when it crashed was 67108864. Thanks.

Gene


Arnie wrote:

Exactly.
02-Apr-08

Exactly. Use something like:
try
{
// your code
}
catch ( const std:bad_alloc & e )
{
// Handle out of memory
}
catch ( const std:exception & e )
{
// Something else 'bad' happened
}

- Arnie

Previous Posts In This Thread:

On Wednesday, April 02, 2008 12:09 AM
kevi wrote:

push_back causes crash at the limits of memory


My program crashes on executing a push_back, triggering the message:

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

I am running the program on a 32bit Windows XP Pro machine. At the time I
start my loop with the push_back, the program has already used up 1.5GB of
memory. Somewhere in the middle of my loop, the push_back crashes the
program with the above message. In this case, the user has data which
exceeds the addressable memory, but the push_back does not die gracefully.
Is there some way to stop it from crashing when it can not allocate memory on
a push_back?

For example, this program dies in a similar manner:


using namespace std;

int main()
{
vector<long long> thevector;
longlong theint;

for(theint=0; theint < 20000000000; theint++)
{
thevector.push_back(theint);
}

return 0;
}

On Wednesday, April 02, 2008 12:32 AM
Doug Harrison [MVP] wrote:

Re: push_back causes crash at the limits of memory
wrote:


Try catching the exception that push_back throws.

--


Doug Harrison
Visual C++ MVP

On Wednesday, April 02, 2008 12:33 AM
Igor Tandetnik wrote:

Re: push_back causes crash at the limits of memory

It doesn't crash. push_back throws a bad_alloc exception, which your

program doesn't catch. An uncaught exception causes the runtime to
terminate the program.

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925

On Wednesday, April 02, 2008 9:04 AM
Arnie wrote:

Exactly.


Exactly. Use something like:
try
{
// your code
}
catch ( const std:bad_alloc & e )
{
// Handle out of memory
}
catch ( const std:exception & e )
{
// Something else 'bad' happened
}

- Arnie

On Monday, April 07, 2008 12:16 PM
Green wrote:

Hi,vector only support 67108864 elements,over the number will crash ,and
Hi,


vector only support 67108864 elements,
over the number will crash ,and push_back not throw exceptions.

yours,
Tylor

On Monday, April 07, 2008 12:27 PM
Green wrote:

Re: push_back causes crash at the limits of memory
Hi,

?
vector::max_size ?

yours,
Tylor

On Monday, April 07, 2008 12:30 PM
Doug Harrison [MVP] wrote:

Re: push_back causes crash at the limits of memory


If true, I suspect it depends heavily on virtual memory settings, and in
any case, it would be a bug.

--


Doug Harrison
Visual C++ MVP

On Monday, April 07, 2008 1:06 PM
Alex Blekhman wrote:

Re: push_back causes crash at the limits of memory
"Doug Harrison [MVP]" wrote:

I think that OP refers to `vector::max_size()' method. The return
value of the `vector::max_size()' method depends on the size of
the type that was used to instatiate its allocator. MS's
implementation returns "(size_t)(-1) / sizeof (_Ty)" value, where
_Ty is the actual type of a vector (and its allocator).

The single restriction that the standard imposes on this value is

that it is "the largest value that can meaningfully be passed to
X::allocate().". Of course, it's nowhere near the true allocation
limit for the vector.

Alex

On Monday, April 07, 2008 3:32 PM
Doug Harrison [MVP] wrote:

Re: push_back causes crash at the limits of memory


On Mon, 7 Apr 2008 20:06:08 +0300, "Alex Blekhman" <tkfx....@yahoo.com>

wrote:


If that's what he was thinking, I don't know how he came up with 67,108,864
elements for a vector of 8-byte integers (long long).

Right, the actual limit is usually a lot less.

--
Doug Harrison
Visual C++ MVP

On Tuesday, April 08, 2008 11:28 AM
Alex Blekhman wrote:

Re: push_back causes crash at the limits of memory
"Doug Harrison [MVP]" wrote:

It seems that OP measured it once for some type with sizeof = 16
and then remembered it.

Alex


Submitted via EggHeadCafe - Software Developer Portal of Choice
Use the Google Reader API to purge old feeds
http://www.eggheadcafe.com/tutorials/aspnet/02188aa0-d50b-42fe-9bdb-30a321dd7671/use-the-google-reader-api.aspx

Igor Tandetnik

unread,
Dec 31, 2009, 1:47:46 PM12/31/09
to
gene howell wrote:
> I tried your suggestion with the simple program above and it did not work. That is, I had:
>
> for(theint=0; theint < 20000000000; theint++)
> {
> try
> {
> thevector.push_back(theint);
> }
> catch ( const std::bad_alloc & e )
> {
> return 0;
> }
> catch ( const std::exception & e )
> {
> return 0;
> }
> }
>
> The program crashed (threw an exception) on the push_back(*) call.

For me, your code eventually throws a bad_alloc exception which is successfully caught by the appropriate catch clause. How does your experience differ?

Do you have exception handling turned off, by any chance? Check Project | Properties | C/C++ | Code Generation | Enable C++ Exceptions.

0 new messages