Is there any difference in processing speed if use pointers in function instead of passing by value?

Dani AI

Generated

Short answer: it depends on the type and on what you mean by "better/faster." For small built-in types (ints, doubles, pointers) passing by value is simple and typically as fast as passing a pointer because the argument itself fits in a register. For large or non-trivial objects the cost of copying can be high, so avoiding an unnecessary copy (for example with a const reference) is usually the right choice. Ownership, lifetime and whether the callee must be able to mutate the object should drive the decision more than microbenchmarks.

Practical rules of thumb:

  • For small PODs (int, double, pointer): pass by value.
  • For large, non-trivial objects: pass by const T& if you only need read access.
  • If the function needs its own copy (or to take ownership): accept T by value and move it internally (C++11+), or use std::move where appropriate.
  • For polymorphism or optional-null semantics: use references for mandatory arguments, pointers (or std::optional/std::unique_ptr) when null/ownership semantics are needed.
  • Prefer references over raw pointers when nullability is not required; prefer smart pointers when ownership is involved.

Performance notes and caveats: modern ABIs pass many arguments in registers and compilers aggressively optimize and inline, so the practical differences are often smaller than you expect. Pointer indirection can harm locality and add a dereference cost; copying can be optimized away in many cases. Follow the intent-first rule (clarity, safety, correct ownership), then measure hotspots with a profiler before micro-optimizing. This ties together points made by , , and : semantics first, then measure.

Recommended Answers

All 6 Replies

When you pass by value, you "create" a second instance of the variable, local only to the function receiving it...

When you pass by pointer/reference you can see it as passing a big neon arrow pointing to the original variable...

thus you can imagine, as no additional memory is allocated or the value is not duplicated, it is much more efficient to pass a pointer/reference rather than by value, however this would mean that the function called would be able to change the value of the variable and have that change reflected back to the original...

To avoid this you could create a new variable in the function containing the value of the pointer passed, but then you are doing exactly the same as passing by value

alternatively... passing

"const int* MyInt"

would pass a pointer and make it constant (not changable)

Well, consider this: When you pass a pointer to a function, that pointer is being passed by value. So which is faster, passing a pointer by value or passing a value by value? I don't see how a pointer can be faster. If I am figuring this right, when the source code is translated into machine code, the machine code for dealing with a pointer would be a tad bit larger and take a little bit longer to address the data than directly addressing the data. But that may depend on the processor.

Mandrew I think we are talking past each other...

What I am saying:

void class::a(){

    int MyInt = 5;

    b(MyInt);

    //MyInt = 6
}

void class::b(int* MyIntPointer){

    MyIntPointer++;
}

What you are saying:

void class::a(){

    int* MyInt = 5;

    b(MyInt);

    //MyInt = 5
}

void class::b(int MyIntPointer){

    MyIntPointer++;
}

See http://www.cprogramming.com/tutorial/lesson6.html

Pointers are passed by value. What this means is that for POD types passing a value to a function by pointer instead of by value makes very little difference. Imagine a 32 bit machine with 32 bit ints and 32 bit pointers. You have an integer to pass so in either case you are poutting 32 bits onto the stack.

You only really need to use pointers to pass POD types if the function you are calling is going to alter that parameter and the calling code wants to receive the new value set by the function.

However once you introduce classes then passing by pointer can have a very large effect. You may not know the implementation of a particular class but passing it by value means that the entire class needs to be copied. In this instance passing the class to the function by pointer can save a lot of processing and memory allocation/deallocation.

If you are going to pass by pointer then remember it is better where possible to actually pass by reference (since the calling code does not then need to check that the pointer is valid) and where possible, where the function does not actually change the data pointed to/referenced you should make the pointer or reference const.

Ah! Serious beginner C++ programmer errors. Reference variables are much like pointers, but you can declare them as const, and thus refuse the called funtion from modifying them without going through some contortions (such as explicitly casting them as non-const). The issue is your intention. If you WANT the called function to be able to modify the contents of the object, then pass them as non-const, otherwise, a const reference is preferable. For C++, passing variables as pointers is usually not recommended. Example (from your source):

void class::b(int& MyIntRef)
{
    MyIntRef++;
}

void class::a()
{
    int MyInt = 5;
    b(MyInt);
    //MyInt now == 6
}

Clearer now?

Reference variables are much like pointers, but you can declare them as const

You seem to imply that points can't be declared const, I'm not sure if that was your intention but pointers can be declared const just to clear up any confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.