Boost logo

Ublas :

From: Benny Buerger (bbuerger_at_[hidden])
Date: 2008-02-19 04:32:12


Now I got the point.
Thanks to Gunter and Sebastian

Am Montag, den 18.02.2008, 12:00 -0500 schrieb
ublas-request_at_[hidden]:
> Send ublas mailing list submissions to
> ublas_at_[hidden]
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> or, via email, send a message with subject or body 'help' to
> ublas-request_at_[hidden]
>
> You can reach the person managing the list at
> ublas-owner_at_[hidden]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of ublas digest..."
>
>
> Today's Topics:
>
> 1. Re: ULBAS Memory Exception (Mike Hayford)
> 2. memory allocation in local function (Benny Buerger)
> 3. Re: memory allocation in local function (Gunter Winkler)
> 4. Re: memory allocation in local function (Sebastian Gesemann)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 11 Feb 2008 15:54:27 -0800
> From: Mike Hayford <mike_at_[hidden]>
> Subject: Re: [ublas] ULBAS Memory Exception
> To: ublas mailing list <ublas_at_[hidden]>
> Message-ID: <47B0E033.4090108_at_[hidden]>
> Content-Type: text/plain; charset="gb2312"
>
> 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
> >
>
> -------------- next part --------------
> HTML attachment scrubbed and removed
>
> ------------------------------
>
> Message: 2
> Date: Tue, 12 Feb 2008 17:51:09 +0100
> From: Benny Buerger <bbuerger_at_[hidden]>
> Subject: [ublas] memory allocation in local function
> To: ublas_at_[hidden]
> Message-ID:
> <1202835069.13392.6.camel_at_[hidden]>
> Content-Type: text/plain; charset=UTF-8
>
> Hi,
>
> I'm a ublas newbie and have a basic question about memory allocation
> with ublas. Is it valid to assign a matrix in a local function and then
> use it in other functions, or do I have to use pointers?
>
> Here is a short example.
>
> class X
> {
> int sizex, sizey;
> void init();
> ublas::matrix<double> mat1;
> }
>
> X::X()
> {
> sizex=3;
> sizey=4;
> }
>
> void X::init()
> {
> mat1 = ublas::matrix<double>(sizex,sizey);
> }
>
> void X::useMatrix()
> {
> std::cout << mat1;
> }
>
> Is mat1 always defined after calling init() ?
>
>
> thx in advance
> Benny B?rger
>
>
>
> ------------------------------
>
> Message: 3
> Date: Sun, 17 Feb 2008 22:00:48 +0100
> From: Gunter Winkler <guwi17_at_[hidden]>
> Subject: Re: [ublas] memory allocation in local function
> To: ublas mailing list <ublas_at_[hidden]>
> Message-ID: <47B8A080.700_at_[hidden]>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> Benny Buerger schrieb:
> > Hi,
> >
> > I'm a ublas newbie and have a basic question about memory allocation
> > with ublas. Is it valid to assign a matrix in a local function and then
> > use it in other functions, or do I have to use pointers?
> >
> You can use an ublas matrix like any other type. By default the matrix
> handles all memory allocation by itself, so there is no need use pointers.
>
> mfg
> Gunter
>
>
>
> ------------------------------
>
> Message: 4
> Date: Mon, 18 Feb 2008 11:35:00 +0100
> From: "Sebastian Gesemann" <s.gesemann_at_[hidden]>
> Subject: Re: [ublas] memory allocation in local function
> To: "ublas mailing list" <ublas_at_[hidden]>
> Message-ID:
> <4103e0500802180235i1e298ea7j4ce64c3207b53f52_at_[hidden]>
> Content-Type: text/plain; charset=UTF-8
>
> On Feb 12, 2008 5:51 PM, Benny Buerger <bbuerger_at_[hidden]> wrote:
> > Here is a short example.
> >
> > class X
> > {
> > int sizex, sizey;
> > void init();
> > ublas::matrix<double> mat1;
> > }
> >
> > X::X()
> > {
> > sizex=3;
> > sizey=4;
> > }
> >
> > void X::init()
> > {
> > mat1 = ublas::matrix<double>(sizex,sizey);
> > }
> >
> > Is mat1 always defined after calling init() ?
>
> Since mat1 is a member of X it is always constructed when an object of
> class X is constructed. Either via an initializer list a la
>
> X::X() : sizex(3), sizey(4), mat1(sizex,sizey) {}
>
> or by calling the default c'tor implicitely which is what is happening
> in your case, actually. Before you assign 3 and 4 to sizex and sizey,
> the mat1 object is already constructed. in X::init() you could also do
> something like this:
>
> X::init() { mat1.resize(sizex,sizey,false); }
>
> This makes sense in case you want to delay the memory allocation for
> the matrix' elements as opposed to the initializer list alternative.
> I'd prefer this over your mat1=matrix<double>(sizex,sizey) because
> it's probably more efficient. If I had to guess what exactly happens
> with your version I'd say:
> - you're creating a temporary matrix object on the stack which
> allocates memory on the heap for the matrix' elements
> - the assignment operator lets the mat1 object also allocate 3x4
> elements of its own. The elements of the temporary matrix are then
> "copied into" your mat1 object. (*)
> - the temporary matrix is then destroyed.
>
> (* Even though the matrix (or its embedded container) object usually
> stores a pointer to an element array it has *value semantics*)
>
>
> Gr??e,
> Sebastian
>
> ------------------------------
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
>
>
> End of ublas Digest, Vol 39, Issue 11
> *************************************