+ 1

Passing array to a function. Memory handling.

Hi. May I work with the array in c++ this way? Is it OK? Mainly from the memory handling point of view. Thanks. void fill_arr(int xarr[], unsigned int xsize) { ... } int main() { unsigned int size = 100; int *arr = NULL; arr = new int[size]; fill_arr(arr,size); delete [] arr; return 0; }

21st Jan 2018, 4:18 PM
Miroslav M
Miroslav M - avatar
2 Réponses
+ 1
You would need to take either a reference or pointer to the array as a parameter instead of the array itself.
23rd Jan 2018, 3:58 AM
Jacob Pembleton
Jacob Pembleton - avatar
+ 1
@Jacob So far, what I wrote, looks like it is working. Can you please give me an example what do you mean? Thanks. Trying this resulted in error: void fill_arr(int *xarr[], unsigned int xsize) { ... } int main() { unsigned int size = 100; int *arr = NULL; arr = new int[size]; fill_arr(&arr,size); delete [] arr; return 0; }
24th Jan 2018, 8:59 AM
Miroslav M
Miroslav M - avatar