0

I've got a syntax question in C++ about returning an array. When we pass in an array, when can do like this:

void merge_sort(int input_array[], int size);//notice the first parameter

I know this works:

int* merge_sort(int input_array[], int size){
    //do something with input_array
    return new int[2]; //dummy array
}

Question:

int[] merge_sort(int input_array[], int size){ //Question is on return type, wont compile
     //do something with input_array
    return new int[2]; //dummy array
}

return int* succeed. Why returning int [] fail?

SDEZero
  • 363
  • 2
  • 13

3 Answers3

2

In both C and C++, you cannot either pass an array as an argument to a function, or return an array as a function result.

Yes, the syntax can make it look like you're passing an array argument:

void func(int param[]) {
    // ...
}

...

int arr[10];
func(arr);

but in fact that's just passing a pointer to the array's first element. The parameter definition int param[] is adjusted; it's exactly equivalent to int *param. And an expression of array type, in most contexts, is implicitly converted to a pointer its first element.

That's why you need to pass the size as a separate argument.

Using only C features, there are several ways to do something like returning an array:

  • A function can return a pointer to the first element of a static array. This has some disadvantages: the size has to be fixed, and multiple callers get pointers to the same object.
  • A function can receive a pointer to the first element of an array, passed in by the caller. This places the burden of allocating and deallocating the array on the caller.
  • A function can return a pointer to the first element of a dynamically allocated (malloc() in C, new in C++) array. This requires the caller to deallocate the array.

C++ provides a rich set of library classes that can take care of all this for you.

Recommended reading: Section 6 of the comp.lang.c FAQ.

You can pass structs as arguments, and return them as function results, and structs can contain arrays as members. But that's not as useful as you might think. An array that's a member of a struct must have a fixed size; most useful code that operates on arrays can handle dynamic sizes.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

Why would you want to return anything in the first place?

Array is not really "passed" to the function (no copy of the elements is made), only the pointer to it's beginning is. When the function rearranges the array elements, it is doing so in the original array. After the function exits, the calling code can simply continue to use the old (now-sorted) array.

In other words, the function produces a side-effect.

I know this works:

int* merge_sort(int input_array[], int size){
    //do something with input_array
    return new int[2]; //dummy array
}

(Assuming you actually need to return...)

Well it "works", but is very dangerous. The caller needs to be aware it needs to free the returned array (otherwise a memory leak ensues), and it needs to be aware it must use delete[] and not just delete (otherwise an undefined behavior ensues), which is distinctly non-obvious based only on the return type (which is just a pointer). The caller might also be in doubt whether the input_array is freed inside the function or not.

Even if you have documented all that, it's extremely easy for the caller to make a mistake. It's much better to use the facilities provided by modern C++ instead: for example, you could return an std::vector.

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
0

you should be using a vector in this approach, in your project include and then use a vector almost as u would with an array.

u can have a look at vectors here: http://msdn.microsoft.com/en-us/library/vstudio/9xd04bzs.aspx

the great thing about vectors is that they are dynamically scaling for your projects and would not create any memory leaks or such due to buffer overflow's, its much like an ArrayList

Paze
  • 49
  • 7