Re: vector/memory

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Arnaud Debaene (adebaene_at_club-internet.fr)
Date: 01/05/05


Date: Wed, 5 Jan 2005 23:13:34 +0100

Kyle wrote:
> Whats the way to detect allocation failure in vector ?
> If the only answer is "using exceptin handling" i would apriciate some
> insight on it

Whenever you do an operation that could do a memory allocation (eg, an
insert / push_back / reserve) in the vector, put the operation in a try
block and catch std::bad_alloc exception :
try
{
   myVector.push_back(new_value);
}
catch (std::bad_alloc&)
{
   //allocation failure, do whatever you want to recover...
}

If you want to optimize performances, try to use vector::reserve whenever
you can.

Arnaud
MVP - VC



Relevant Pages

  • Re: xmalloc string functions
    ... applications doing complex tasks for which memory allocation failure is only ... applications which do try to handle malloc failure of course). ...
    (comp.lang.c)
  • Re: Why leave the error handling to the caller?
    ... what was and wasn't appropriate behavior on memory allocation failure ... handling) than OpenSSH. ... I presume the difference is that Laurier's off-campus environment includes the ...
    (comp.lang.c)
  • vector/memory
    ... Whats the way to detect allocation failure in vector? ... If the only answer is "using exceptin handling" i would apriciate some ... insight on it ...
    (microsoft.public.vc.stl)