Is this Limitation of dynamic array?

Hi,

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.

This is the illustration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int *arrayA;

generate_data(){
...
arrayA = new int[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.

So is there any trick to solve this issue?

Thanks
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.
Hi,

that's a good idea.

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.
I could not pass the data (created using std::vector) in class into my other function.
Why not? It is no different from arrays.

If you know upper limit of your array size, overallocating it first time will be a good idea too.
Hi,

Suppose my other_function receives parameter const void *array and I have a class to generate data called genData that contain vector called arrayA.

void other_function(const void *array);

and

I create an object of genData

genData gen;

void other_function(genData.arrayA);

passing the vector will produce invalid cast to const void *.

This is what I mean.
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 myvector is declared in class, then how should I write to pass into other_function?


other_function(&MyClassThing.myvector.front());

You can use [0] instead of front(), I just personally prefer the latter.

Edit: wait a minute... is myvector public?
Last edited on
on stack array
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]

What prevents you from doing this?
Hi, Catfish4

yup, myvector is public. Thanks anyway

@MiiniPaa

Ahh, I see.
btw, Do you mean overallocate in heap is by using vector?
Deduu wrote:
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.
Well. I will give a try.. Thanks alot.
Topic archived. No new replies allowed.