Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ARRAYS FUNCTIONS

Reply
Thread Tools

ARRAYS FUNCTIONS

 
 
coinjo
Guest
Posts: n/a
 
      12-06-2005
How to return an array from a function?

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      12-06-2005
coinjo wrote:

Don't use all caps in the subject line.

> How to return an array from a function?


[we should have a faq on this]

http://tinyurl.com/7ne54


Jonathan

 
Reply With Quote
 
 
 
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-06-2005
"coinjo" <(E-Mail Removed)> writes:

> How to return an array from a function?


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
 
Reply With Quote
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-06-2005
Niklas Norrthon <(E-Mail Removed)> writes:

> 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;
> }
> }


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
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      12-07-2005

Niklas Norrthon wrote in message <(E-Mail Removed)>...
>Niklas Norrthon <(E-Mail Removed)> writes:
>
>> 3. Let the function allocate an array and pass it back:

>[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


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<<MyArray[i]<<std::endl;}
// ......
delete [] MyArray;

--
Bob R
POVrookie



 
Reply With Quote
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-07-2005
"BobR" <(E-Mail Removed)> writes:

> Niklas Norrthon wrote in message <(E-Mail Removed)>...
> >Niklas Norrthon <(E-Mail Removed)> writes:
> >
> >> 3. Let the function allocate an array and pass it back:

> >[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

>
> 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.


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?

> 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.


My mistake. I typed too quick, correcting the mistake in my previous code.
Luckily though, the compiler would have caught this one.

/Niklas Norrthon
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      12-07-2005

Niklas Norrthon wrote in message <(E-Mail Removed)>...
>"BobR" <(E-Mail Removed)> writes:
>
>> Niklas Norrthon wrote in message <(E-Mail Removed)>...
>> >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;

[snip]
>> >/Niklas Norrthon

>>
>> 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.

>
>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?


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


 
Reply With Quote
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-08-2005
"BobR" <(E-Mail Removed)> writes:

> 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):


Of course it was a typo, and since I know what I meant, I read it as
what I meant.

/Niklas Norrthon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments