@blackcoder41:
Yes, you can do it the way you first suggested, but that's definitely not "easier to manage". Either way, you ought to be monitoring the length of your array, either by:
1) Passing a pointer to a length integer, and returning the array pointer. You can dereference the length integer's pointer and assign its value inside the function, and then return the new array.
|
int *myFunction(int *pLength);
|
If you're using C++ instead of C, you can instead just pass the length integer as a non-const reference, and you can change the value that way without worrying about pointers.
|
int *myFunction(int& length);
|
2) Passing a double-pointer and returning the length as an int. You allocate memory as described in the above answers, and just return the length as a regular int. (This is preferred over #1 in most cases).
|
int myFunction(int **pArray);
|
3) Passing both the number (reference or pointer) and the double-pointer, and returning void.
Ultimately, if you're using C++, you might as well take advantage of its standard containers (or your own). That's the whole point of the language, really - objects manage things for themselves. For example, it is always preferable to use containers like STL's vector, in place of using arrays and having to keep track of lengths yourself. If we wanted to do that all the time, we'd just use pure C.