0

I have a Nx3 array which I need to fill as a function (so vector isn't an option). I already know how big N as as I feed it into the function as a parameter. I still get this stupid error of "must have a constant value", my code is:

double bspline_plot(double CP[], double Knot[], const int N, int d, int ncontrol, double *A){
    // CP are the control points
    //Knot is the knot vector
    //N is the number of internal point you want in each segment
    //d is the degree of the polynomials
    double min_x, max_x, dx;
    double *x_1;
    x_1 = new double[N];
    double A[N][2];
    int i, j, M, L;
    min_x = min(Knot);
    max_x = max(Knot);
    dx = (max_x - min_x) / N;
    for (i = 0; i <= N; i = i + 1)
    {
        x_1[i] = min_x + dx*i;
    }

    M = ncontrol;
    L = (sizeof(Knot) / sizeof(*Knot)); 
    if (L < d + M + 1) // This checks if the number of control points are positive
    {
        printf("Incorrectly defined knot vector\n");
        return;
    }
    else //This is the Cox - deBoor algorithm
    {
        for (i = 0; i <= N; i = i + 1)
        {
            for (j = 0; j <= L - 1; j = j + 1)
            {
                A[i][1] = A[i][1] + CP[j, 1] * CdB(j, d, x_1[i], Knot);
                A[i][2] = A[i][2] + CP[j, 2] * CdB(j, d, x_1[i], Knot);
                A[i][3] = A[i][3] + CP[j, 3] * CdB(j, d, x_1[i], Knot);
            }
            A[N][1] = CP[L, 2];
            A[N][2] = CP[L, 2];
            A[N][3] = CP[L, 1];
        }
    }
    return A;
}

My other option is to feed in an array and then find it's values in the function but that seems a bit silly.

halex
  • 16,253
  • 5
  • 58
  • 67
Matthew Hunt
  • 101
  • 2
  • 5
    @ForceBru although `malloc` does have valid uses in C++, it should not be the "go to" solution, and `new` should be used instead (or better yet, `std::array` or `std::vector`). Matthew, I'm not clear on *why* you can't use `std::vector` ("fill as a function" does not mean anything to me). – crashmstr Feb 17 '15 at 14:10
  • You can pass a pointer to the vector's data to use it as a C-style array parameter by calling `&myvec[0]` or `myvec.data()` in C++11. Naturally it's up to you to ensure you don't go out of bounds within the function. – Neil Kirk Feb 17 '15 at 14:18
  • @crashmstr, yep, you're right, I work more with C and know some C++, that's why I'm using lots of C stuff in C++. – ForceBru Feb 17 '15 at 14:25
  • 1
    `vector isn't an option` ... `vector` is always an option in C++. Most of the times the right one. – bolov Feb 17 '15 at 14:46
  • I would rather avoid vector. – Matthew Hunt Feb 17 '15 at 15:40

3 Answers3

3

try to use std::vector in following way:

std::vector<std::vector<double>> A( N );
for( auto& row : A )
  row.resize( M );

or

std::vector<std::vector<double>> A( N, std::vector<double>( M ));
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • I don't think vector is the correct way to go about these things and I would avoid resize like the plague. – Matthew Hunt Feb 17 '15 at 15:18
  • @MatthewHunt: `vector` absolutely is the correct way to manage a dynamic array. The alternative is to juggle raw pointers, remember allocated sizes, and spend many long nights debugging memory leaks and worse. – Mike Seymour Feb 17 '15 at 15:35
  • If you say so but I would rather avoid it. – Matthew Hunt Feb 17 '15 at 15:42
1

From a quick inspection, the problem in your C++ code appears to be the following array declaration:

double A[N][2];

You need to dynamically allocate this 2d array as follows:

double** A = new double*[N];
for (int i=0; i<N; ++i)
    A[i] = new double[2];

Have a look at this SO article for more information.  

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

In the end I had to split A up into three vectors and change the output of the function from double to void and read in the (now) three vectors as double*. I can then just change the contents of the vectors and it now is showing no errors.

Matthew Hunt
  • 101
  • 2