-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(c++): destructor and memory leak example
- Loading branch information
1 parent
be175ea
commit 8c2a69d
Showing
6 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
**/output/** | ||
**/output/** | ||
/backup/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |