Define a 3D array which indexes start with a number other than zero.

Hi,
I want to change below code into a 3D array which the indexes are from -1 to 2.
I mean, I want change below code to create this:
A[-1][-1][-1] A[-1][-1][0] A[-1][-1][1] A[-1][-1][2]
A[-1][0][-1] A[-1][0][0] A[-1][0][1] A[-1][0][2]
A[-1][1][-1] A[-1][1][0] A[-1][1][1] A[-1][1][2]
A[-1][2][-1] A[-1][2][0] A[-1][2][1] A[-1][2][2]

A[0][-1][-1] A[0][-1][0] A[0][-1][1] A[0][-1][2]
A[0][0][-1] A[0][0][0] A[0][0][1] A[0][0][2]
A[0][1][-1] A[0][1][0] A[0][1][1] A[0][1][2]
A[0][2][-1] A[0][2][0] A[0][2][1] A[0][2][2]

A[1][-1][-1] A[1][-1][0] A[1][-1][1] A[1][-1][2]
A[1][0][-1] A[1][0][0] A[1][0][1] A[1][0][2]
A[1][1][-1] A[1][1][0] A[1][1][1] A[1][1][2]
A[1][2][-1] A[1][2][0] A[1][2][1] A[1][2][2]

would you please help me to change the below code and write the correct one?
-------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
//2D array
 
int **A;
A = new int*[4];
for (int i=0; i < 4; i++)
    A[i] = new int[4];

//  A is a 2D array which its indexs are:
//    A[0][0] A[1][0] A[2][0]  A[3][0]  
//    A[0][1] A[1][1] A[2][1]  A[3][1]  
//    A[0][2] A[1][2] A[2][2]  A[3][2]  
//    A[0][3] A[1][3] A[2][3]  A[3][3] 


Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

template < typename T, std::ptrdiff_t START_INDEX, std::size_t SZ > struct array
{
    T& operator[] ( std::ptrdiff_t pos ) { return arr[ pos - START_INDEX ] ; }
    const T& operator[] ( std::ptrdiff_t pos ) const { return arr[ pos - START_INDEX ] ; }
    T arr[SZ] ;
};

int main()
{
    using inner = array< int, -1, 4 > ; // indices are -1, 0, 1, 2
    using mid = array< inner, -1, 4 > ;
    using array3d = array< mid, -1, 4 > ;

    array3d A {} ;

    for( int i = -1 ; i < 3 ; ++i )
        for( int j = -1 ; j < 3 ; ++j )
            for( int k = -1 ; k < 3 ; ++k )
                A[i][j][k] = i*100 + j*10 + k ;

    for( int i = -1 ; i < 3 ; ++i )
        for( int j = -1 ; j < 3 ; ++j )
            for( int k = -1 ; k < 3 ; ++k )
                std::cout << std::showpos << "A[" << i << "][" << j << "][" << k << "] == " << A[i][j][k] << '\n' ;
}

http://coliru.stacked-crooked.com/a/947955c1c2522200
Thank you,
but as I described, for some reasons I have to use dynamic memory allocation (pointers and using "new")
(something like the code, which I have written for 2D)

int ***A;
Is it possible?

> for some reasons I have to use dynamic memory allocation (pointers and using "new")

What are those reasons? Even if the array sizes are to be determined at run-time,
why can't we do something sane like use std::vector<>?

Anyway, mechanism is similar - overload the subscript operator to cater to the start of the array having a logical non-zero index. Though the code does get ugly, brittle and error-prone when resource management is not encapsulated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

template < typename T > struct array_view
{
    array_view( std::ptrdiff_t start_index = 0, T* ptr = nullptr ) : ptr(ptr), start_index(start_index) {}

    T& operator[] ( std::ptrdiff_t pos ) { return ptr[ pos - start_index ] ; }
    const T& operator[] ( std::ptrdiff_t pos ) const { return ptr[ pos - start_index ] ; }

    T* ptr ;
    std::ptrdiff_t start_index ;
};

int main()
{
    array_view< array_view< array_view<int> > > A( -1, new array_view< array_view<int> >[4] );

    // allocate memory for inner arrays (note: not exception safe)
    for( int i = -1 ; i < 3 ; ++i )
    {
        A[i] = { -1, new array_view<int>[4] } ;
        for( int j = -1 ; j < 3 ; ++j ) A[i][j] = { -1, new int[4]{} } ;
    }

    // use the view
    for( int i = -1 ; i < 3 ; ++i )
        for( int j = -1 ; j < 3 ; ++j )
            for( int k = -1 ; k < 3 ; ++k )
                A[i][j][k] = i*100 + j*10 + k ;

    for( int i = -1 ; i < 3 ; ++i )
        for( int j = -1 ; j < 3 ; ++j )
            for( int k = -1 ; k < 3 ; ++k )
                std::cout << std::showpos << "A[" << i << "][" << j << "][" << k << "] == " << A[i][j][k] << '\n' ;

    // clean up (release memory)
    for( int i = -1 ; i < 3 ; ++i )
    {
        for( int j = -1 ; j < 3 ; ++j ) delete[] A[i][j].ptr ;
        delete[] A[i].ptr ;
    }
}

http://coliru.stacked-crooked.com/a/94db2d50220e8784
I need to transfer a code from 2D form into 3D in a simulator that is written in SystemC. (Basic code of systemC is C++.)
And the 3D array is a signal.
Originally 2D format is written by dynamic memory allocation,
I wanted to preserve the basic mechanism, as far as possible.

Maybe you're right, I'll check.
Topic archived. No new replies allowed.