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
class WrapperA{ | |
public: | |
A *p; | |
WrapperA(){ | |
p = new A; | |
} | |
~WrapperA(){ | |
delete p; | |
} | |
}; |
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
int mem_demo_static(){ | |
A a; | |
return a.func(); | |
// A's destructor is called automatically | |
} | |
int mem_demo_dynamic(){ | |
A* a = new A; | |
return a->func(); | |
// A's destructor is not called, memory leak! |
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
class A { | |
public: | |
A(int n){ | |
array = new int[n]; | |
} | |
~A(){ | |
delete array; | |
} | |
private: | |
int *array; |
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
int demo3(){ | |
A *a = new A; | |
int ra = a -> func(); | |
if(ra == 2) { | |
try { | |
B *b = new B; | |
} | |
catch (...) | |
{ | |
delete a; |
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
int demo2(){ | |
A *a = new A; | |
int ra = a -> func(); | |
if(ra == 2) { | |
B *b = new B; | |
int rb = b->func(); | |
delete a; | |
delete b; | |
return rb; | |
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
int demo1(){ | |
A *a = new A(); | |
int r = a -> func(); | |
delete a; | |
return r; | |
} |
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
g++ main.o -L. -lmy_math -o a.out -Wl,-rpath,/home/cpp_tutorial/static_library | |
./a.out | |
0.5 |
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
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/cpp_tutorial/static_library | |
./a.out | |
0.5 |
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
./a.out | |
./a.out: error while loading shared libraries: libmy_math.so: cannot open shared object file: No such file or directory |
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
rm libmy_math.a | |
./a.out | |
0.5 |
NewerOlder