How to return an array from a function?
ARRAYS FUNCTIONS
Collapse
This topic is closed.
X
X
-
coinjoTags: None -
Jonathan Mcdougall
Re: ARRAYS FUNCTIONS
coinjo wrote:
Don't use all caps in the subject line.
[color=blue]
> How to return an array from a function?[/color]
[we should have a faq on this]
http://tinyurl.com/7ne54
Jonathan
-
Niklas Norrthon
Re: ARRAYS FUNCTIONS
"coinjo" <coinjo@gmail.c om> writes:
[color=blue]
> How to return an array from a function?[/color]
Several alternatives:
1. Use std::vector<>
std::vector<int > func(size_t sz)
{
std::vector<int > vec;
vec.reserve(sz) ;
for (size_t i = 0; i < sz; ++i) {
vec.push_back(i );
}
return vec;
}
2. Let the caller define the array and pass it as an argument:
void func(int* arr, size_t sz)
{
for (size_t i = 0; i < sz; ++i) {
arr[i] = i;
}
}
3. Let the function allocate an array and pass it back:
int* func(size_t sz)
{
int* arr = malloc(sz * sizeof *arr);
for (size_t i = 0; i < sz; ++i) {
arr[i] = i;
}
}
Alternative 3 is the most complex, since the caller will be
responsible for freeing the array. This needs a careful
analysis of ownership and lifetime issues.
/Niklas Norrthon
Comment
-
Niklas Norrthon
Re: ARRAYS FUNCTIONS
Niklas Norrthon <do-not-use@invalid.net > writes:
[color=blue]
> 3. Let the function allocate an array and pass it back:
>
> int* func(size_t sz)
> {
> int* arr = malloc(sz * sizeof *arr);
> for (size_t i = 0; i < sz; ++i) {
> arr[i] = i;
> }
> }[/color]
Sorry for replying to my own post, but the above is C and not C++
of course. In C++ the value returned by malloc must be cast. But
better is:
int* func(size_t sz)
{
int* arr = new int[sz];
for (size_t i = 0; i < sz; ++i) {
arr[i] = i;
}
}
And the caller must free the array with delete[] sz;
To my defence I can only say that I commonly use malloc in C
and rarely new [], and delete [] in C++, where I usually
choose one of the container classes instead.
/Niklas Norrthon
Comment
-
BobR
Re: ARRAYS FUNCTIONS
Niklas Norrthon wrote in message <0plkyyzi5n.fsf @niklas.ua.dyna s.se>...[color=blue]
>Niklas Norrthon <do-not-use@invalid.net > writes:
>[color=green]
>> 3. Let the function allocate an array and pass it back:[/color]
>[snip]
>Sorry for replying to my own post, but the above is C and not C++
>of course. In C++ the value returned by malloc must be cast. But
>better is:
>
>int* func(size_t sz){
> int* arr = new int[sz];
> for (size_t i = 0; i < sz; ++i) { arr[i] = i;}
>}
>
>And the caller must free the array with delete[] sz;
>To my defence I can only say that I commonly use malloc in C
>and rarely new [], and delete [] in C++, where I usually
>choose one of the container classes instead.
>/Niklas Norrthon[/color]
The way I see it, 'sz' is passed into the func 'by value'. That means a
temporary 'sz' is created in the func and destroyed when the func returns.
But, that is one thing, losing track of the newed memory is going to leave
you with no way to delete it. You promised to return an 'int*', but didn't.
int* func(size_t sz){
int* arr = new int[sz];
for(size_t i(0); i < sz; ++i) { arr[i] = i; }
return arr;
}
// ......
int *MyArray( func( 10 ) );
for(size_t i(0); i < 10; ++i){ std::cout<<MyAr ray[i]<<std::endl;}
// ......
delete [] MyArray;
--
Bob R
POVrookie
Comment
-
Niklas Norrthon
Re: ARRAYS FUNCTIONS
"BobR" <RemoveBadBobR@ worldnet.att.ne t> writes:
[color=blue]
> Niklas Norrthon wrote in message <0plkyyzi5n.fsf @niklas.ua.dyna s.se>...[color=green]
> >Niklas Norrthon <do-not-use@invalid.net > writes:
> >[color=darkred]
> >> 3. Let the function allocate an array and pass it back:[/color]
> >[snip]
> >Sorry for replying to my own post, but the above is C and not C++
> >of course. In C++ the value returned by malloc must be cast. But
> >better is:
> >
> >int* func(size_t sz){
> > int* arr = new int[sz];
> > for (size_t i = 0; i < sz; ++i) { arr[i] = i;}
> >}
> >
> >And the caller must free the array with delete[] sz;
> >To my defence I can only say that I commonly use malloc in C
> >and rarely new [], and delete [] in C++, where I usually
> >choose one of the container classes instead.
> >/Niklas Norrthon[/color]
>
> The way I see it, 'sz' is passed into the func 'by value'. That means a
> temporary 'sz' is created in the func and destroyed when the func returns.[/color]
It's the local sz is destroyed, but the function was called from somewhere,
and the caller of the function knows what sz is, so what's the problem?
[color=blue]
> But, that is one thing, losing track of the newed memory is going to leave
> you with no way to delete it. You promised to return an 'int*', but didn't.[/color]
My mistake. I typed too quick, correcting the mistake in my previous code.
Luckily though, the compiler would have caught this one.
/Niklas Norrthon
Comment
-
BobR
Re: ARRAYS FUNCTIONS
Niklas Norrthon wrote in message <0p4q5lz685.fsf @niklas.ua.dyna s.se>...[color=blue]
>"BobR" <RemoveBadBobR@ worldnet.att.ne t> writes:
>[color=green]
>> Niklas Norrthon wrote in message <0plkyyzi5n.fsf @niklas.ua.dyna s.se>...[color=darkred]
>> >int* func(size_t sz){
>> > int* arr = new int[sz];
>> > for (size_t i = 0; i < sz; ++i) { arr[i] = i;}
>> >}
>> >And the caller must free the array with delete[] sz;[/color][/color][/color]
[snip][color=blue][color=green][color=darkred]
>> >/Niklas Norrthon[/color]
>>
>> The way I see it, 'sz' is passed into the func 'by value'. That means a
>> temporary 'sz' is created in the func and destroyed when the func returns.[/color]
>
>It's the local sz is destroyed, but the function was called from somewhere,
>and the caller of the function knows what sz is, so what's the problem?[/color]
The problem was this line:
"And the caller must free the array with delete[] sz;"
I assume it was a 'typo', otherwise the OP might think you should do (in
main):
size_t ArrSz(23);
int *MyArray( func( ArrSz ) );
// .........
// delete [] MyArray; // correct way
delete [] ArrSz; // Bbzzzzztt, TROUBLE!
I'm sure you know you don't 'delete[]' a number when you meant to delete the
memory used by the array, but, a person just starting C++ might not.
We 'good' on this one now? :-}
--
Bob R
POVrookie
Comment
-
Niklas Norrthon
Re: ARRAYS FUNCTIONS
"BobR" <RemoveBadBobR@ worldnet.att.ne t> writes:
[color=blue]
> The problem was this line:
> "And the caller must free the array with delete[] sz;"
>
> I assume it was a 'typo', otherwise the OP might think you should do (in
> main):[/color]
Of course it was a typo, and since I know what I meant, I read it as
what I meant.
/Niklas Norrthon
Comment
Comment