简体   繁体   中英

Pointers function arguments copy and performance in C++

First question:

Many times we pass a reference of one object to another via function call using pointers.For example:

int num =25;
int *num1Ptr=#
int *num2Ptr=NULL;

void assinNum (int *numPtr){

    num2Ptr = numPtr; ////we copy the value (address) of the num1Ptr pointer to num2Ptr
}

My question is : if such a method gets called very frequently can we expect a significant overhead of pointers copy?

Second question :

In the following scenario , does it mean we copy the value in the memory address pointed by passed numPtr to the memory address pointed by num2Ptr? If yes ,then it is the same as passing by value ?

int num =25;
int *num1Ptr=#
int *num2Ptr=NULL;

void assinNum (int *numPtr){

    *num2Ptr = *numPtr; ////num2Ptr points to the same value in the memory pointed by numPtr argument. 
}

Update for the first question:

What are the consequences for the pointers to large objects (not primitives)?

if such a method gets called very frequently can we expect a significant overhead of pointers copy?

A pointer is usually just a 32-bit or 64-bit quantity. So copying a pointer just involves copying a 32-bit or 64-bit quantity, which is very cheap on most platforms. However, copying an int directly is also very cheap, so in this case, using pointers probably doesn't bring you much benefit.

It's also worth pointing out that in many situations, the compiler will optimize this function by inlining it.

does it mean we copy the value in the memory address pointed by passed numPtr to the memory address pointed by num2Ptr?

In theory, yes. However, num2Ptr = NULL , so your code is likely to cause a segmentation fault.

then it is the same as passing by value ?

I'm not sure what you're referring to, so it's difficult to know how to answer this!

if such a method gets called very frequently can we expect a significant overhead of pointers copy?

"Overhead" implies that you're comparing some amount of optional or spurious work being done to the amount of work that actually needs to be done to achieve the specific desired effect. The difference between the total work and the minimum required work is overhead. In this case, it's not clear what your baseline is. What is it that you'd consider overhead? The operation of copying one pointer to another is very small -- it's just a 32- or 64-bit assignment, depending on your target platform. Such an operation isn't free, but it's very fast.

memory address pointed by passed numPtr to the memory address pointed by num2Ptr?

Yes, the code you show copies the value in the memory referenced by numPtr to the memory referenced by numPtr2 . Of course, in your example, the pointers reference the address 0x00000019 and 0x00000000, respectively, so you'll crash when reading the source value unless you know you've got readable memory there; and crash when writing unless you know you've got writeable memory there, too (and you probably don't). Note that your comment ( ////num2Ptr points to the same value in the memory pointed by numPtr argument. ) is incorrect.

If yes ,then it is the same as passing by value ?

Passing a pointer is not like passing by value. A pointer is a reference to data, not a value of data. (Of course, the pointer itself is passed by value, but you're expected to dereference it.) Since the pointer is writable, you can write to it and the caller will see the effect of such a write upon its return.

My question is : if such a method gets called very frequently can we expect a significant overhead of pointers copy?

For copying sizeof(int*) ? No. For a function call, though, there might be a significant overhead, especially if the call is performed through the PLT. In a multi-threaded environment, this can indirectly introduce other overheads.

In the following scenario, does it mean we copy the value in the memory address pointed by passed numPtr to the memory address pointed by num2Ptr?

Yes.

If yes, then it is the same as passing by value?

No. Passing of integer by value is faster primarily because passing by value will not involve reading memory and will be done through registers.

Pointer copy is very low cost, but makes little sense for primitive data types where the size of the pointer is as large or larger then the stored data.

For your second question you are coping the value and would be the same as coping by value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM