Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

array as return value: possible?

0 views
Skip to first unread message

Axel

unread,
Oct 24, 2002, 10:35:18 AM10/24/02
to
I am trying to create a function wich deliver back an array,
like:

int DoSomething()
{
int val[7];
...
return val;
}

in this way it is not possible! Do I have to use pointers?
Has someone an idea?

thanks in advance
Axel


Neil Butterworth

unread,
Oct 24, 2002, 10:43:52 AM10/24/02
to

"Axel" <a.w...@fh-oldenburg.de> wrote in message
news:ap90hb$sogbi$1...@ID-77641.news.dfncis.de...

> I am trying to create a function wich deliver back an array,
> like:
>
> int DoSomething()
> {
> int val[7];
> ...
> return val;
> }
>
> in this way it is not possible!

You cannot pass or return arrays by value in C++.

> Do I have to use pointers?

You could create an array dynamically and returna pointer to it, but I'd
suggest a better solution is to use a std::vector:

std::vector <int> DoSomething() {
std::vector <int> val(7);
...
return val;
}

NeilB


Axel

unread,
Oct 24, 2002, 10:51:31 AM10/24/02
to
Great,
that' s the way it works.

cheers
Axel

"Neil Butterworth" <neil_but...@lineone.net> schrieb im Newsbeitrag
news:3db80...@mk-nntp-1.news.uk.worldonline.com...

Victor Bazarov

unread,
Oct 24, 2002, 11:08:12 AM10/24/02
to
"Axel" <a.w...@fh-oldenburg.de> wrote...

> I am trying to create a function wich deliver back an array,
> like:
>
> int DoSomething()
> {
> int val[7];
> ...
> return val;
> }
>
> in this way it is not possible! Do I have to use pointers?
> Has someone an idea?


Arrays cannot be the return value type of functions. Use vector<>:

vector<int> DoSomething()
{


vector<int> val(7);
...
return val;
}

Victor
--
Please remove capital A's from my address when replying by mail


tom_usenet

unread,
Oct 24, 2002, 12:08:00 PM10/24/02
to
On Thu, 24 Oct 2002 16:35:18 +0200, "Axel" <a.w...@fh-oldenburg.de>
wrote:

>I am trying to create a function wich deliver back an array,
>like:
>
>int DoSomething()
>{
> int val[7];
> ...
> return val;
>}
>
>in this way it is not possible! Do I have to use pointers?
>Has someone an idea?

In addition to the vector suggestion, you can return structs
containing arrays.

struct S
{
int val[7];
};

S DoSomething()
{
S s;
...
return s;
}

The array is copied when S is copied.

Tom

Alex Kro

unread,
Oct 25, 2002, 4:03:33 AM10/25/02
to
"Victor Bazarov" <vAba...@dAnai.com> wrote in message
news:urg35br...@corp.supernews.com

> "Axel" <a.w...@fh-oldenburg.de> wrote...
>> I am trying to create a function wich deliver back an array,
>> like:
>>
>> int DoSomething()
>> {
>> int val[7];
>> ...
>> return val;
>> }
>>
>> in this way it is not possible! Do I have to use pointers?
>> Has someone an idea?
>
>
> Arrays cannot be the return value type of functions. Use vector<>:
>
> vector<int> DoSomething()
> {
> vector<int> val(7);
> ...
> return val;
> }

This is not very efficient, isn't it? A copy constructor is invoked to
return the vector by value. Although NRVO can be used here, it may not be
supported by your favourite compiler.

--
alex kro


Neil Butterworth

unread,
Oct 25, 2002, 5:04:25 AM10/25/02
to
"Alex Kro" <res0...@verizon.net> wrote in message
news:pV6u9.9802$iV1....@nwrddc02.gnilink.net...

> "Victor Bazarov" <vAba...@dAnai.com> wrote in message
> news:urg35br...@corp.supernews.com
> > "Axel" <a.w...@fh-oldenburg.de> wrote...
> >> I am trying to create a function wich deliver back an array,
> >> like:
> >>
> >> int DoSomething()
> >> {
> >> int val[7];
> >> ...
> >> return val;
> >> }
> >>
> >> in this way it is not possible! Do I have to use pointers?
> >> Has someone an idea?
> >
> >
> > Arrays cannot be the return value type of functions. Use vector<>:
> >
> > vector<int> DoSomething()
> > {
> > vector<int> val(7);
> > ...
> > return val;
> > }
>
> This is not very efficient, isn't it?

It's very efficient from the standpoint of programmer time and program
correctness.

NeilB


Victor Bazarov

unread,
Oct 25, 2002, 10:07:23 AM10/25/02
to
"Alex Kro" <res0...@verizon.net> wrote...


There are ways to overcome "inefficiency". First, on successful
proof of "inefficiency" not affecting overall program performance
(due to the fact that it happens very rarely, for example) the
whole thing can be left alone. Second, if there is a necessity
to avoid extra copying of the data, the MOJO technique could be
used (see latest posts from Andrei Alexandrescu in comp.std.c++).
You could also just wrap the array in an object that does efficient
memcpy or memmove (since the array is of fundamental type).

But before anything can happen there has to be proof of inefficiency.

E. Robert Tisdale

unread,
Oct 25, 2002, 10:56:50 AM10/25/02
to
Alex Kro wrote:

> This is not very efficient, isn't it?

It is efficient.

> A copy constructor is invoked to return the vector by value.
> Although NRVO can be used here,

> it may not be supported by your favorite compiler.

If your favorite [C++] compiler does not support the NRVO,
then you should be shopping around for a C++ compiler
that does support the NRVO.

0 new messages