Boost logo

Ublas :

From: Mike Hayford (mike_at_[hidden])
Date: 2008-02-11 18:54:27


How about:

          try
          {
              myvector.resize(blocksize);
          }
          catch(std::bad_alloc& e)
          {
               std::cout << "Memory allocation failure!"
 << std::endl;
          }

Hongyu Miao wrote:
> Hi, All
> I'm wondering how to handle the memory allocation exception. For example,
>
> int blocksize = 1000000;
> ublas::vector<double> myvector(0);
> myvector.resize(blocksize);
>
> 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
>
> try
> {
> myvector.resize(blocksize);
> if (blocksize != myvector.size())
> throw "Memory allocation failure!";
> }
> catch(char* str)
> {
> std::cout << str << std::endl;
> }
>
> However, I can do this in a much simpler way like:
>
> myvector.resize(blocksize);
> if (blocksize != myvector.size())
> std::cout << "Memory allocation failure!" << std::endl;
>
> What's the difference between these two methods? And does UBLAS have a better
> built-in solution for this?
>
> Thanks very much,
> H.M.
>
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
>