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

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.