本质上没有区别,在汇编代码下,内容基本一致,证明:
代码 1(基于引用)
#include "stdio.h"
#include "string.h"
typedef struct test__{
int a;
int b;
}T_test;
void ModifyStructvalue(T_test& test)
{
test.a = 10;
test.b = 20;
printf("In Function test:%d,%d\n",test.a,test.b);
}
int main()
{
T_test test = {1,2};
printf("test:%d,%d\n",test.a,test.b);
ModifyStructvalue(test);
printf("test:%d,%d\n",test.a,test.b);
}
对应的汇编程序为:
(gdb) disassemble main
Dump of assembler code for function main:
0x000000000040059e <+0>: push %rbp
0x000000000040059f <+1>: mov %rsp,%rbp
0x00000000004005a2 <+4>: sub $0x10,%rsp
0x00000000004005a6 <+8>: movl $0x1,-0x10(%rbp)
0x00000000004005ad <+15>: movl $0x2,-0xc(%rbp)
0x00000000004005b4 <+22>: mov -0xc(%rbp),%edx
0x00000000004005b7 <+25>: mov -0x10(%rbp),%eax
0x00000000004005ba <+28>: mov %eax,%esi
0x00000000004005bc <+30>: mov $0x4006a8,%edi
0x00000000004005c1 <+35>: mov $0x0,%eax
0x00000000004005c6 <+40>: callq 0x400440 <printf@plt>
0x00000000004005cb <+45>: lea -0x10(%rbp),%rax
0x00000000004005cf <+49>: mov %rax,%rdi
0x00000000004005d2 <+52>: callq 0x40055d <_Z17ModifyStructvalueR6test__>
0x00000000004005d7 <+57>: mov -0xc(%rbp),%edx
0x00000000004005da <+60>: mov -0x10(%rbp),%eax
0x00000000004005dd <+63>: mov %eax,%esi
0x00000000004005df <+65>: mov $0x4006a8,%edi
0x00000000004005e4 <+70>: mov $0x0,%eax
0x00000000004005e9 <+75>: callq 0x400440 <printf@plt>
0x00000000004005ee <+80>: mov $0x0,%eax
0x00000000004005f3 <+85>: leaveq
0x00000000004005f4 <+86>: retq
End of assembler dump.
(gdb) disassemble ModifyStructvalue
Dump of assembler code for function _Z17ModifyStructvalueR6test__:
0x000000000040055d <+0>: push %rbp
0x000000000040055e <+1>: mov %rsp,%rbp
0x0000000000400561 <+4>: sub $0x10,%rsp
0x0000000000400565 <+8>: mov %rdi,-0x8(%rbp)
0x0000000000400569 <+12>: mov -0x8(%rbp),%rax
0x000000000040056d <+16>: movl $0xa,(%rax)
0x0000000000400573 <+22>: mov -0x8(%rbp),%rax
0x0000000000400577 <+26>: movl $0x14,0x4(%rax)
0x000000000040057e <+33>: mov -0x8(%rbp),%rax
0x0000000000400582 <+37>: mov 0x4(%rax),%edx
0x0000000000400585 <+40>: mov -0x8(%rbp),%rax
0x0000000000400589 <+44>: mov (%rax),%eax
0x000000000040058b <+46>: mov %eax,%esi
0x000000000040058d <+48>: mov $0x400690,%edi
0x0000000000400592 <+53>: mov $0x0,%eax
0x0000000000400597 <+58>: callq 0x400440 <printf@plt>
0x000000000040059c <+63>: leaveq
0x000000000040059d <+64>: retq
End of assembler dump.
(gdb)
代码2(基于指针)
#include "stdio.h"
#include "string.h"
typedef struct test__{
int a;
int b;
}T_test;
void ModifyStructvalue(T_test* test)
{
test->a = 10;
test->b = 20;
printf("In Function test:%d,%d\n",test->a,test->b);
}
int main()
{
T_test test = {1,2};
printf("test:%d,%d\n",test.a,test.b);
ModifyStructvalue(&test);
printf("test:%d,%d\n",test.a,test.b);
}
对应的汇编程序为:
(gdb) disassemble main
Dump of assembler code for function main:
0x000000000040059e <+0>: push %rbp
0x000000000040059f <+1>: mov %rsp,%rbp
0x00000000004005a2 <+4>: sub $0x10,%rsp
0x00000000004005a6 <+8>: movl $0x1,-0x10(%rbp)
0x00000000004005ad <+15>: movl $0x2,-0xc(%rbp)
0x00000000004005b4 <+22>: mov -0xc(%rbp),%edx
0x00000000004005b7 <+25>: mov -0x10(%rbp),%eax
0x00000000004005ba <+28>: mov %eax,%esi
0x00000000004005bc <+30>: mov $0x4006a8,%edi
0x00000000004005c1 <+35>: mov $0x0,%eax
0x00000000004005c6 <+40>: callq 0x400440 <printf@plt>
0x00000000004005cb <+45>: lea -0x10(%rbp),%rax
0x00000000004005cf <+49>: mov %rax,%rdi
0x00000000004005d2 <+52>: callq 0x40055d <_Z17ModifyStructvalueP6test__>
0x00000000004005d7 <+57>: mov -0xc(%rbp),%edx
0x00000000004005da <+60>: mov -0x10(%rbp),%eax
0x00000000004005dd <+63>: mov %eax,%esi
0x00000000004005df <+65>: mov $0x4006a8,%edi
0x00000000004005e4 <+70>: mov $0x0,%eax
0x00000000004005e9 <+75>: callq 0x400440 <printf@plt>
0x00000000004005ee <+80>: mov $0x0,%eax
0x00000000004005f3 <+85>: leaveq
0x00000000004005f4 <+86>: retq
End of assembler dump.
(gdb) disassemble ModifyStructvalue
Dump of assembler code for function _Z17ModifyStructvalueP6test__:
0x000000000040055d <+0>: push %rbp
0x000000000040055e <+1>: mov %rsp,%rbp
0x0000000000400561 <+4>: sub $0x10,%rsp
0x0000000000400565 <+8>: mov %rdi,-0x8(%rbp)
0x0000000000400569 <+12>: mov -0x8(%rbp),%rax
0x000000000040056d <+16>: movl $0xa,(%rax)
0x0000000000400573 <+22>: mov -0x8(%rbp),%rax
0x0000000000400577 <+26>: movl $0x14,0x4(%rax)
0x000000000040057e <+33>: mov -0x8(%rbp),%rax
0x0000000000400582 <+37>: mov 0x4(%rax),%edx
0x0000000000400585 <+40>: mov -0x8(%rbp),%rax
0x0000000000400589 <+44>: mov (%rax),%eax
0x000000000040058b <+46>: mov %eax,%esi
0x000000000040058d <+48>: mov $0x400690,%edi
0x0000000000400592 <+53>: mov $0x0,%eax
0x0000000000400597 <+58>: callq 0x400440 <printf@plt>
0x000000000040059c <+63>: leaveq
0x000000000040059d <+64>: retq
End of assembler dump.
(gdb)
内容基本一致。