Blitz logo

Blitz Support :

From: carlos.rega_at_[hidden]
Date: 2005-02-15 06:33:59


What you want to do is relatively straightforward, you need to use the
functions resize, or resizeAndPreserve to change the size of your arrays
on the fly, and blitz will take care of allocation.

Regarding the problem with a1, you can not initialise a data member in the
class declaration. You need a constructor to do that, or a static
initialisation. What I would do would be something like this:

using namespace blitz;

class Fred {
private:
  Array<float,3> a1;
  Array<float,3> a2;

public:
  Fred(void):a1(2,4,5),a2(0,0,0){;} //initialise a1 to the correct size,
and a2 is empty
  void allocate(int x,int y, int z);
};

void Fred::allocate(int x, int y, int z) {
  a2.resize(x,y,z); // now a2 is grown to the desired size, use
resizeAndPreserve if there is data there you want to keep
}

Note that here a1 can also be resized if you want. This compiles and works
for me when I don't know the sizes of the arrays at compile time.

HTH

Carlos A. Rega, PhD
Development Scientist
Malvern Instruments Ltd

John Bray <home_at_[hidden]>
Sent by: blitz-support-bounces_at_[hidden]
15/02/2005 10:38
Please respond to Support list for Blitz++

 
        To: blitz-support_at_[hidden]
        cc:
        Subject: [Blitz-support] Putting Array inside my own class to dynamically allocate
it

As someone with a long background in Fortan 90 struggling to see how the
same features might be implemented, Blitz looks very useful, but I'm
confused how I could introduce Arrays into my own classes for dynamic
allocation.

In Fortran I'd have

MODULE fred
  REAL :: a1(2,4,5)
  REAL, POINTER :: a2(:,:,:)
CONTAINS
SUBROUTINE Allocate(x,y,z)
INTEGER,INTENT(IN) :: x,y,z
ALLOCATE (a2(x,y,z))
END SUBROUTINE Allocate
END MODULE fred

With Blitz in C++ I'd assume something like

#include <blitz/array.h>
using namespace blitz;

class Fred {
  Array<float,3> a1(2,4,5);
// Not sure how to define a2 here
public:
  void allocate(int x,int y, int z);
};

void Fred::allocate(int x, int y, int z) {
  Array<float,3> a2(x,y,z);
  return;
}

Now this is wrong because g++ objects to the definition of a1, and I
wanted a2 to be a class level variable, not local to allocate. The
dynamic array a2 is of course the more useful one to me.

I suspect this is due to my ignorance of C++, but none of the Blitz
examples try to do this kind of thing.

John

_______________________________________________
Blitz-support mailing list
Blitz-support_at_[hidden]
http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support

______________________________________________________________________
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom
they are addressed.

If you have received this email in error please notify the
originator of the message.

Any views expressed in this message are those of the individual
sender, except where the sender specifies and with authority,
states them to be the views of Malvern Instruments.
______________________________________________________________________