Format : T A[N]
Array size : Sizeof(T) x N Bytes
Contiguously allocated region
Does not do bound checking
Two-Dimensional Nested Array
- Format : T A[R][C]
- All levels in one contiguous block
- Row-Major ordering
- Elements are contiguous
- Array size : R x C x Sizeof(T) Bytes
- How to calculate address
-> A[i][j] : A + (i x C x K) + (j x K), where K is required bytes
Multi-Level Array
- Format : T *A[N], where T is data type, N is length
- First level in one contiguous block
-> Each element in first level points another subarray
-> Address of each level array is not contiguous- Each element is a pointer (8 bytes)
Nested array VS Multi-level array
Nested Array | Multi-level Array |
---|---|
needs consecutive memory space | only needs the size of memory that pointer points |
access once to get a value | access twice to get a value |
- 변수이므로 스택영역에 할당된다.
따라서, 메인 함수가 return되게 된다면 사라지게 된다.
Must have an address that is a multiple of k
Memory Alignment를 통해 Memory access 횟수를 줄일 수 있다.
-> Each structure has alignment requirement Kmax, where Kmax is the largest alignment of any element.
Unaligned Data
After alignment
-> 3 bytes, 4 bytes are padding data.
-> 낭비되는 memory를 최소화 하기 위해 Padding data 또한 최소화하는 것이 목적!
선언 순서를 바꿈으로써 memory 효율성을 증가시킬 수 있다.
Example