ARRAYS FUNCTIONS

N

Niklas Norrthon

coinjo said:
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;
}
}

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

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
 
N

Niklas Norrthon

Niklas Norrthon said:
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;
}
}


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

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
 
B

BobR

Niklas Norrthon wrote in message said:
Niklas Norrthon said:
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;}
}

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; }
return arr;
}
// ......
int *MyArray( func( 10 ) );
for(size_t i(0); i < 10; ++i){ std::cout<<MyArray<<std::endl;}
// ......
delete [] MyArray;
 
N

Niklas Norrthon

BobR said:
Niklas Norrthon wrote in message said:
Niklas Norrthon said:
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;}
}

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
 
B

BobR

Niklas Norrthon wrote in message said:
BobR said:
Niklas Norrthon wrote in message said:
int* func(size_t sz){
int* arr = new int[sz];
for (size_t i = 0; i < sz; ++i) { arr = 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? :-}
 
N

Niklas Norrthon

BobR said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top