Re: vector/memory
From: Arnaud Debaene (adebaene_at_club-internet.fr)
Date: 01/05/05
- Next message: Stephen Howe: "Re: ifstream and empty strings"
- Previous message: Duane Hebert: "Re: ifstream and empty strings"
- In reply to: Kyle: "vector/memory"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Stephen Howe: "Re: ifstream and empty strings"
- Previous message: Duane Hebert: "Re: ifstream and empty strings"
- In reply to: Kyle: "vector/memory"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|