TIP: Be Careful with Dummy Reference Arguments

How many times have you used functions that return results through reference parameters, such as this:

void func(int & ra1, int & ra2, int & ra3) ;

You would probably call this function this way:

func(a1, a2, a3) ;

Sometimes, if you do not want the outputs of ra1 and ra2 arguments, but only interested in ra3, you might be tempted to make a call to the function as follows:

int dummy, res ;
func(dummy, dummy, res) ;

This way, you might end up saving the space that is required for that one extra int argument by using dummy in place of arguments a1 and a2. The function may even work properly. But, what's important to note is that if the output argument ra3 is dependent on the values already computed in ra1 and ra2 by func, you might be looking at an entirely different scenario. Take the following example implementation of func:

# include <iostream.h>

void
func(int & ra1, int & ra2, int & ra3)
{
   ra1 = 1 ;
   ra2 = 2 ;
   ra3 = ra1 + ra2 ;
}

void
main()
{
   int dummy, res ;
   func(dummy, dummy, res) ;

   cout << res ;
}

No points for guessing the output. It's not 3 (as expected), but 4. This is obviously due to ra1 and ra2 being allotted the same address, that of dummy. Disaster!! You would have successfully traded accuracy/correctness for a simple saving of 4 bytes in virtual space.

Therefore, in cases where you are not sure whether the values of one result parameter affects others, safely use different variables to hold output parameters, as in:

int  dummy1, dummy2, res ;
func(dummy1, dummy2, res) ;


About the Author

Ashwin Kumar

A mind that's ready to keep learning.

Comments

  • Is it not the other way round?

    Posted by prantlf on 02/24/2009 04:30pm

    A method implementation should not make assumptions how the parameters must be entered. Otherwise the implementation will be difficult to maintain and a cause of many difficult to find troubles. In this case, local variables should be used and the output parameters filled at the end. Don't let yourself discouraged and keep learning :-) Cheers, Ferda Prantl

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds