Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6834575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:07:43+00:00 2026-05-26T23:07:43+00:00

I work with C++ in an embedded environment (actually a subset of C++) and

  • 0

I work with C++ in an embedded environment (actually a subset of C++) and want some advice on the use of pointers/references when passing arguments to routines.

A common tip goes something like “use references when you can, and pointers when you have to”. I believe this is related to avoiding null pointers.

However, our current programming guideline prohibits the use of references.

Please refer to the discussion thread below, and see if you agree.

Bert:
We should be taking advantage of passing arguments by reference (built-in null pointer protection) when appropriate. This would yield smaller, more robust code.

Ernie:
I disagree. Using references hides what parameters might be changed and as such, when you are reviewing code or looking at what a particular function is doing, you now have to look at each and every function it calls, and each and every function they call to see if any of the parameters you are passing in might get changed.
Whereas, if you use pointers, then you know by the fact that if you are passing in a pointer, it might get changed. If you are not passing in a pointer, then you know it has not been changed.
This ease in maintenance is worth the NULL pointer issues. With references, just because you don’t have NULL pointer issues, does not mean that the object has been correctly initialized and that you have good data.

Bert:
That concern can be mitigated by labeling input parameters (references) as “const”. (This is similar to the Google coding style.) Then you know the other parameters are the ones that may be modified. The modifiable parameters can start with the “r” prefix, which was allowed in our guidelines earlier. (And don’t be surprised when tools such as Visual Assist color code inputs/outputs to help give visual cues.)

Ernie:
Labeling parameter (references) as “const” does NOT mitigate the problem. Because when you are up a level and someone is calling the function, then you have just a regular variable like “nSomeNumber”. You MUST go to the function prototype to see if that variable might get changed. Or, you MUST hover over the function to see the prototype. So you can not just quickly scan code and have an idea of what might be happening.
Also, there are times where you do not have color coding, like in Beyond Compare during a code review, and you can not easily hover or go to the prototype.
I am strongly against the use of references, as they make code maintenance and readability difficult and error prone; and the errors you get are not the simple to detect NULL pointer issues, they are logic errors where a value is set above a function and passed into the function as a reference, and then after the function is called the value is used and not having expected it to change.

Grover:
FYI, Google allows const reference for arguments. Presumably, this is to save on the cost of copying objects. Google does not allow simple reference for arguments. If an object is to change, then pass it by pointer.
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Reference_Arguments

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:07:44+00:00Added an answer on May 26, 2026 at 11:07 pm

    A common tip goes something like “use references when you can, and pointers when you have to”. I believe this is related to avoiding null pointers.

    This is good advice, and yes, avoiding null pointers is the reason (it’s also easier to read as you don’t have to constantly dereference references, or use ->)

    Grover: FYI, Google allows const reference for arguments. Presumably, this is to save on the cost of copying objects. Google does not allow simple reference for arguments. If an object is to change, then pass it by pointer.

    I can understand the argument against using non-const references. In that case, Google’s style guide is the best approach (use const references for inputs, pointers for outputs). Not using const references for inputs is just madness: you either have to pass by value, which will be incredibly slow, or pass by pointer, which is going to require a hell of a lot of address taking and dereferencing (not to mention pointless null pointer checks). That would make the code far more unreadable than worrying about modified references.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm responsible for some embedded software that has to work with a customer's proprietary
I am trying to work with some embedded objects in Word documents. An earlier
I work in the embedded world, using mainly C and no GUI at all
The media player embedded code work only in IE.How I can do it able
Hi I am having trouble getting an embedded js file to work. I have
I am setting up embedded forms in my rails app. This does not work
work on SQL Server 2000. want to Automated Email Notifications using SQL Server Job
I'd like to use embedded IronPython as a way of allowing users to script
I'm an Ubuntu Linux user and I use Win XP only for my work
I use Linux as primary OS. I need some suggestions regarding how should I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.