How to pass pointer to function and dynamically allocate memory within function C++
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i