By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

returning a reference variable to itself

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<co***********@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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.
Jul 23 '05 #2

P: n/a
co***********@hotmail.com wrote:
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
Jul 23 '05 #3

P: n/a
Andrew Koenig wrote:
<co***********@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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.


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
Jul 23 '05 #4

P: n/a
co***********@hotmail.com wrote:
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.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #5

P: n/a
Andrey Tarasevich wrote:
co***********@hotmail.com wrote:
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
Jul 23 '05 #6

P: n/a
co***********@hotmail.com wrote:
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.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7

P: n/a
Victor Bazarov wrote:
...
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?
...


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.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.