C pointers vs direct member access for structs

前端 未结 9 2777
抹茶落季
抹茶落季 2020-12-16 17:59

Say I have a struct like the following ...

typedef struct {
  int WheelCount;
  double MaxSpeed;
} Vehicle;

... and I have a global variabl

9条回答
  •  忘掉有多难
    2020-12-16 18:24

    The first one should be faster since it doesn't require pointer dereferencing. Then again thats true for x86 based systems, not sure for others.

    on x86 the first one would translate to something like this

    mov eax, [address of MyGlobal.MaxSpeed]
    

    and the second one would be something like this

    mov ebx, [address of pMyGlobal] 
    mov eax, [ebx+sizeof(int)] 
    

提交回复
热议问题