简体   繁体   English

指针函数参数在C ++中的复制和性能

[英]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? 在以下情况下,这是否意味着我们将传递的numPtr指向的内存地址中的值复制到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. 指针通常只是32位或64位数量。 So copying a pointer just involves copying a 32-bit or 64-bit quantity, which is very cheap on most platforms. 因此,复制指针仅涉及复制32位或64位数量,这在大多数平台上非常便宜。 However, copying an int directly is also very cheap, so in this case, using pointers probably doesn't bring you much benefit. 但是,直接复制一个int也很便宜,因此在这种情况下,使用指针可能不会带来很多好处。

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? 这是否意味着我们将传递的numPtr指向的内存地址中的值复制到num2Ptr指向的内存地址中?

In theory, yes. 从理论上讲,是的。 However, num2Ptr = NULL , so your code is likely to cause a segmentation fault. 但是, num2Ptr = NULL ,因此您的代码很可能会导致分段错误。

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. 将一个指针复制到另一个指针的操作非常小-只是32位或64位分配,具体取决于您的目标平台。 Such an operation isn't free, but it's very fast. 这样的操作不是免费的,但速度很快。

memory address pointed by passed numPtr to the memory address pointed by num2Ptr? 传递的numPtr指向的内存地址是num2Ptr指向的内存地址?

Yes, the code you show copies the value in the memory referenced by numPtr to the memory referenced by numPtr2 . 是的,您显示的代码numPtr引用的内存中的值复制到numPtr引用的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; 当然,在您的示例中,指针分别引用了地址0x00000019和0x00000000,因此,除非您知道那里有可读的内存,否则在读取源值时会崩溃。 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. 请注意,您的注释( ////num2Ptr points to the same value in the memory pointed by numPtr argument. )不正确。

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*) ? 对于复制sizeof(int*) No. For a function call, though, there might be a significant overhead, especially if the call is performed through the PLT. 不会。但是,对于函数调用,可能会有相当大的开销,特别是如果通过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? 在以下情况下,是否意味着我们将传递的numPtr指向的内存地址中的值复制到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. 对于第二个问题,您正在应对价值,这与按价值应对相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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