I'm building an algorithm in which I want to generate some data using (for loop) and store them into array A. I created a function for generating these data and initalize it at the beginning. I won't call this function except I needed it.
The problem arise when I have other function that use data from this array A in a loop. When I used dynamic array, I should free the allocated memory which consequently delete the data stored in array A. I did not want to call the function for generating data each time in a loop as it was computationally expensive until I needed it.
int *arrayA;
generate_data(){
...
arrayA = newint[size]; //the size is dynamically changing
...
...
}
int main(){
generate_data();
while(1){
other_function(arrayA);
generate_data(); // when needed
delete []arrayA; // arrayA will be freed
}
}
If I use stack array, I can allocate first a huge of data storage in the beginning, and need not to be freed so I can call generata_data() as needed without losing the data in arrayA. But, it won't efficient.
Use std::vector. It can be dynamically resized and manages memory itself, so you don't need to worry about it. Just pass it by reference to avoid unnesessary copy.
But, suppose If I create a class and not function to generate and save the data. I could not pass the data (created using std::vector) in class into my other function. I mean when using class, we can't access the data that stored in vector array.
Vectors have .data() member fuction which returns pointer to underlying array. So you can pass it. And why not make other function recieve vector as well? Also use of void pointers (and raw pointers as whole) is discouraged in C++
Well, actually I'm dealing with 3D graphics library with predefined function that receive void pointer argument. .data() member is only supported in c++11 whereas my NDK do not support c++11. So what do you think would be the best way?
Before C++ 11 we used address of first element to pass underlying array: &(myvector[0])
And what about heap overallocation? It would save you time on allocating/freeing memory, copy and no risk of invalidating pointer to first element or memory leak.
Before C++ 11 we used address of first element to pass underlying array: &(myvector[0])
If myvector is declared in class, then how should I write to pass into other_function?
Yup, right now I'm still underlying on overallocation on stack array, but I think it is not efficient because it reserves some storages that it might not need.
If there could be large amounts of data, allocate it in heap.
I think it is not efficient
Speed or memory usage. You cannot have both.
If myvector is declared in class, then how should I write to pass into other_function?
What the problem in returning reference to vector or even address to first element directly? FYK data() actually returns &front() which in order returns (*this)[0]
btw, Do you mean overallocate in heap is by using vector?
No. If you decide to not use vector and use arrays instead and your array can hold more than 10000 elements, it might be good idea to place it in heap to avoid possible stack size problems.