how to I recast pointer to n-dim array in C++?

H

Housing

Hi all,

I have a pointer which is double * ptr,

it actually points to a 3D array.

For the sake of programming ease, is there a way to recast the double * ptr
into a form that I can access as follows:

ptr[m][n][k]

or

ptr[m, m, k]

???

Thanks a lot! I am looking for a good simple recast that does not lead to
processing overhead, i.e. probably a class-level wrap is too much burden...
 
K

Kai-Uwe Bux

Housing said:
Hi all,

I have a pointer which is double * ptr,

it actually points to a 3D array.

For the sake of programming ease, is there a way to recast the double *
ptr into a form that I can access as follows:

ptr[m][n][k]

or

ptr[m, m, k]

???

Thanks a lot! I am looking for a good simple recast that does not lead to
processing overhead, i.e. probably a class-level wrap is too much
burden...

The following has undefined behavior, it may work on your platform.

#include <algorithm>
#include <iostream>
#include <iterator>

unsigned int const dim1 = 2;
unsigned int const dim2 = 3;
unsigned int const dim3 = 4;
unsigned int const size = dim1 * dim2 * dim3;
typedef float array1d [dim3];
typedef array1d array2d [dim2];
typedef array2d array3d [dim3];

typedef float * float_ptr;

int main ( void ) {
float_ptr fp = new float [ size ];
array3d & ar = reinterpret_cast< array3d& >( *fp );
for ( unsigned int i = 0; i < dim1; ++i ) {
for ( unsigned int j = 0; j < dim2; ++j ) {
for ( unsigned int k = 0; k < dim3; ++k ) {
ar[j][k] = i + j + k;
}
}
}
std::copy( fp, fp +size,
std::eek:stream_iterator< float >( std::cout, " " ) );
std::cout << '\n';
}

If you need something that avoids undefined behavior, a wrapper class would
probably be the way to go. I don't know of any casting magic that achieves
your objective without UB.


Best

Kai-Uwe Bux
 
Z

Zara

Hi all,

I have a pointer which is double * ptr,

it actually points to a 3D array.

For the sake of programming ease, is there a way to recast the double * ptr
into a form that I can access as follows:

ptr[m][n][k]

or

ptr[m, m, k]

???

Thanks a lot! I am looking for a good simple recast that does not lead to
processing overhead, i.e. probably a class-level wrap is too much burden...

It is really ugly:

It would be better if you used stabdard library containers...

double (&ref)[m][n][k]=reinterpret_cast<double (&)[m][n][k]>(*ptr);

Now you have a reference to the array.

Best regards,

Zara
 
B

BobR

Housing wrote in message...
Hi all,
I have a pointer which is double * ptr,
it actually points to a 3D array.

For the sake of programming ease, is there a way to recast the double * ptr
into a form that I can access as follows:

ptr[m][n][k]

or

ptr[m, m, k]

???
Thanks a lot! I am looking for a good simple recast that does not lead to
processing overhead, i.e. probably a class-level wrap is too much burden...

#include <vector>

std::vector<std::vector<std::vector<double> > > Array3D;

Or, you can init it like this:

std::vector<std::vector<std::vector<double> > > Array3D( 3,
std::vector<std::vector<double> >( 3,
std::vector<double>( 3, 3.1415 ) ) );

Now you have a 3x3x3 vector, all initialized to 3.1415.

Access last element:
std::cout<<Array3D.at(2).at(2).at(2); // 3.141500

or (if you are *positive* the index is valid):
std::cout<<Array3D[2][2][2]; // 3.141500
// like you wanted.

Add more:

Array3D.push_back( std::vector<std::vector<double> >( 3,
std::vector<double>( 3, 1.4731 ) ) );
// Now you have a 4x3x3 vector.

std::cout<<Array3D.at(3).at(2).at(2); // 1.473100

Setting it up is a little ugly, but look at the ease of use
(other than the 'push_back'. <G>), and not a 'new[]' in sight.
 
L

Logan Shaw

Housing said:
I have a pointer which is double * ptr,

it actually points to a 3D array.

For the sake of programming ease, is there a way to recast the double * ptr
into a form that I can access as follows:

ptr[m][n][k]

or

ptr[m, m, k]

???

Thanks a lot! I am looking for a good simple recast that does not lead to
processing overhead, i.e. probably a class-level wrap is too much burden...

Is it necessarily a lot of burden? If you write a class with non-virtual
methods that the compiler can inline (possibly with the help of an inline
directive), I'm not sure it would have to be any less efficient than a
regular array. You could even use templates so that the size of the array
is known at compile time; that could conceivably help if one of the
dimensions is a power of two or something.

- Logan
 
O

Old Wolf

I have a pointer which is double * ptr,
it actually points to a 3D array.

For the sake of programming ease, is there a way to recast the double * ptr
into a form that I can access as follows:

ptr[m][n][k]

Assuming it does actually point to a 3D array, and
not something else (such as an array of pointers),
this code shows what to do:

void foo(double *ptr)
{
double (*aptr)[2][3] = (double (*)[2][3])ptr;

aptr[0][1][2] = 42;
}

int main()
{
double arr[1][2][3] = { 0 };
double *ptr = &arr[0][0][0];

foo(ptr);
printf("%f\n", arr[0][1][2]);
}
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top