-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCArr.cpp
More file actions
55 lines (46 loc) · 1.16 KB
/
Copy pathCArr.cpp
File metadata and controls
55 lines (46 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "CArr.h"
#include<assert.h>
CArr::CArr()
: m_pInt(nullptr)
, m_iCount(0)
, m_iMaxCount(0)
{
m_pInt = new int[2]; //int자료형 2개만큼 할당하겠다. (malloc 대신에 쓰이는 것)
}
CArr::~CArr()
{
delete[] m_pInt; //[] 붙인 이유는 int[2] 배열 형태를 받았기 때문에
}
void CArr::push_back(int _iData)
{
if (m_iMaxCount <= m_iCount)
{
reallocate(m_iMaxCount * 2);
}
m_pInt[m_iCount++] = _iData;
}
void CArr::reallocate(int _iDataCount)
{
//현재 최대 수용량 보다 더 작은 수치로 확장하려는 경우
if (m_iMaxCount >= _iDataCount)
{
assert(nullptr);
}
// 1. 리사이즈 시킬 개수만큼 동적 할당 한다.
int* pNew = new int[_iDataCount];
// 2. 기존 공간에 있던 데이터들을 새로 할당한 공간으로 복사시킨다.
for (int i = 0; i < m_iCount; ++i)
{
pNew[i] = m_pInt[i];
}
// 3. 기존 공간은 메모리 해제
delete[] m_pInt;
// 4. 배열이 새로 할당한 공간을 가르킨다.
m_pInt = pNew;
// 5. maxcount 변경점 적용
m_iMaxCount = _iDataCount;
}
int& CArr::operator[](int idx)
{
return m_pInt[idx];
}