Member Avatar for MonsieurPointer

Hello everyone,

I have a 2D array as follows:

double cornerPoints[4][3];

which is used to determine the four corners of a 2D bounding box of a curve.
However, since I do not know how many curves are to be processed, I would like to dynamically allocate memory, knowing that the number of corner points (4) will be in 3D coordinates (3). I therefore declared my variable as such:

double *pCornerPoints[4][3];

and tried to allocate it as follows:

pCornerPoints = new double[curves.size()];

to which Visual Studio's intellisense gave the message Expression must be a modifiable lvalue.

How would I go about allocating memory for it? Is it even possible? If so, what is the syntax (using the new and malloc operators)? I know how to allocate a complete dynamic 3D array, but I would like to avoid it if possible.

Thank you!

#include <iostream>
#include <memory>
#include <vector>

int main()
{
    typedef double corner_points_t[4][3] ;
    // using corner_points_t = double[4][3] ;

    std::size_t n = 15 ; // curves.size()

    // option one: low-level memory management - raw pointer sematics, messy
    {
        corner_points_t* corner_points = new corner_points_t[n] ; // { {0} } ;

        // use corner_points

        // once we are done
        delete[] corner_points ;
    }

    // option two: smart pointer - owned (or shared) pointer semantics
    {
        std::unique_ptr< corner_points_t[] > corner_points( new corner_points_t[n] ) ;
        // std::shared_ptr<corner_points_t> corner_points( new corner_points_t[n], 
                                                 // []( corner_points_t* p ) { delete[] p ; } ) ;

        // use corner_points
    }

    // option three: std::vector<> - simple value seantics
    {
        std::vector< std::vector< std::vector<double> > > corner_points( n,
                                   std::vector< std::vector<double> >( 4, std::vector<double>(3) ) ) ;

        // use corner_points
    }
}
Member Avatar for MonsieurPointer

Thank you for your reply, but that is not the answer I was looking for. What would be the call if I did not use a typedef (and using only the new operator)?

Thank you for your reply, but that is not the answer I was looking for.

I suspect the answer you're looking for is unreasonable. A better solution is to fix your design such that you don't need to wade into the shithole of pointers to arrays. For example, using a class for your bounding box and then having an array of objects of that class.

What would be the call if I did not use a typedef (and using only the new operator)?

Off the top of my head, I can't recall the correct syntax without using a typedef:

typedef double (*bb_t)[4][3];

double (**pCornerPoints)[4][3] = new bb_t[10]; // Or bb_t *pCornerPoints = new bb_t[10];

What would be the call if I did not use a typedef (and using only the new operator)?

int main()
{
    std::size_t n = 15 ; // curves.size()
    double (*corner_points)[4][3] = new double [n][4][3] ;

    // using corner_points is perfectly natural; use it as you
    // would use any array of three dimensions

    // for instance
    double d = 1.0 ;
    for( std::size_t i = 0 ; i < n ; ++i )
        for( auto& row : corner_points[i] )
             for( double& v : row )
                 v = d += 1.0 ;

    // this is the messy part. you must delete the array, delete it once
    // and only once, not use it after it is deleted etc.
    delete[] corner_points ;
}
Member Avatar for MonsieurPointer

vijayan,

Thanks a lot for your input. That is what I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.