引用和指针有区别吗?
#include <stdio.h>
void Fun1(int &n)
{
n = 100;
}
void Fun2(int *n)
{
*n = 100;
}
int main()
{
int a = 10;
int b = 10;
Fun1(a);
Fun2(&b);
return 0;
}
int a = 10;
0023175E C7 45 F8 0A 00 00 00 mov dword ptr [a],0Ah
int b = 10;
00231765 C7 45 EC 0A 00 00 00 mov dword ptr [b],0Ah
Fun1(a);
0023176C 8D 45 F8 lea eax,[a]
0023176F 50 push eax
00231770 E8 30 F9 FF FF call Fun1 (02310A5h)
00231775 83 C4 04 add esp,4
Fun2(&b);
00231778 8D 45 EC lea eax,[b]
0023177B 50 push eax
0023177C E8 3D F9 FF FF call Fun2 (02310BEh)
00231781 83 C4 04 add esp,4
void Fun1(int &n)
{
002316C0 55 push ebp
002316C1 8B EC mov ebp,esp
002316C3 81 EC C0 00 00 00 sub esp,0C0h
002316C9 53 push ebx
002316CA 56 push esi
002316CB 57 push edi
002316CC 8D BD 40 FF FF FF lea edi,[ebp-0C0h]
002316D2 B9 30 00 00 00 mov ecx,30h
002316D7 B8 CC CC CC CC mov eax,0CCCCCCCCh
002316DC F3 AB rep stos dword ptr es:[edi]
n = 100;
002316DE 8B 45 08 mov eax,dword ptr [n]
002316E1 C7 00 64 00 00 00 mov dword ptr [eax],64h
}
002316E7 5F pop edi
}
002316E8 5E pop esi
002316E9 5B pop ebx
002316EA 8B E5 mov esp,ebp
002316EC 5D pop ebp
002316ED C3 ret
void Fun2(int *n)
{
00231700 55 push ebp
00231701 8B EC mov ebp,esp
00231703 81 EC C0 00 00 00 sub esp,0C0h
00231709 53 push ebx
0023170A 56 push esi
0023170B 57 push edi
0023170C 8D BD 40 FF FF FF lea edi,[ebp-0C0h]
00231712 B9 30 00 00 00 mov ecx,30h
00231717 B8 CC CC CC CC mov eax,0CCCCCCCCh
0023171C F3 AB rep stos dword ptr es:[edi]
*n = 100;
0023171E 8B 45 08 mov eax,dword ptr [n]
00231721 C7 00 64 00 00 00 mov dword ptr [eax],64h
}
00231727 5F pop edi
00231728 5E pop esi
00231729 5B pop ebx
0023172A 8B E5 mov esp,ebp
0023172C 5D pop ebp
0023172D C3 ret
#include <stdio.h>
void Fun1(int *&n)
{
static int test = 0;
n = &test;
}
void Fun2(int **n)
{
static int test = 0;
*n = &test;
}
int main()
{
int *a = NULL;
int *b = NULL;
Fun1(a);
Fun2(&b);
return 0;
}
int *a = NULL;
00A3175E C7 45 F8 00 00 00 00 mov dword ptr [a],0
int *b = NULL;
00A31765 C7 45 EC 00 00 00 00 mov dword ptr [b],0
Fun1(a);
00A3176C 8D 45 F8 lea eax,[a]
00A3176F 50 push eax
00A31770 E8 83 FB FF FF call Fun1 (0A312F8h)
00A31775 83 C4 04 add esp,4
Fun2(&b);
00A31778 8D 45 EC lea eax,[b]
00A3177B 50 push eax
00A3177C E8 31 FB FF FF call Fun2 (0A312B2h)
00A31781 83 C4 04 add esp,4
void Fun1(int *&n)
{
00A316C0 55 push ebp
00A316C1 8B EC mov ebp,esp
00A316C3 81 EC C0 00 00 00 sub esp,0C0h
00A316C9 53 push ebx
00A316CA 56 push esi
00A316CB 57 push edi
00A316CC 8D BD 40 FF FF FF lea edi,[ebp-0C0h]
00A316D2 B9 30 00 00 00 mov ecx,30h
00A316D7 B8 CC CC CC CC mov eax,0CCCCCCCCh
00A316DC F3 AB rep stos dword ptr es:[edi]
static int test = 0;
n = &test;
00A316DE 8B 45 08 mov eax,dword ptr [n]
00A316E1 C7 00 38 91 A3 00 mov dword ptr [eax],offset test (0A39138h)
}
00A316E7 5F pop edi
}
00A316E8 5E pop esi
00A316E9 5B pop ebx
00A316EA 8B E5 mov esp,ebp
00A316EC 5D pop ebp
00A316ED C3 ret
void Fun2(int **n)
{
00A31700 55 push ebp
00A31701 8B EC mov ebp,esp
00A31703 81 EC C0 00 00 00 sub esp,0C0h
00A31709 53 push ebx
00A3170A 56 push esi
00A3170B 57 push edi
00A3170C 8D BD 40 FF FF FF lea edi,[ebp-0C0h]
00A31712 B9 30 00 00 00 mov ecx,30h
00A31717 B8 CC CC CC CC mov eax,0CCCCCCCCh
00A3171C F3 AB rep stos dword ptr es:[edi]
static int test = 0;
*n = &test;
00A3171E 8B 45 08 mov eax,dword ptr [n]
00A31721 C7 00 3C 91 A3 00 mov dword ptr [eax],offset test (0A3913Ch)
}
00A31727 5F pop edi
00A31728 5E pop esi
00A31729 5B pop ebx
00A3172A 8B E5 mov esp,ebp
00A3172C 5D pop ebp
00A3172D C3 ret
在汇编语言层,引用和指针没有区别
普通变量的引用就是一级指针
一级指针的引用就是二级指针
要修改普通变量,要用一级指针(普通引用)
要修改一级指针,要用二级指针(引用一级指针)
|