请问C++的引用是怎么实现的,使用引用的效率是否能快于使用指针呢?

yyxgs 2016-09-20 10:39:22
请问C++的引用是怎么实现的,使用引用的效率是否能超过使用指针呢?
...全文
489 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 21 楼 paschen 的回复:
《C++反汇编与逆向分析技术揭秘》 这本书上有对其深入分析
下载了,感谢推荐!!
paschen 版主 2016-09-21
  • 打赏
  • 举报
回复
《C++反汇编与逆向分析技术揭秘》 这本书上有对其深入分析
paschen 版主 2016-09-21
  • 打赏
  • 举报
回复
引用 18 楼 yyxgs的回复:
[quote=引用 17 楼 paschen 的回复:] [quote=引用 16 楼 yyxgs 的回复:] [quote=引用 15 楼 paschen 的回复:] [quote=引用 12 楼 yyxgs 的回复:] [quote=引用 9 楼 qq423399099 的回复:] 看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
哈哈,请问VS2015中怎么查看C++编译后release版反汇编代码呢?[/quote] Alt + 8[/quote] Ctrl+F7(编译)后,我在编辑器里按Alt + F8,请问怎么没有反应呢?[/quote] 我说的是8,不是F8[/quote] 哦哦,我试了,还是没出来...会不会2015已经改变了...[/quote] 程序要断下来....
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 17 楼 paschen 的回复:
[quote=引用 16 楼 yyxgs 的回复:] [quote=引用 15 楼 paschen 的回复:] [quote=引用 12 楼 yyxgs 的回复:] [quote=引用 9 楼 qq423399099 的回复:] 看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
哈哈,请问VS2015中怎么查看C++编译后release版反汇编代码呢?[/quote] Alt + 8[/quote] Ctrl+F7(编译)后,我在编辑器里按Alt + F8,请问怎么没有反应呢?[/quote] 我说的是8,不是F8[/quote] 我知道了,在调试窗口的时候,按Ctrl + Alt + D,感谢大神帮助!
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 17 楼 paschen 的回复:
[quote=引用 16 楼 yyxgs 的回复:] [quote=引用 15 楼 paschen 的回复:] [quote=引用 12 楼 yyxgs 的回复:] [quote=引用 9 楼 qq423399099 的回复:] 看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
哈哈,请问VS2015中怎么查看C++编译后release版反汇编代码呢?[/quote] Alt + 8[/quote] Ctrl+F7(编译)后,我在编辑器里按Alt + F8,请问怎么没有反应呢?[/quote] 我说的是8,不是F8[/quote] 哦哦,我试了,还是没出来...会不会2015已经改变了...
paschen 版主 2016-09-21
  • 打赏
  • 举报
回复
引用 16 楼 yyxgs 的回复:
[quote=引用 15 楼 paschen 的回复:] [quote=引用 12 楼 yyxgs 的回复:] [quote=引用 9 楼 qq423399099 的回复:] 看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
哈哈,请问VS2015中怎么查看C++编译后release版反汇编代码呢?[/quote] Alt + 8[/quote] Ctrl+F7(编译)后,我在编辑器里按Alt + F8,请问怎么没有反应呢?[/quote] 我说的是8,不是F8
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 15 楼 paschen 的回复:
[quote=引用 12 楼 yyxgs 的回复:] [quote=引用 9 楼 qq423399099 的回复:] 看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
哈哈,请问VS2015中怎么查看C++编译后release版反汇编代码呢?[/quote] Alt + 8[/quote] Ctrl+F7(编译)后,我在编辑器里按Alt + F8,请问怎么没有反应呢?
paschen 版主 2016-09-21
  • 打赏
  • 举报
回复
引用 12 楼 yyxgs 的回复:
[quote=引用 9 楼 qq423399099 的回复:] 看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
哈哈,请问VS2015中怎么查看C++编译后release版反汇编代码呢?[/quote] Alt + 8
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 11 楼 paschen 的回复:
[quote=引用 3 楼 yyxgs 的回复:] [quote=引用 1 楼 paschen 的回复:] 引用编译器也是按指针方式实现的,不能超过指针
谢谢,《C++ primer》说建议C++里使用引用,我还以为引用能超过指针的效率来着。[/quote] 引用比指针安全[/quote] 重点。
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 10 楼 u010370871 的回复:
引用本身就是指针的另一种方式,有不一样的地方就是引用必须有初始值,指针无所谓,多看看effective c++系列,会给你一个满意的答复的
多谢推荐!
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 9 楼 qq423399099 的回复:
看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
哈哈,请问VS2015中怎么查看C++编译后release版反汇编代码呢?
paschen 版主 2016-09-21
  • 打赏
  • 举报
回复
引用 3 楼 yyxgs 的回复:
[quote=引用 1 楼 paschen 的回复:] 引用编译器也是按指针方式实现的,不能超过指针
谢谢,《C++ primer》说建议C++里使用引用,我还以为引用能超过指针的效率来着。[/quote] 引用比指针安全
张小飞Official 2016-09-21
  • 打赏
  • 举报
