프로그래밍/C++
#35. 다차원 배열
코딩하는상후니
2022. 7. 19. 22:07
*다차원 배열
int D[2][5] = { {4,2,3,4,1,}, {1,2,5,5,3,} };
for (int y = 0; y < 2; ++y)
{
for (int x = 0; x < 5; ++x)
{
cout << D[y][x] << " ";
}
cout << endl;
}
*메모리 구조는 어떻게 잡혀있을까?? ( int D[2][5] = { {4,2,3,4,1,}, {1,2,5,5,3,} }; )
mov dword ptr [D], 4
mov dword ptr [ebp-28h], 2
mov dword ptr [ebp-24h], 3
...
=> 즉, int(4byte) 씩 메모리에 '일렬로' 데이터가 '복사' 되어지는 걸 볼 수 있다.
short D2[10] = { 4,2,3,4,1,1,2,5,5,3 };
for (int y = 0; y < 2; ++y)
{
for (int x = 0; x < 5; ++x)
{
int idx = (y * 5) + x;
short num = D2[idx];
cout << num << " ";
}
cout << endl;
}

=> eax, dword ptr [ ebp-84h] => 계산된 idx 가져옴.
mov, cx, word ptr D2[eax*2] => cx 에 D2[idx] 데이터 복사
( short 형이라 eax*2 인 주소를 복사하는 것을 볼 수 있음.)
mov word ptr[ebp-90h], cx => num 에 word ( 2byte ) 만큼 데이터 복사
*함수의 매개변수론 어떻게 쓰일까??
int ArrPrint(int a[][10])
{
int Ret = 0;
for (int y = 0; y < 10; ++y)
for (int x = 0; x < 10; ++x)
if(a[y][x]) ++Ret;
return Ret;
}
int main()
{
int Arr[10][10] = { 1, };
int Result = ArrPrint(Arr);
return 0;
}
*Arr 크기는 얼마인가 ??
*Result = 100 이 아니다. 그 이유는 ??