new for n-dimensional array.

A

Akyl Tulegenov

Dear All!

this is a construct I use to create the tensor of rank 2 in C.

double **dtensor2(long nl1,long nh1,long nl2,long nh2){
double **tensor=NULL;
long i1,i2;
tensor=(double **)calloc((nh1-nl1+2), sizeof (*tensor));
if(nl1!=0) tensor=tensor-nl1+1;
for(i1=nl1;i1<=nh1;i1++){
tensor[i1]=(double *)calloc((nh2-nl2+2), sizeof (*tensor[0]));
if(nl2!=0) tensor[i1]=tensor[i1]-nl2+1;
}
return tensor;
}

Can anybody show how it would look in C++ (when "new" is used)? Any
additional comments on whether it is feasible to use "new" instead of
calloc are extremely invited.

Thanks,
Akyl Tulegenov.




________________________________________________________________________
-= For every properly stated question there exists a definite answer. =-
L.Wittgenstein
 
M

Mike Wahler

Akyl Tulegenov said:
Dear All!
this is a construct I use to create the tensor of rank 2 in C.
double **dtensor2(long nl1,long nh1,long nl2,long nh2){
double **tensor=NULL;
long i1,i2;
tensor=(double **)calloc((nh1-nl1+2), sizeof (*tensor));
if(nl1!=0) tensor=tensor-nl1+1;
for(i1=nl1;i1<=nh1;i1++){
tensor[i1]=(double *)calloc((nh2-nl2+2), sizeof (*tensor[0]));
if(nl2!=0) tensor[i1]=tensor[i1]-nl2+1;
}
return tensor;
}
Can anybody show how it would look in C++ (when "new" is used)? Any
additional comments on whether it is feasible to use "new" instead of
calloc are extremely invited.

The 'size' arguments for 'malloc()'/'calloc()'/'realloc()' are
expressed as a number bytes.

The 'size' argument for allocating an array with 'operator new'
is expressed as a number of objects.

int *p = new int[42]; /* allocates 42 * sizeof(int) bytes */

Also 'new' has no counterparts to 'calloc()' or 'realloc()'.
IOW, after allocating with 'new', you'd need to initialize
the array manually, e.g. with a loop. This is the case with
the 'built-in' types such as 'int'. This 'manual' initialization
may or may not be necessary for UDT's depending upon whether
they provide default constructors for this purpose (most of the
standard library UDT's do).

For reallocation, you'd need to allocate the new desired size,
copy the old data there, then delete[] the old array.

Finally, the C++ standard library offers a (imo) superior solution
to such 'raw' arrays: containers (e.g. vectors), which handle
their own memory management, as well as automatically resize
themselves as needed.

std::vector<std::vector<int> >matrix(10, std::vector<int>(10));
/* simulates a 10x10 'matrix' of integers */

The constructor invoked by the above syntax will also
automatically default-initialize all the elements (to
a value of zero for the built-in types, or using
default ctor for UDTs)

The vector can be accessed using array syntax:

matrix[4][3] = 42;

It 'knows' its own size:

matrix.size();

and has many member functions for various operations
(e.g. insertions, deletions, etc.)

Many/most of the standard library algorithms
(e.g. std::sort, std::find, etc can be applied to
containers).

etc. etc.


When the vector is destroyed upon going out of scope it will
handle the release of any memory it has allocated.

Pretty cool, huh? :)

I've only glossed over the surface of these issues, you really
need a good C++ text to learn this correctly. See www.accu.org
for peer reviewed books.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top