+ 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; }
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.
+ 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;
}