Skip to content

Commit

Permalink
feat(c++): destructor and memory leak example
Browse files Browse the repository at this point in the history
  • Loading branch information
bugoverdose committed Jun 19, 2024
1 parent be175ea commit 8c2a69d
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**/output/**
**/output/**
/backup/**
64 changes: 64 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"array": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp"
}
}
37 changes: 37 additions & 0 deletions c++/cpp-destructor-and-memory-leak/ex1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>

class One {
public:
One() {};
~One() {
std::cout<<"Destroying One"<<std::endl;
};
};

class Two {
private:
One* one;
public:
Two() {
one = new One();
};
~Two() {
// delete one;
std::cout<<"Destroying Two"<<std::endl;
};
};

void doSomething() {
Two two;
// ...
return;
// [출력] Destroying Two
}

int main(void)
{
std::cout<<"Before doSomething"<<std::endl;
doSomething();
std::cout<<"After doSomething"<<std::endl;
return 0;
}
38 changes: 38 additions & 0 deletions c++/cpp-destructor-and-memory-leak/ex2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>

class One {
public:
One() {};
~One() {
std::cout<<"Destroying One"<<std::endl;
};
};

class Two {
private:
One* one;
public:
Two() {
one = new One();
};
~Two() {
delete one;
std::cout<<"Destroying Two"<<std::endl;
};
};

void doSomething() {
Two two;
// ...
return;
// [출력] Destroying One
// [출력] Destroying Two
}

int main(void)
{
std::cout<<"Before doSomething"<<std::endl;
doSomething();
std::cout<<"After doSomething"<<std::endl;
return 0;
}
50 changes: 50 additions & 0 deletions c++/cpp-destructor-and-memory-leak/ex3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <cstring>

class First {
private:
char* str1;
public:
First(char* str) {
this->str1=new char[strlen(str)+1];
strcpy(this->str1, str);
};
~First() {
std::cout<<"Destroying First"<<std::endl;
std::cout<<"Destroying '"<<str1<<"'"<<std::endl;
delete []str1;
};
};

class Second : public First {
private:
char* str2;
public:
Second(char* str1, char* str2) : First(str1) {
this->str2=new char[strlen(str2)+1];
strcpy(this->str2, str2);
};
~Second() {
std::cout<<"Destroying Second"<<std::endl;
std::cout<<"Destroying '"<<str2<<"'"<<std::endl;
delete []str2;
};
};

int main(void)
{
Second* second = new Second("one", "two");
delete second;
// [출력] Destroying Second
// [출력] Destroying 'two'
// [출력] Destroying First
// [출력] Destroying 'one'

std::cout<<"==================="<<std::endl;

First* first = new Second("one", "two");
delete first;
// [출력] Destroying First
// [출력] Destroying 'one'
return 0;
}
43 changes: 43 additions & 0 deletions c++/cpp-destructor-and-memory-leak/ex4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <cstring>

class First {
private:
char* str1;
public:
First(char* str) {
this->str1=new char[strlen(str)+1];
strcpy(this->str1, str);
};
virtual ~First() {
std::cout<<"Destroying First"<<std::endl;
std::cout<<"Destroying '"<<str1<<"'"<<std::endl;
delete []str1;
};
};

class Second : public First {
private:
char* str2;
public:
Second(char* str1, char* str2) : First(str1) {
this->str2=new char[strlen(str2)+1];
strcpy(this->str2, str2);
};
~Second() {
std::cout<<"Destroying Second"<<std::endl;
std::cout<<"Destroying '"<<str2<<"'"<<std::endl;
delete []str2;
};
};

int main(void)
{
First* first = new Second("one", "two");
delete first;
// [출력] Destroying Second
// [출력] Destroying 'two'
// [출력] Destroying First
// [출력] Destroying 'one'
return 0;
}

0 comments on commit 8c2a69d

Please sign in to comment.