数组取值


数组取值
一维数组:
void HelloWord()
{

    int x =1;  ebp-4
    int y =2;  ebp-8
    int arr1[5] = {1,2,3,4,5};
    printf("%d",arr1[x+y]);  

}

反汇编代码:



00401498   mov         dword ptr [ebp-4],1
0040149F   mov         dword ptr [ebp-8],2
004014A6   mov         dword ptr [ebp-1Ch],1
004014AD   mov         dword ptr [ebp-18h],2
004014B4   mov         dword ptr [ebp-14h],3
004014BB   mov         dword ptr [ebp-10h],4
004014C2   mov         dword ptr [ebp-0Ch],5
004014C9   mov         eax,dword ptr [ebp-4]
004014CC   add         eax,dword ptr [ebp-8]
004014CF   mov         ecx,dword ptr [ebp+eax*4-1Ch]



一维数组取值方式:
[ebp+(1+2)*4(int为4个字节)-1Ch]


多维数组
void HelloWord()
{


    
    int arr[3][4]={
        {1,2,3,5},
        {2,3,4,5},
        {9,2,3,1}
    };

    printf("%d",arr[0][2]);
    
}

反汇编代码:

00401498   mov         dword ptr [ebp-30h],1
0040149F   mov         dword ptr [ebp-2Ch],2
004014A6   mov         dword ptr [ebp-28h],3
004014AD   mov         dword ptr [ebp-24h],5
004014B4   mov         dword ptr [ebp-20h],2
004014BB   mov         dword ptr [ebp-1Ch],3
004014C2   mov         dword ptr [ebp-18h],4
004014C9   mov         dword ptr [ebp-14h],5
004014D0   mov         dword ptr [ebp-10h],9
004014D7   mov         dword ptr [ebp-0Ch],2
004014DE   mov         dword ptr [ebp-8],3
004014E5   mov         dword ptr [ebp-4],1
004014EC   mov         eax,dword ptr [ebp-28h]


一维数组取值方式:



arr[0][2]
arr[0*12+2]

例子2:
    int arr[5][12] = {                    
                        
        {1,2,1,4,5,6,7,8,9,1,2,3},                  
                        
        {11,12,11,14,15,16,17,18,19,11,12,13},                
                        
        {21,22,21,24,25,26,27,28,29,21,22,23},                
                        
        {31,32,31,34,35,36,37,38,39,31,32,33},                
                        
        {41,42,41,44,45,46,47,48,49,41,42,43}                
                        
    }   
                
编译器分配内存空间
int arr[5*12] = {.................................}                    
假设取值:arr[3][7]
编译器是如何找到这个数据的
	// 5 * 12 * 4 = 240
	//
	//(3 * 12 + 7) = 43 * 4 = 172 
	//240-172 = 68 = 44  ebp-44
arr[3*12+7] 

 
 例子3:
 
int arr[5][4][3] = {                                
                                
    {{1,2,3},{4,5,6},{7,8,9},{11,12,13}},                        
                                
    {{11,12,13},{14,15,16},{17,18,19},{111,112,113}},                        //1    arr[1][2][1] = 18
                                
    {{21,22,23},{24,25,26},{27,28,29},{211,212,213}},                        //2    arr[1*4*3 + 2*3 +1]
                                
    {{31,32,33},{34,35,36},{37,38,39},{311,312,313}},                        //3    4 4 2
                                
    {{41,42,43},{44,45,46},{47,48,49},{411,412,413}}                        //4    arr[3][3][1]
                                
};                                
arr[3*4*3 + 3*3 + 1]


例子4:
编译器如何分配空间:                                
                                
int arr[5*4*3] = {.....................};                                
                                
                                
如果获取第2个班级、第3组、第2个人的年龄.                            
                                
arr[1][2][1]                            
                                
编译器如何计算:                                
                                
arr[1*4*3 + 2*3 + 1]                                


int arr[n][m][k][w][r];

arr[2][3][4][2][2]    

arr[2*m*k*w*r+3*k*w*r+4*w*r+2*r+2]








上一篇
指针一
下一篇
数组越界
发表评论 / Comment

用心评论~