Created
July 1, 2014 12:09
-
-
Save funatsufumiya/bac63c6e02c7b0fd721a to your computer and use it in GitHub Desktop.
【C++】ポインターのスコープをチェックする【修正済】 ref: http://qiita.com/atmarksharp/items/db1afac2ba4c64f88e4d
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
#include <iostream> | |
#include <string> | |
#define bold "\e[1;30m" | |
#define red "\e[0;31m" | |
#define green "\e[0;32m" | |
#define cyan "\e[0;36m" | |
#define reset "\e[0m" | |
using namespace std; | |
// Testクラス | |
class Test{ | |
public: | |
Test() | |
{ | |
cout << red << "<< Test: created >>" << reset << endl; | |
} | |
~Test(){ | |
cout << green << "<< ~Test: deleted >>" << green << endl; | |
} | |
}; | |
// ログを出力 | |
void log(string s){ | |
cout << s << endl; | |
} | |
// ポインターがNULLならNULL、それ以外ならOKと表示 | |
// 出力: 変数名(スコープLv): [OK or NULL] | |
void exists(void* t,string name,string scope){ | |
if(t){ | |
log(name + "(" + scope + "): OK"); | |
}else{ | |
log(name + "(" + scope + "): NULL"); | |
} | |
} | |
//=========================================== | |
// テストを実行 | |
void scope_check1(){ | |
cout << cyan << "[Scope Check 1]" << reset << endl << endl; | |
// コンパイルエラーが起こらないようにする | |
void* a(NULL); | |
void* b(NULL); | |
void* c(NULL); | |
exists(a,"a","0: before"); // NULL | |
exists(b,"b","0: before"); // NULL | |
exists(c,"c","0: before"); // NULL | |
log(""); | |
{ | |
void* a(new Test); | |
exists(a,"a","1: before"); // OK | |
log(""); | |
{ | |
void* b(new Test); | |
exists(a,"a","2: before"); // OK | |
exists(b,"b","2: before"); // OK | |
log(""); | |
{ | |
void* c(new Test); | |
exists(a,"a","3"); // OK | |
exists(b,"b","3"); // OK | |
exists(c,"c","3"); // OK | |
log(""); | |
} | |
// ローカル変数"c"はスコープを外れている | |
exists(a,"a","2: after"); // OK | |
exists(b,"b","2: after"); // OK | |
exists(c,"c","2: after"); // NULL | |
log(""); | |
} | |
// ローカル変数"b,c"はスコープを外れている | |
exists(a,"a","1: after"); // OK | |
exists(b,"b","1: after"); // NULL | |
exists(c,"c","1: after"); // NULL | |
log(""); | |
} | |
// ローカル変数"a,b,c"はスコープを外れている | |
exists(a,"a","0: after"); // NULL | |
exists(b,"b","0: after"); // NULL | |
exists(c,"c","0: after"); // NULL | |
} | |
int main(){ | |
log(""); | |
{ | |
scope_check1(); | |
} | |
log(""); | |
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
#include <iostream> | |
#include <string> | |
#include <memory> | |
#define bold "\e[1;30m" | |
#define red "\e[0;31m" | |
#define green "\e[0;32m" | |
#define cyan "\e[0;36m" | |
#define reset "\e[0m" | |
using namespace std; | |
class Test{ | |
public: | |
Test() | |
{ | |
cout << red << "<< Test: created >>" << reset << endl; | |
} | |
~Test(){ | |
cout << green << "<< ~Test: deleted >>" << green << endl; | |
} | |
}; | |
void log(string s){ | |
cout << s << endl; | |
} | |
void exists(void* t,string name,string scope){ | |
if(t){ | |
log(name + "(" + scope + "): OK"); | |
}else{ | |
log(name + "(" + scope + "): NULL"); | |
} | |
} | |
//=========================================== | |
class ScopeCheck2{ | |
public: | |
void* a; | |
void* b; | |
void* c; | |
ScopeCheck2(): | |
a(NULL), | |
b(NULL), | |
c(NULL) | |
{ | |
cout << cyan << "[Scope Check 2]" << reset << endl << endl; | |
exists(a,"a","0: before"); // NULL | |
exists(b,"b","0: before"); // NULL | |
exists(c,"c","0: before"); // NULL | |
log(""); | |
{ | |
void* a(new Test); | |
exists(a,"a","1: before"); // OK | |
log(""); | |
this->a = a; // <<< COPY REFERENCE >>> | |
{ | |
void* b(new Test); | |
exists(a,"a","2: before"); // OK | |
exists(b,"b","2: before"); // OK | |
log(""); | |
this->b = b; // <<< COPY REFERENCE >>> | |
{ | |
void* c(new Test); | |
exists(a,"a","3"); // OK | |
exists(b,"b","3"); // OK | |
exists(c,"c","3"); // OK | |
log(""); | |
this->c = c; // <<< COPY REFERENCE >>> | |
} | |
exists(a,"a","2: after"); // OK | |
exists(b,"b","2: after"); // OK | |
exists(c,"c","2: after"); // NULL -> OK | |
log(""); | |
} | |
exists(a,"a","1: after"); // OK | |
exists(b,"b","1: after"); // NULL -> OK | |
exists(c,"c","1: after"); // NULL -> OK | |
log(""); | |
} | |
exists(a,"a","0: after"); // NULL -> OK | |
exists(b,"b","0: after"); // NULL -> OK | |
exists(c,"c","0: after"); // NULL -> OK | |
} | |
}; | |
int main(){ | |
log(""); | |
{ | |
ScopeCheck2 sct; | |
} | |
log(""); | |
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
#include <iostream> | |
#include <string> | |
#include <memory> | |
#define bold "\e[1;30m" | |
#define red "\e[0;31m" | |
#define green "\e[0;32m" | |
#define cyan "\e[0;36m" | |
#define reset "\e[0m" | |
using namespace std; | |
class Test{ | |
public: | |
Test() | |
{ | |
cout << red << "<< Test: created >>" << reset << endl; | |
} | |
~Test(){ | |
cout << green << "<< ~Test: deleted >>" << reset << endl; | |
} | |
}; | |
void log(string s){ | |
cout << s << endl; | |
} | |
void exists(shared_ptr<Test> t,string name,string scope){ | |
if(t){ | |
log(name + "(" + scope + "): OK"); | |
}else{ | |
log(name + "(" + scope + "): NULL"); | |
} | |
} | |
//=========================================== | |
class ScopeCheck3{ | |
public: | |
shared_ptr<Test> a; | |
shared_ptr<Test> b; | |
shared_ptr<Test> c; | |
ScopeCheck3(): | |
a(NULL), | |
b(NULL), | |
c(NULL) | |
{ | |
cout << cyan << "[Scope Check 3]" << reset << endl << endl; | |
exists(a,"a","0: before"); // NULL | |
exists(b,"b","0: before"); // NULL | |
exists(c,"c","0: before"); // NULL | |
log(""); | |
{ | |
shared_ptr<Test> a(new Test); | |
exists(a,"a","1: before"); // OK | |
log(""); | |
this->a = a; // <<< COPY REFERENCE >>> | |
{ | |
shared_ptr<Test> b(new Test); | |
exists(a,"a","2: before"); // OK | |
exists(b,"b","2: before"); // OK | |
log(""); | |
this->b = b; // <<< COPY REFERENCE >>> | |
{ | |
shared_ptr<Test> c(new Test); | |
exists(a,"a","3"); // OK | |
exists(b,"b","3"); // OK | |
exists(c,"c","3"); // OK | |
log(""); | |
this->c = c; // <<< COPY REFERENCE >>> | |
} | |
exists(a,"a","2: after"); // OK | |
exists(b,"b","2: after"); // OK | |
exists(c,"c","2: after"); // NULL -> OK | |
log(""); | |
} | |
exists(a,"a","1: after"); // OK | |
exists(b,"b","1: after"); // NULL -> OK | |
exists(c,"c","1: after"); // NULL -> OK | |
log(""); | |
} | |
exists(a,"a","0: after"); // NULL -> OK | |
exists(b,"b","0: after"); // NULL -> OK | |
exists(c,"c","0: after"); // NULL -> OK | |
} | |
}; | |
int main(){ | |
log(""); | |
{ | |
ScopeCheck3 sct; | |
} | |
log(""); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment