Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > checking stl vector successful memory allocation

Reply
Thread Tools

checking stl vector successful memory allocation

 
 
kwijibo28
Guest
Posts: n/a
 
      08-07-2003
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-07-2003
"kwijibo28" <> 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


 
Reply With Quote
 
 
 
 
Patrick Frankenberger
Guest
Posts: n/a
 
      08-07-2003

"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


 
Reply With Quote
 
Jim Fischer
Guest
Posts: n/a
 
      08-07-2003
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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-08-2003


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

 
Reply With Quote
 
kwijibo28
Guest
Posts: n/a
 
      08-11-2003
Karl Heinz Buchegger <> 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
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      08-11-2003
On 11 Aug 2003 04:14:09 -0700, (kwijibo2
wrote:

>Karl Heinz Buchegger <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does STL vector guarantee contagious memory Allocation Pallav singh C++ 1 03-27-2008 01:03 PM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
what are the possible reasons that successful pre-synthesis simulation + successful synthesis = failed post-synthes walala VHDL 4 09-08-2003 01:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57