gpt4 book ai didi

C 如何避免多次取消引用同一个变量?

转载 作者:行者123 更新时间:2023-12-02 07:44:45 26 4
gpt4 key购买 nike

我有一个结构数组,我有一些函数将使用这些结构的多个成员。我想避免在每一行中取消引用。我认为会有一些方法可以在某个内存位置声明一个变量......像这样:

someStruct &myStruct = arrayOfStructs[i];
myStruct.x = foo+bar*myStruct.y*myStruct.w;
//Instead of myStruct->x = foo+bar*myStruct->y*myStruct->w;
//It would/should even be possible to access the members in a similar way:
int &x = &myStruct.x;
x = x+4*y+2*z;
//This should avoid overhead of dereferencing the pointer, and offsetting to the member
//by just accessing that particular address of memory as though it was where the variable
//had always been.

这段示例代码可能有助于解释:

#define NUM_BIGSTRUCTS 10000

typedef struct {
int a,b,c;
float d,e,f;
} bigStruct;

bigStruct* arrayOfStructs;

void foo() {
for(int i=0; i<NUM_BIGSTRUCTS; i++) {
bigStruct* temp = arrayOfStructs[i];
temp->f = (temp->d+temp->e)*((float)temp->a+temp->e);
//more similar, with conditionals, etc...
//actually I've got nested loops, and a very very large array
//so any gains per inner loop would decrease my number of instructions exponentially

//So, if I could declare a bigStruct and set its address to the location of a bigStruct in the array
//then I could avoid a dereference every time I access a member of that bigStruct
//Leaving just the member access overhead... which could be handled in a similar manner
//if possible, and when appropriate
}
}

int main(int argx, char** argv) {
arrayOfStructs = g_new0(bigStruct,NUM_BIGSTRUCTS); //Allocate and 0 memory for simplicity

foo();

return 0;
}

我从来没有在 SO 上取得过巨大的成功,所以希望我解释了我想做的事情。顺便说一句,我正在使用 C99,而且考虑到 c 的低级性质,我相信这是可能的。

[编辑]看起来我正在寻找来自 C++ 的“引用”,但对于 C。即便如此,它们只允许赋值一次(初始化),这在我的示例中不起作用。我决定依靠编译器来优化对同一内存部分的多次访问。

谢谢,詹姆斯· Newman

最佳答案

您正在尝试编译器优化比手动优化做得更好的事情。此外,C99 没有您尝试在示例中定义它们的方式(特别是 C++ 取消引用声明)的这些引用构造,如果您也变得非常庞大和深入,我建议您重新考虑您的算法。如果你试图引入一些临时变量和更多的内存来做引用,你会让你的生活更加艰难。

例如,如果您查看:

struct some_struct {
int a;
struct {
float f;
double d;
} s;
};

struct some_struct array[10000];

int process1(struct some_struct *r) {
#define R (*r)
R.a+= 1;
R.s.f = R.s.f/2;
R.s.d = ( R.s.d + R.s.f ) * 2;
}

int process2(struct some_struct *r) {
r->a+= 1;
r->s.f = r->s.f/2;
r->s.d = ( r->s.d + r->s.f ) * 2;
}

int doit() {
int i;
for (i = 0; i < sizeof(array)/sizeof(struct some_struct); i++ ) {
struct some_struct *r = &array[i]; /* via reference */
process1(r);
process2(r);
}
}

process1 和 process2 在 x86_64 平台上使用 gcc -O2 生成相同的汇编输出:

        .file   "foo.c"
.text
.p2align 4,,15
.globl process1
.type process1, @function
process1:
.LFB11:
.cfi_startproc
movss .LC0(%rip), %xmm0
addl $1, (%rdi)
mulss 8(%rdi), %xmm0
movss %xmm0, 8(%rdi)
unpcklps %xmm0, %xmm0
cvtps2pd %xmm0, %xmm0
addsd 16(%rdi), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 16(%rdi)
ret
.cfi_endproc
.LFE11:
.size process1, .-process1
.p2align 4,,15
.globl process2
.type process2, @function
process2:
.LFB12:
.cfi_startproc
movss .LC0(%rip), %xmm0
addl $1, (%rdi)
mulss 8(%rdi), %xmm0
movss %xmm0, 8(%rdi)
unpcklps %xmm0, %xmm0
cvtps2pd %xmm0, %xmm0
addsd 16(%rdi), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 16(%rdi)
ret
.cfi_endproc
.LFE12:
.size process2, .-process2
.p2align 4,,15
.globl doit
.type doit, @function
doit:
.LFB13:
.cfi_startproc
xorl %edx, %edx

movss .LC0(%rip), %xmm2
.p2align 4,,10
.p2align 3
.L4:
leaq (%rdx,%rdx,2), %rax
addq $1, %rdx
leaq array(,%rax,8), %rax
movss 8(%rax), %xmm1
addl $2, (%rax)
mulss %xmm2, %xmm1
cmpq $10000, %rdx
unpcklps %xmm1, %xmm1
cvtps2pd %xmm1, %xmm0
mulss %xmm2, %xmm1
addsd 16(%rax), %xmm0
movss %xmm1, 8(%rax)
unpcklps %xmm1, %xmm1
cvtps2pd %xmm1, %xmm1
addsd %xmm0, %xmm0
addsd %xmm1, %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 16(%rax)
jne .L4
rep
ret
.cfi_endproc
.LFE13:
.size doit, .-doit
.comm array,240000,32
.section .rodata.cst4,"aM",@progbits,4
.align 4
.LC0:
.long 1056964608
.ident "GCC: (GNU) 4.6.1"
.section .note.GNU-stack,"",@progbits

关于C 如何避免多次取消引用同一个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7853243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com