When I needed to allocate memory through a function, most of the time I was using double pointers (when I was experiencing C). However, now I'm learning C++ and I was wondering if there is a way to replace double pointer with a reference (&) or something similar?
See a little example bellow!
struct Matrix {
int** pMatrix;
int row, column;
};
int allocate_memory(Matrix** ppMatrix) {
// allocate memory for ppMatrix
}
int main() {
Matrix* ptr = nullptr;
allocate_memory(&ptr);
return 0;
/*
Can we do something like this?
Matrix obj_matrix;
allocate_memory(obj_matrix);
...
*/
}
Edit: some people have been saying that I should avoid using direct allocation in my c++ program. The thing is I'm not allowed to use containers yet since we didn't learn them on the lesson so.. I'm basically forced to do stuff with pointers, yeah..