简体   繁体   English

通过指针访问结构成员

[英]Access to structure member by pointer

I have a structure that I pass to a function as constant pointer, my question is the following: There is a difference between those two implementations of function updatedFields: 我有一个结构,我作为常量指针传递给一个函数,我的问题如下:函数updatedFields的这两个实现之间有区别:

typedef struct
{
    int spec[100];
    int spec1[200];
    int spec2[200];
    int spec3[500];
    int spec4[100];
    int spec5[700];
    float value[100];
    char desc[1000]:
}t_product;



void updateFields_1(t_product const* context)
{

    int i,buffer[1500];
    int * pt_int;

    pt_int = (int*)context->spec1;      
    for(i = 0; i < 200; i++)
    {
        buffer[i] = pt_int[i];
    }

    pt_int = (int*)context->spec3;        
    for(i = 0; i < 500; i++)
    {
        buffer[i] = pt_int[i];
    }

    ...
}

void updateFields_2(t_product const* context)
{

    int i,buffer[1500];

    for(i = 0; i < 200; i++)
    {
        buffer[i] = context->spec1[i];
    }

    for(i = 0; i < 500; i++)
    {
        buffer[i] = context->spec3[i];
    }

    ...
}

int main(void)
{
    t_product prod;

    /* Initialisation of the structure */
    ...

    updateField(&prod);

}

I mean, there is any advantages to use pointer to member of a struct (pointer to the arrays) instead of accessing directly to the member of struture. 我的意思是,使用指向结构成员的指针(指向数组的指针)而不是直接访问struture成员有任何好处。

It's probably a dumb question but I don't know if the access of a struct member "costs" more operations. 这可能是一个愚蠢的问题,但我不知道结构成员的访问是否“花费”更多的操作。

It won't ever cost more in your case. 在您的情况下,它不会花费更多。 Even without optimization. 即使没有优化。 Actually your pt_int example is likely to be slightly worse if you don't enable optimizations. 实际上,如果不启用优化,则pt_int示例可能会稍微恶化一些。

This is because context->spec3[i] isn't dereferencing more pointers than pt_int[i] . 这是因为context->spec3[i]没有解除引用比pt_int[i]更多的指针。 pt_int[i] is just a pointer plus an offset, so the access can be written as @(ptr_int + 4*i) . pt_int[i]只是一个指针和一个偏移量,所以访问可以写成@(ptr_int + 4*i) In context->spec3[i] , it could look like there is one more pointer dereferenced, but it isn't the case. context->spec3[i] ,看起来有一个指针被解除引用,但情况并非如此。 spec3 isn't a value in context , it's just an offset from context. spec3不是context的值,它只是与上下文的偏移。 The address you access will therefore be @(context + 2000 + 4*i) . 因此,您访问的地址为@(context + 2000 + 4*i) There is only one pointer access. 只有一个指针访问。

Now you can wonder if @(context + 2000 + 4*i) costs more than @(ptr_int + 4*i) . 现在你可以想知道@(context + 2000 + 4*i)成本是否超过@(ptr_int + 4*i) It doesn't, because most architectures, including x86, AMD64 and ARM (that is, 100% of personal devices), have instructions to do accesses with constant offsets. 它没有,因为大多数架构,包括x86,AMD64和ARM(即100%的个人设备),都有使用常量偏移进行访问的指令。 Also, the difference can disappear at soon as you enable trivial optimizations, because context + 2000 can be converted to a single context_2000 (but compilers won't actually do that, since it can only worsen performances). 此外,当您启用简单的优化时,差异可能很快消失,因为context + 2000可以转换为单个context_2000 (但编译器实际上不会这样做,因为它只会恶化性能)。

它确实花费更多(它必须在每次迭代时取消引用原始指针),但成本可能很小,并且中途不错的编译器将为您进行优化。

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

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