How to write a function that returns an array?

[quote author=Nick Gammon link=topic=106013.msg797614#msg797614 date=1337298880] Assuming you are correct about the shallow copy, you still need to have the array in the caller (for it to be copied into). So you don't achieve anything over passing that other array by reference anyway.

[/quote]

You're mistaken.

When the return type of the function is a struct, the space for the return value in the stack frame is the size of the struct (significantly, in this case, [u]including the array elements[/u]). The struct contains the array elements, not a pointer to the array. So you can execute:

Name myName = getName();

The return statement does a shallow copy of the struct (including the content of the array) into the return stack frame, and then as the call completes the return value is copied from the stack frame to the myName variable. It is exactly the same as when a scalar type is returned, except that it is applied to the entire struct.

The semantics of struct assignment and array assignment are very different in this respect. It's an inconsistency that was introduced to the language spec at a very early stage and is now so entrenched that I don't see it ever being corrected.

PeterH: The return statement does a shallow copy of the struct (including the content of the array) into the return stack frame, and then as the call completes the return value is copied from the stack frame to the myName variable.

All I am saying is that in the example code just above no copying at all takes place. There is one instance "n_Name" which resides in the caller. The called function gets it by reference, no copying in, no copying out. Just the one piece of memory.

[quote author=Nick Gammon link=topic=106013.msg798496#msg798496 date=1337375121]

PeterH: The return statement does a shallow copy of the struct (including the content of the array) into the return stack frame, and then as the call completes the return value is copied from the stack frame to the myName variable.

All I am saying is that in the example code just above no copying at all takes place. There is one instance "n_Name" which resides in the caller. The called function gets it by reference, no copying in, no copying out. Just the one piece of memory. [/quote]

I accept and agree that it is possible to have the caller pass in the variable address/reference and have the called function put them in that location.

The point I was trying to make is that without using those approaches, and just using a plain old function return value, it is possible to return the array (in its entirety, including all the elements) as part of a struct value returned by the function.

Name myName = getName();

OK, I see. A shallow copy you say? So no copy constructor, or operator= called? I’ve seen things like this before:

String foo ()
  {
  String bar = "hello world";
  return bar;
  }

They seem to work. You could have to look closely to see if the exact object was returned (which you seem to be saying would happen) or a constructed copy.