From: Gregor Kova? on
Hi!

First I have to say that I've been learning C++ for about a week. I've been
programming in various languages (most Java) for couple of years. I've been
using a book C++ Primer to learn C++.
So on to the question:
The book says that returning a reference to a local variable is wrong since
the memory is freed when the function returns.
Does that mean that if I write a piece of code like this:
std::vector<int> getNumbers() {
std::vector<int> localVector;

localVector.push_back(8);
localVector.push_back(3);
localVector.push_back(4);

return localVector;
}

that this is also wrong? Is yes, how to deal with it? If the code is OK,
does that mean that the localVector is copied when the function returns? If
it is copied and there are a lot of items in the vector how does this
impact performance? It there a faster way of doing things?

Best regards,
Kovi
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| Gregor Kovac | Gregor.Kovac(a)mikropis.si |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
From: Mark P on
Gregor Kovac( wrote:
> Hi!
>
> First I have to say that I've been learning C++ for about a week. I've been
> programming in various languages (most Java) for couple of years. I've been
> using a book C++ Primer to learn C++.
> So on to the question:
> The book says that returning a reference to a local variable is wrong since
> the memory is freed when the function returns.
> Does that mean that if I write a piece of code like this:
> std::vector<int> getNumbers() {
> std::vector<int> localVector;
>
> localVector.push_back(8);
> localVector.push_back(3);
> localVector.push_back(4);
>
> return localVector;
> }
>
> that this is also wrong?

No, because that is not returning a reference. If the function were
instead declared as:

std::vector<int>& getNumbers();

then you would be in trouble.

Is yes, how to deal with it? If the code is OK,
> does that mean that the localVector is copied when the function returns?

Yes, if not returned by reference then it is returned by value, creating
a copy (although in some cases the compiler may optimize that copy
away-- look up "return value optimization"-- but don't worry, if the
copy is optimized away it is done so "safely")

> If
> it is copied and there are a lot of items in the vector how does this
> impact performance?

Well, they'll have to be copied. Whether this is significant is
something that you have to test.

It there a faster way of doing things?

You can pass *in* a reference or pointer to a vector and let the
function operate directly on that vector. Then you don't have to return
anything-- the vector is modified in place.

Mark
From: Gregor Kova? on
Hi!

Thanks... I thought something like this, but I just had to make sure.
I think for now I'll stick with the getNumbers() function defined like:
std::vector<int> getNumbers()
since I don't like to change the arguments of a function (a Java thing :) ).
But if the need woul arise because of the poor performance I might use your
proposed method.

Best regards,
Kovi

Mark P wrote:

> Gregor Kovac( wrote:
>> Hi!
>>
>> First I have to say that I've been learning C++ for about a week. I've
>> been programming in various languages (most Java) for couple of years.
>> I've been using a book C++ Primer to learn C++.
>> So on to the question:
>> The book says that returning a reference to a local variable is wrong
>> since the memory is freed when the function returns.
>> Does that mean that if I write a piece of code like this:
>> std::vector<int> getNumbers() {
>> std::vector<int> localVector;
>>
>> localVector.push_back(8);
>> localVector.push_back(3);
>> localVector.push_back(4);
>>
>> return localVector;
>> }
>>
>> that this is also wrong?
>
> No, because that is not returning a reference. If the function were
> instead declared as:
>
> std::vector<int>& getNumbers();
>
> then you would be in trouble.
>
> Is yes, how to deal with it? If the code is OK,
>> does that mean that the localVector is copied when the function returns?
>
> Yes, if not returned by reference then it is returned by value, creating
> a copy (although in some cases the compiler may optimize that copy
> away-- look up "return value optimization"-- but don't worry, if the
> copy is optimized away it is done so "safely")
>
>> If
>> it is copied and there are a lot of items in the vector how does this
>> impact performance?
>
> Well, they'll have to be copied. Whether this is significant is
> something that you have to test.
>
> It there a faster way of doing things?
>
> You can pass *in* a reference or pointer to a vector and let the
> function operate directly on that vector. Then you don't have to return
> anything-- the vector is modified in place.
>
> Mark

--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| Gregor Kovac | Gregor.Kovac(a)mikropis.si |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
From: Gregor Kova? on
Another thing...

If I have code like t his:

void f1(std::vector vec) {
// do something on vec
}

int main() {
std::vector numbers;
numbers.push_back(1);
numbers.push_back(2);
f1(numbers);
}

then when calling f1(numbers) the elements of the numbers vector are copied
into vec (argument of the f1 function) vector ?


Mark P wrote:

> Gregor Kovac( wrote:

> Yes, if not returned by reference then it is returned by value, creating
> a copy (although in some cases the compiler may optimize that copy
> away-- look up "return value optimization"-- but don't worry, if the
> copy is optimized away it is done so "safely")

--
----------- Gregor Kovac -----------------------------------
In A World Without Fences Who Needs Gates? Experience Linux.
From: Chris ( Val ) on

"Gregor Kovac" <gregor.kovac(a)mikropis.si> wrote in message
news:4%9Og.4922$oj5.1906790(a)news.siol.net...
| Another thing...
|
| If I have code like t his:
|
| void f1(std::vector vec) {

That's a syntax error.
void f1( std::vector<int> vec )

| // do something on vec
| }
|
| int main() {
| std::vector numbers;

Same syntax error above.

| numbers.push_back(1);
| numbers.push_back(2);
| f1(numbers);
| }
|
| then when calling f1(numbers) the elements of the numbers vector are copied
| into vec (argument of the f1 function) vector ?

Yes, inside the function f1, the parameter "vec" refers
to a local copy of "numbers", thus modifying "vec" will
have no effect on the original vector passed in.

To modify the original in function f1, you would use
"pass by reference", rather than "pass by value":

void f1( std::vector<int>& vec ) ...

In time you will find that your declarators can get pretty
large, especially with other container types and also when
nesting containers, therefore it is highly reccomended that
you use typedef(s) for a cleaner outlook on your code.

For example, if we create the following type definition:

typedef std::vector<int> IntVec;

....we could then write:

void f1( IntVec vec ) or
void f1( IntVec& vec )

"IntVec" can be considered an alias to the full declaration.

Cheers,
Chris Val


 |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: simple class
Next: istream& problem