By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

checking stl vector successful memory allocation

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"kwijibo28" <kw*******@hotmail.com> wrote...
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
Jul 19 '05 #2

P: n/a
kwijibo28 wrote:
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>

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #3

P: n/a

"kwijibo28" wrote:
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
Jul 19 '05 #4

P: n/a


kwijibo28 wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5

P: n/a
Karl Heinz Buchegger <kb******@gascad.at> wrote in message

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);
}

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
Jul 19 '05 #6

P: n/a
On 11 Aug 2003 04:14:09 -0700, kw*******@hotmail.com (kwijibo28)
wrote:
Karl Heinz Buchegger <kb******@gascad.at> wrote in message
>
> 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);
> }
>

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.


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
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.