回复
引用本身就是指针的另一种方式,有不一样的地方就是引用必须有初始值,指针无所谓,多看看effective c++系列,会给你一个满意的答复的
小灸舞 2016-09-21
  • 打赏
  • 举报
回复
看这段c++代码,一个引用,一个指针

struct _test
{
	int n;
	int m;
	int j;
};
int test1(_test * n);
int test2(_test & n);

void main()
{
	_test t;
	test1(&t);
	test2(t);
}

int test1(_test * n)
{
	n->j = 10;
	n->m = 20;
	n->n = 30;

	return 0;
}
int test2(_test & n)
{
	n.j = 10;
	n.m = 20;
	n.n = 30;

	return 0;
}

然后看看,他们的编译后对应的汇编代码,在vc中编译的时候用/FAs选项输出,用release模式 TITLE G:\test\test\main.cpp .386P include listing.inc if @Version gt 510 .model FLAT else _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' _DATA ENDS CONST SEGMENT DWORD USE32 PUBLIC 'CONST' CONST ENDS _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' _BSS ENDS _TLS SEGMENT DWORD USE32 PUBLIC 'TLS' _TLS ENDS ; COMDAT _main _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT PARA USE32 PUBLIC 'CODE' _TEXT ENDS FLAT GROUP _DATA, CONST, _BSS ASSUME CS: FLAT, DS: FLAT, SS: FLAT endif PUBLIC ?test1@@YAHPAU_test@@@Z ; test1 PUBLIC ?test2@@YAHAAU_test@@@Z ; test2 PUBLIC _main ; COMDAT _main _TEXT SEGMENT _t$ = -12 _main PROC NEAR ; COMDAT ; File G:\test\test\main.cpp ; Line 12 00000 83 ec 0c sub esp, 12 ; 0000000cH ; Line 14 00003 8d 44 24 00 lea eax, DWORD PTR _t$[esp+12] 00007 50 push eax 00008 e8 00 00 00 00 call ?test1@@YAHPAU_test@@@Z ; test1 ; Line 15 0000d 8d 4c 24 04 lea ecx, DWORD PTR _t$[esp+16] 00011 51 push ecx 00012 e8 00 00 00 00 call ?test2@@YAHAAU_test@@@Z ; test2 ; Line 16 00017 83 c4 14 add esp, 20 ; 00000014H 0001a c3 ret 0 _main ENDP _TEXT ENDS ; COMDAT ?test1@@YAHPAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test1@@YAHPAU_test@@@Z PROC NEAR ; test1, COMDAT ; Line 20 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 21 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 22 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 24 00018 33 c0 xor eax, eax ; Line 25 0001a c3 ret 0 ?test1@@YAHPAU_test@@@Z ENDP ; test1 _TEXT ENDS ; COMDAT ?test2@@YAHAAU_test@@@Z _TEXT SEGMENT _n$ = 8 ?test2@@YAHAAU_test@@@Z PROC NEAR ; test2, COMDAT ; Line 28 00000 8b 44 24 04 mov eax, DWORD PTR _n$[esp-4] 00004 c7 40 08 0a 00 00 00 mov DWORD PTR [eax+8], 10 ; 0000000aH ; Line 29 0000b c7 40 04 14 00 00 00 mov DWORD PTR [eax+4], 20 ; 00000014H ; Line 30 00012 c7 00 1e 00 00 00 mov DWORD PTR [eax], 30 ; 0000001eH ; Line 32 00018 33 c0 xor eax, eax ; Line 33 0001a c3 ret 0 ?test2@@YAHAAU_test@@@Z ENDP ; test2 _TEXT ENDS END 结果就是:他妈的就是一回事。没有谁比谁快
The_best_man 2016-09-21
  • 打赏
  • 举报
回复
指针侧重的是传址,引用是传值,传地址的效率大于传值效率
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 7 楼 cherish_ff 的回复:
个人观点:指针和引用在传递过程中都是地址,而引用的优势在于他的绑定,即引用之后地址不在改变而指针则不同。在传参过程中,采用引用则传递指针,且含义确定,无疑提高了效率。
谢谢!
ESMGAL 2016-09-21
  • 打赏
  • 举报
回复
个人观点:指针和引用在传递过程中都是地址,而引用的优势在于他的绑定,即引用之后地址不在改变而指针则不同。在传参过程中,采用引用则传递指针,且含义确定,无疑提高了效率。
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 5 楼 cherish_ff 的回复:
不能,引用与被引用的地址一样。实质就是一个指针。
哦哦,既然如此,那C++引入引用的有什么作用呢?请问它的优势在哪?
ESMGAL 2016-09-21
  • 打赏
  • 举报
回复
不能,引用与被引用的地址一样。实质就是一个指针。
yyxgs 2016-09-21
  • 打赏
  • 举报
回复
引用 2 楼 mxway 的回复:
在vs系列中,指针是程序员行为,引用是编译器行为。其它编译器不知道怎么实现引用的,使用vs编译的程序反汇编后引用与指针没有区别。
谢谢,《C++ primer》说建议C++里使用引用,我还以为引用能超过指针的效率来着。
加载更多回复(3)

64,671

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