Thread: Using local parameters by reference

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Using local parameters by reference

    Hi All,

    I am maintaining some code that looks a bit quirky - but seems to behave as expected. I'd rather not change it, as the interface ("second_function_called" in the below example) is used everywhere, but if anyone believes the code looks illegal, please tell. It's certainly not best practice.

    The program is single-threaded. I've simplified it for the purpose of analysis:

    Code:
    void first_function_called(int a)
    {
      // pass parameter "a" through to another function for read access only
    
      second_function_called(&a);
    }
    
    void second_function_called(int *b)
    {
      some_struct s;
    
      // Get a copy of b and use it for something
    
      s.some_field = *b;
    
      // Parameter "b" is never manipulated, passing a copy of "b" would have been fine!!!
    }
    I guess the question is - is taking the address of a function parameter and then passing it off to another function for read access OK? I believe it probably is, as it should just be taking the address of the parameter off the first functions stack space.

    Thankyou.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You'd usually see something like this when code has changed -- perhaps the value used to be altered instead of just read.

    As you say, I'd keep the API the same to avoid breaking a bunch of code. But I'd also at least change it to a const pointer to enforce the fact that the second_function_called() does not change the value it points to.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm guessing the point is given in your first comment -- second_function_called can change the value of the parameter passed in (I can't say for sure that it does, but I would imagine so) -- the first_function_called thus "protects" the original value by letting the second function change a harmless copy.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Quote Originally Posted by brewbuck View Post
    You'd usually see something like this when code has changed -- perhaps the value used to be altered instead of just read.

    As you say, I'd keep the API the same to avoid breaking a bunch of code. But I'd also at least change it to a const pointer to enforce the fact that the second_function_called() does not change the value it points to.
    Good tip brewbuck. I've already added "const" to quite a few function headers already, this is just another

    This code is legit though right? I've never seen it do anything weird - and to the second reply of this post, no, the parameter pointed to by "b" is never modified.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tristan70 View Post
    This code is legit though right? I've never seen it do anything weird - and to the second reply of this post, no, the parameter pointed to by "b" is never modified.
    It's probably not how you'd write it if you wrote it today, but it's legitimate. This is just what happens when code evolves.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 09:02 AM
  2. reference parameters
    By LowLife in forum C Programming
    Replies: 8
    Last Post: 01-25-2006, 11:50 AM
  3. Reference parameters and calculating change
    By Cstudent2121 in forum C Programming
    Replies: 6
    Last Post: 11-04-2005, 03:19 PM
  4. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  5. Passing parameters by reference
    By aker_y3k in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2003, 06:46 PM