-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
강의 마지막부분 IntArray 클래스 구현 .
20.07.11 22:22 작성 조회수 267
0
아래 코드는 제가 직접 구현해본 IntArray 클래스 입니다.
------------------------------------------------
#include<iostream>
#include<initializer_list>
class intarray {
private:
int m_length = 0;
int* m_data = nullptr;
public:
intarray(unsigned length)
:m_length(length)
{
m_data = new int[m_length];
}
intarray(const std::initializer_list<int>& list)
:intarray(list.size())
{
int count = 0;
for (auto& element : list)
{
m_data[count] = element;
++count;
}
}
intarray& operator=(const intarray& arr)
{
if (this == &arr)
{
return *this;
}
this->m_length = arr.m_length;
delete[] m_data;
if (arr.m_data != nullptr)
{
m_data = new int[m_length];
for (int i = 0; i < m_length; i++)
{
m_data[i] = arr.m_data[i];
}
}
else { m_data = nullptr; }
}
void reset() {
if (m_data != nullptr) delete[] m_data;
m_data = nullptr;
m_length = 0;
}
intarray& resize(const int &size)
{
if (size > m_length)
{
intarray temp(0);
temp = (*this);
reset();
m_length = size;
m_data = new int[m_length];
for (int i = 0; i < temp.m_length; i++)
{
(*this).m_data[i] = temp.m_data[i];
}
for (int i = temp.m_length; i < m_length; i++)
{
m_data[i] = 0;
}
}
else if (size < m_length)
{
int* temp = new int[size];
for (int i = 0; i < size; i++)
{
temp[i] = m_data[i];
}
reset();
m_length = size;
m_data = new int[m_length];
for (int i = 0; i < size; i++)
{
m_data[i] = temp[i];
}
delete[] temp;
}
return *this;
}
intarray& insertbefore(const int& value, const int& ix)
{
resize(m_length + 1);
int* arr = new int[m_length - 1 - ix];
for (int i = 0; i < m_length - ix - 1; i++)
{
arr[i] = m_data[i + ix];
}
m_data[ix] = value;
for (int i = 0; i < m_length - ix; i++)
{
m_data[ix + 1 + i] = arr[i];
}
delete[]arr;
return *this;
}
intarray& remove(const int& ix)
{
intarray temp(m_length - 1);
for (int i = 0; i < ix; i++)
{
temp.m_data[i] = m_data[i];
}
for (int i = ix + 1; i < m_length; i++)
{
temp.m_data[i - 1] = m_data[i];
}
resize(--m_length);
(*this) = temp;
return *this;
}
intarray& pushback(const int& value)
{
resize(++m_length);
m_data[m_length - 1] = value;
return *this;
}
friend std::ostream& operator <<(std::ostream& out, const intarray& arr)
{
for (int t = 0; t < arr.m_length; t++)
{
out << arr.m_data[t] << " ";
}
return out;
}
~intarray() { if (m_data != nullptr) { delete[] m_data; } }
};
int main() {
intarray myarr{ 1,3,5,7,9 };
std::cout << myarr << std::endl;
/*std::cout << myarr.resize(4) << std::endl; //for resize test.
std::cout << myarr.resize(6) << std::endl;*/ //for resize test.
std::cout<<myarr.insertbefore(10, 1)<<std::endl;
std::cout<<myarr.remove(3) << std::endl;
std::cout<<myarr.pushback(13) << std::endl;
return 0;
}
---------------------------------------------
작동은 제대로 됩니다만 , Debug 모드와 Release 모드 실행시 차이가 있습니다 .
위와같은 릴리즈모드에시 실행시에는 아무 에러창없이 실행되구요
위와같이 디버그모드서 실행시 디버그 에러가 납니다.
다만 , 실행결과에는 차이가없구요.
왜그런건가요?
답변을 작성해보세요.
2
![TaeChoon Pakr님의 프로필](https://cdn.inflearn.com/public/main/profile/default_profile.png?w=48)
TaeChoon Pakr
질문자2020.07.12
아하하 ... 바보같은 실수를 했네요!! resize(m_length+1)을 해야 resize내에서 새로이 동적할당을 받아오는데 ,( m_length++) 로 하니 그냥 나와서 Heap Corruption 이 발생 ..
긴 질문이였는데 정성스레 답변해주셔서 감사합니다!!
1
답변 2