returning a reference variable to itself

C

confusedcoder

I found a strange piece of code that basically did the following:

int doSomething(int& foo)
{
//manipulate the value of foo
return foo;
}

int main(int argc, char* argv[])
{
int a = 5;

a = doSomething(a);

return 0;
}

Aside from being very bad practice, what problems does this present? I
am asking because it was causing the program to crash on a small number
of computers that it was running on. I have changed the code so that
it doesn't crash anymore, but I am curious as to what the problem may
have been.
 
A

Andrew Koenig

I found a strange piece of code that basically did the following:

int doSomething(int& foo)
{
//manipulate the value of foo
return foo;
}
a = doSomething(a);
Aside from being very bad practice, what problems does this present?

The main problem is that doSomething's interface seems somewhat muddled.
Why return the same value twice? It stores it in its parameter and also
returns it as an explicit result.

But aside from the wasted effort and duplication, I don't see any particular
problems.
 
V

Victor Bazarov

I found a strange piece of code that basically did the following:

int doSomething(int& foo)
{
//manipulate the value of foo
return foo;
}

int main(int argc, char* argv[])
{
int a = 5;

a = doSomething(a);

return 0;
}

Aside from being very bad practice,

Who says it's bad practice?
what problems does this present? I
am asking because it was causing the program to crash on a small number
of computers that it was running on.

Really? What was the cause? I couldn't be the sheer passing the same
variable as the one the return value assigned to, could it?
I have changed the code so that
it doesn't crash anymore, but I am curious as to what the problem may
have been.

I don't see any problem. Were the changes you made relevant to the your
question?

V
 
V

Victor Bazarov

Andrew said:
The main problem is that doSomething's interface seems somewhat muddled.
Why return the same value twice? It stores it in its parameter and also
returns it as an explicit result.

But aside from the wasted effort and duplication, I don't see any particular
problems.

Returning a value could be a requirement for template code that has to
chain some calls. It could be that 'doSomething' is used to print the
value, like

printf("%d", doSomething(a));

which could be remedied by

printf("%d", (doSomething(a),a));

but is increasingly ugly. In any way, the need to use the function in
an expression is the single requirement that calls for the return value
to be there.

V
 
I

Ioannis Vranos

I found a strange piece of code that basically did the following:

int doSomething(int& foo)
{
//manipulate the value of foo
return foo;
}

int main(int argc, char* argv[])
{
int a = 5;

a = doSomething(a);

return 0;
}

Aside from being very bad practice, what problems does this present?



No problems at all. The returned value is copied to a, that is the above
is equivalent to:


int a = 5;

doSomething(a);

a=a;





I
am asking because it was causing the program to crash on a small number
of computers that it was running on. I have changed the code so that
it doesn't crash anymore, but I am curious as to what the problem may
have been.


Perhaps something else was the cause.
 
A

Andrey Tarasevich

I found a strange piece of code that basically did the following:

int doSomething(int& foo)
{
//manipulate the value of foo
return foo;
}

int main(int argc, char* argv[])
{
int a = 5;

a = doSomething(a);

return 0;
}

Aside from being very bad practice, what problems does this present?

I don't see how it is necessarily bad. Seemingly redundant returning of
the modified argument through the function's return value has been an
accepted practice in C and C++ languages for quite some time now. Take a
look at 'strcpy' and/or 'strcat', for example. They return their first
arguments. The main purpose of this is to allow chaining of function
calls inside a single expression, like

FILE* f = fopen(
strcat(strcat(strcpy(file_name, name), separator), extension),
"w");

(Disclaimer: this is not an example of safe and/or efficient code, this
is just an example that demonstrates function calls chained together is
a single expression).

In your case it might look as follows

x = doSomethingDifferent(doSomethingElse(doSomething(a)));

or maybe as a repetitive application of the same function to the same
argument

doSomething(doSomething(doSomething(a)));
I am asking because it was causing the program to crash on a small number
of computers that it was running on.

The practice alone can't be the main reason for the crash. The root of
the problem is(was?) hidden somewhere else.
 
V

Victor Bazarov

Andrey said:
I found a strange piece of code that basically did the following:

int doSomething(int& foo)
{
//manipulate the value of foo
return foo;
}

int main(int argc, char* argv[])
{
int a = 5;

a = doSomething(a);

return 0;
}

Aside from being very bad practice, what problems does this present?

[...]

In your case it might look as follows

x = doSomethingDifferent(doSomethingElse(doSomething(a)));

or maybe as a repetitive application of the same function to the same
argument

doSomething(doSomething(doSomething(a)));

I don't think it's OK since the return is an rvalue, and a non-const ref
won't bind to it... Or will it?

V
 
A

Andrey Tarasevich

Victor said:
I don't think it's OK since the return is an rvalue, and a non-const ref
won't bind to it... Or will it?
...

No, it won't. Sorry, my mistake. For some reason I thought that
'doSomething' is declared as

int& doSomething(int& foo)

In the OP's case the function can still be used for... hm, what should I
call it?... "rvalue chaining", but I don't know whether that was the
main intent of the author of the code in question.
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top