checking stl vector successful memory allocation

K

kwijibo28

Hi all,

I've got a simple question regarding stl containers. Consider this
code:

std::vector<float> foo;
foo.resize(100);

How do I know if memory allocation was successful? The resize method
returns nothing. I understand that resize does not always allocate
memory. But even if I use the reserve method it doesn't return boolean
or something to validate.

Should I do something like this:

if(foo.capacity()<foo.size())
cout << "Memory allocation fail!" << endl;

Is there any standard way of doing this.

Thanks all
Kwijibo
 
V

Victor Bazarov

kwijibo28 said:
I've got a simple question regarding stl containers. Consider this
code:

std::vector<float> foo;
foo.resize(100);

How do I know if memory allocation was successful? The resize method
returns nothing. I understand that resize does not always allocate
memory.

If it does, and there isn't enough of it, std::bad_alloc is thrown.
But even if I use the reserve method it doesn't return boolean
or something to validate.

Should I do something like this:

if(foo.capacity()<foo.size())
cout << "Memory allocation fail!" << endl;

Is there any standard way of doing this.

Yes,

try {
foo.resize(100);
}
catch (std::bad_alloc const&) {
cout << "Memory allocation fail!" << endl;
}

Victor
 
P

Patrick Frankenberger

kwijibo28 said:
Hi all,

I've got a simple question regarding stl containers. Consider this
code:

std::vector<float> foo;
foo.resize(100);

How do I know if memory allocation was successful? The resize method
returns nothing. I understand that resize does not always allocate
memory. But even if I use the reserve method it doesn't return boolean
or something to validate.

Memory allocation requests don't fail. They succeed or throw an exception
(std::bad_alloc).

HTH,
Patrick
 
J

Jim Fischer

kwijibo28 said:
Hi all,

I've got a simple question regarding stl containers. Consider this
code:

std::vector<float> foo;
foo.resize(100);

How do I know if memory allocation was successful? The resize method
returns nothing. I understand that resize does not always allocate
memory. But even if I use the reserve method it doesn't return boolean
or something to validate.

Should I do something like this:

if(foo.capacity()<foo.size())
cout << "Memory allocation fail!" << endl;

Is there any standard way of doing this.

Research the 'std::bad_alloc' exception class. The implementation (i.e.,
the standard C++ library) throws a std::bad_alloc object to report
storage allocation failures.

<example>
try {
std::vector<float> foo;
...
foo.resize(100);
...
}
catch ( const std::bad_alloc & error) {
// whoops... if control reaches here, a memory allocation
// failure occurred somewhere within the 'try{}' block
}
</example>
 
K

Karl Heinz Buchegger

kwijibo28 said:
Thanks for the reply guys.
I've tried all this but it still doesn't work. So I'll be a little
more specific about what I am trying do. I'm writing an MFC aplication
using VC++ 6.0.

When I write something like:

try {
foo.resize(power(2,32)-1);
}
catch (std::bad_alloc const&) {
::MessageBox(NULL,"Memory allocation fail!","Error",MB_OK);
}

I've got a message box that pop up with a "Out of memory" message when
foo.resize is called. I don't know where this message box come from.

Deep from the internals of your runtime system.
MS has yet to learn that emitting such messages is just a way to
support lazy programmers. The number of programmers trying to get
rid of the floppy-disk-failed message in DOS is legion. MS has not
learned from that.
And even worst, my message box with "Memory allocation fail!" don't
appear.

VC++ 6.0 does not throw an exception when a memory allocation failes.
Instead NULL is returned from new. Don't know what resize does with
that information.

But honestly: 2 to the power of 32 is a lot of memory. If we assume
that foo holds unsigned characters, that would be 4 GB. Tracing your
post back, I see that foo holds floats, so that would sum up to
16 GB in VC++. That's a lot. Are you sure you need that much memory?
 
K

kwijibo28

Karl Heinz Buchegger said:
But honestly: 2 to the power of 32 is a lot of memory. If we assume
that foo holds unsigned characters, that would be 4 GB. Tracing your
post back, I see that foo holds floats, so that would sum up to
16 GB in VC++. That's a lot. Are you sure you need that much memory?

I usually don't need that much memory but that was just a test to be
sure that memory allocation would fail, because I was trying to catch
the exception. I don't think it's good practice not to check for
memory allocation problem just because it's a small array.

kwijibo28
 
T

tom_usenet

I usually don't need that much memory but that was just a test to be
sure that memory allocation would fail, because I was trying to catch
the exception. I don't think it's good practice not to check for
memory allocation problem just because it's a small array.

In your case you've passed such a huge number that it will throw a
std::length_error, since the memory allocator doesn't support
allocating that much memory even if your computer has it physically.

A better test would be:

try
{
while(1)
{
int* i = new int[10000000];
//leak!
}
}
catch(std::bad_alloc const&)
{
}

but VC6 won't throw here anyway since it doesn't follow the standard
in this respect, but VC7.1 (and maybe 7.0) catches the exception.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top