Boost logo

Ublas :

From: Hongyu Miao (jackymiao_at_[hidden])
Date: 2008-02-12 10:06:43


if UBLAS uses the new operator, then can the following code work?

try
{
        myvector.resize(blocksize);
}
catch(std::bad_alloc& e)
{
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
}

-------------------------------------------------------------
From:Gunter Winkler
Date:2008-02-11 19:02:26
To:ublas mailing list
CC:
Subject:Re: [ublas] ULBAS Memory Exception

Am Dienstag, 12. Februar 2008 00:42 schrieb Hongyu Miao:
> If the system does not have enough memory, an exception will be
> triggered by the last line of the codes. I tried the try/catch as
> follows

uBLAS uses the usual C++ method for memory allocation: operator new().
Thus you have to use the usual way to catch the thrown exceptions.

Regarding your example, if the new size does not match the requested
size, then you found a bug. If memory allocation fails, then the
program should abort or raise an exception. If you need a more flexible
memory management you can use a custom allocator, e.g.

vector< double, unbounded_array<double, my_allocator> >

then you can add any check you need in

my_allocator.allocate(size_t)

Regarding you question, throwing and catching exceptions is a quite
expensive operation since (usually) a snapshot of callstack has to be
taken. Thus it is better to handle known and expected problems
with 'ifs' and use exception only for 'exceptional' problems.

mfg
Gunter
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBHsOIMghsXb/J0PcERAr2JAKDJ3ezv5oAHrKKI/MgnSfOTfMiv7wCfYVpa
q26toKAhqBJh7eFYZKyiwgQ=
=Y1xp
-----END PGP SIGNATURE-----

_______________________________________________
ublas mailing list
ublas_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/ublas