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 4621306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:40:56+00:00 2026-05-22T02:40:56+00:00

Hey there, I wonder if it’s worth passing primitive single values like int ,

  • 0

Hey there,
I wonder if it’s worth passing primitive single values like int, float, double or char by pointer? Probably it’s not worth!? But if you would simply pass everything by pointer, is this making the program slower?
Should you always just pass arrays as pointer?
Thanks!

  • 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-22T02:40:57+00:00Added an answer on May 22, 2026 at 2:40 am

    I wonder if it’s worth passing primitive single values like int, float, double or char by pointer?

    What are you trying to accomplish? Do you want to be able to write to the passed in value? Or do you just need to use it? If you want to write to it, the idiomatic way is to pass by reference. If you don’t need to write to it, you’re best avoiding any risk that you’ll write to it accidentally and pass by value. Pass by value will make a copy of the variable for local use. (as an aside, if you don’t want to make a copy AND want some level of safety, you can pass by const reference)

    But if you would simply pass everything by pointer, is this making the program slower?

    Difficult to say. Depends on a lot of things. In both pass by value and pass by reference (or pointer) your making a new primitive type. In pass by value, you’re making a copy. In pass by reference/pointer you’re passing an address to the original. In the latter case, however, you’re requiring an extra fetch of memory that may or may not be cached. Its very difficult to say 100% without measuring it.

    That all being said, I doubt the difference is even noticeable. The compiler may be able to optimize out the copy in many pass-by-value cases, as indicated in this article. (thanks Space C0wb0y).

    Should you always just pass arrays as pointer?

    From this.

    In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address.

    To pass an array:

     int foo(int bar[], unsigned int length)
     {
           // do stuff with bar but don't go past length
     }
    

    I’d recommended avoiding arrays and using std::vector which has more easily understood copy semantics.

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

Sidebar

Related Questions

Hey there, I'm currently developing a Mex-file in matlab including CUDA computation. I wonder
Hey there I have this function: void vdCleanTheArray(int (*Gen_Clean)[25]) { } I wanted to
hey there i wonder if any of you have come across a similar issue?
Hey there, I've got a block of HTML that I'm going to be using
Hey there, I am trying to get my signout button to work. This is
Hey there, I'm sort of new to Objective-C, and well, programming in general. I
Hey there, I'm a designer thats really new to programming with xcode or Objective-C
Hey there, I'm trying to validate a form with Zend_Validate and Zend_Form. My element:
hey there, i have a div that expands when the page is loaded, now
Hey there. I'm trying to write a small program that will read the four

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.