1

I have a function noise.cpp which is currently of the form,

double* noise(int* steps, ...)

//some code

std::array<double, *steps> NoiseOut;

//rest of code

Which is being tested and accessed by cppnoise.cpp,

#include <random>
#include <cmath>
#include<stdio.h>
#include <iostream>
#include "noise.h"

main(){

    double* out;

    int steps = 8;
    int* ptr = &steps;

    out = noise(ptr, 1, 1000, 0.1, 1, 3);
    printf("TEST \n");
    std::cout << out[0];

}

With header file,

extern double* noise(int*, double, double, double, double, double); 

Previously I accessed the noise.cpp function through Python where the NoiseOut array was initially, double* NoiseOut = new double[steps]; with desirable results, but this method resulted in a memory leak.

Initially I tried deleting the allocated memory. But the function returns NoiseOut, so I am not sure if that's possible? So, instead I found out that the modern way is to use std::array as it comes with some form of garbage collection. If I tried to do,

double* noise(int steps, ...)

std::array<double, steps> NoiseOut;

I was told steps was not a constant expression. I tried every which way of constexpr, const, and static but, with no success. Usually with the same error error: ‘steps’ is not a constant expression. Also, the reason I pass a pointer from cppnoise.cpp to noise.cpp is because I read somewhere that the pointer is easier to work with, later in compile-time? Such that maybe I could convert it to a constant expression? Probably a fever dream.

So, how can I declare an integer value in a program, which I pass to a function, which is usable in an std::array without causing that error?

NOTE: I am very new to c++ and work primarily with SQL, Python, R, and SageMath.

  • `std::array` needs to know the size at compile time. If you don't know that at compile time (which you don't in your example), then you should probably use an `std::vector` instead. –  Oct 29 '19 at 01:45

1 Answers1

0

std::array is ill suited for this because you don't know what size you need until the code runs. std::array needs to know the size when it is compiled.

Using new gives a dynamic array size at run time, which is why you could use it before.

If you are concerned about memory leaks (or actually, in general), then I suggest using an std::vector instead:

#include <vector>
//...
std::vector<double> NoiseOut;
NoiseOut.reserve(*steps);

An std::vector should allow you to do most everything an std::array or C Array would allow you to so, though I suggest reading up on its documentation (linked above). Note that std::vector also comes with its own garbage collection of sorts in the same way std::array does.

  • So, I did try the std::vector approach! But I had a helluva time converting std::vector to double*. Any ideas there? – Winston Campeau Oct 29 '19 at 02:28
  • [This post](https://stackoverflow.com/q/2923272/10957435) should give you more information on that. –  Oct 29 '19 at 02:50
  • Cheers! Valgrind is reporting no leaks and code seems to compile fine! – Winston Campeau Oct 29 '19 at 02:54
  • Awesome. Glad to hear it. If this solved your problem, feel free to mark as accepted and/or up-vote it. If you have any more questions, feel free to ask another question. –  Oct 29 '19 at 02:56