ãã¹ã1ïŒ
int main() { vector<int> v; v.reserve(2); assert(v.capacity() == 2); v[0]; v[0] = 1; v[1] = 2; cout << v[0] << endl; v.reserve(100); cout << v[0] << endl; return 0; }
ãã®ã³ãŒãã泚ææ·±ã調ã¹ãŠãããã€ã®åé¡ãç¹å®ã§ããŸããïŒ
ãã¡ãããå¿ èŠãªããããŒã¯ãã¹ãŠå«ãŸããŠããŸãïŒ iostreamãvectorãcassert ïŒã
ãã®ã³ãŒãã®ãããã§ãã·ã§ãã«ããŒãžã§ã³ã«ã€ããŠèª¬æããåã«ããœãŒã¹ã³ãŒãã®éçåæã®ããã®æåãªãŠãŒãã£ãªãã£ãåæããŸãã 以äžã¯ãäžèšã®ãŠãŒãã£ãªãã£ã®çµæã§ãã
ã©ãã ïŒ ãªã
Cppcheck ïŒ ãªã
ã°ã©ãŠãã£ãã ïŒ ãªã
g ++ïŒ-Wallãã©ã°ä»ãïŒ ïŒ ãªã
äžèŠãããšããã»ã©æªããªãã®ã§ããããããããšãäœãæªãããšã¯ãªãã®ã§ããããïŒ Sutterã§ãããèšããªãã§ãã ããïŒãšãããã圌ã®èæžãC ++ã®æ°ããè€éãªåé¡ããœãªã¥ãŒã·ã§ã³ãçšãã40ã®æ°ããããºã«ã®äŸãããäŸãåŒçšããŠããŸã ïŒã
mainïŒïŒé¢æ°ã®æ¬äœã®æåã®è¡ã§ãæŽæ°ã§æ§æããããã¯ãã«vã宣èšããŸãã 次ã«ã2ã€ã®èŠçŽ ã®å ŽæãäºçŽããŸãã 次ã®è¡ã«é²ãåã«ãC ++æšæºãèŠãŠãã ããã 圌女ã¯vector :: reserveã¡ãœããã«ã€ããŠäœãæããŠãããŸããïŒ ïŒèŠåïŒç¡æ翻蚳ïŒ
ã
void reserve(size_type n);
ãµã€ãºã®èšç»çãªå€æŽã«ã€ããŠéç¥ããå Žåã«ãã£ãŠã¯ã¡ã¢ãªå²ãåœãŠãå¶åŸ¡ããŸãã reserveïŒïŒã®åŸã容éïŒïŒã¯ ãã¡ã¢ãªã®åå²ãåœãŠã®å Žåã«reserveïŒïŒã«æž¡ãããåŒæ°ä»¥äžã§ãã ãã以å€ã®å Žåã 容éã®åã®å€ïŒïŒãšçãããªããŸãã 容éïŒïŒã®çŸåšã®å€ãreserveïŒïŒåŒæ°ã®å€ããå°ããå Žåã«ã®ã¿ãã¡ã¢ãªã®ååé
ãçºçããŸãã ã³ã³ããã®ãµã€ãºã¯å€æŽããããææªã®å Žåãç·åœ¢ã©ã³ã¿ã€ã ãå¿
èŠã§ãã åŒæ°å€ãmax_sizeïŒïŒãã倧ããå Žåãlong_erroräŸå€ãã¹ããŒããŸãã åé
åžãããå Žåãã³ã³ããèŠçŽ ãåç
§ãããã¹ãŠã®åç
§ããã€ã³ã¿ãããã³ã€ãã¬ãŒã¿ãç¡å¹ã«ããŸãã
ããã§ååã§ãããå ã«é²ã¿ãŸãããã æååã¢ãµãŒãïŒv.capacityïŒïŒ== 2ïŒ; ãããããæªããæåã®ãªã¶ãŒãïŒïŒã¯å®¹éã®å¢å ãä¿èšŒããŸãããã®å Žåã assertã¯ã³ãŒãã®è¿œå è¡ã«ãããŸããïŒãããã°ã©ããŒãã¯ãåæäœã®åŸã«ãã¿ããã«ã¢ãµãŒããä¿¡ãããããã§ãã·ã§ãã«ãªããŒã³ãšèŠãªããŸãïŒã 第äºã«ãæšæºèªäœããåŠãã ããã«ã reserveïŒïŒã¯æå®ãããåŒæ°ãããã³ã³ãã容éãå¢ããããšãã§ãããããSatterã¯assertïŒv.capacityïŒïŒ> = 2ïŒãæšå¥šããŠããŸãã å®å šã«åé€ããããšããå§ãããŸãã
ããã«æªãã ã ãã¯ãã«<T> ::æŒç®å[]æŒç®åã¯ãç¯å²ãã§ãã¯ãå®è¡ã§ããŸãããå¿ é ã§ã¯ãããŸããã æšæºã§ã¯ããã«ã€ããŠã¯äœãèšåãããŠããªããããæšæºã©ã€ãã©ãªã®éçºè ã¯ããã®ãããªãã§ãã¯ãè¿œå ãããããè¡ããªããã¹ãŠã®æš©å©ãæã£ãŠããŸã ã ãã¹ãŠã®çç±ã¯å¹çã§ããããã¹ãã«ã¯æéãããããŸãããã®ãããªãã§ãã¯ãå€æ°è¡ããšãããã°ã©ã ã®æéã®ããªãã®éšåãå¿ èŠã«ãªããŸãã ããããããã¯ãå¹æçãªããã°ã©ã ãVasyaã®ä¿¡é Œã§ããããã°ã©ã ãããåªããŠãããšããæå³ã§ã¯ãããŸããã è¡v [0]ã眮ãæããŠã¿ãŠãã ããã v.atïŒ0ïŒ; ããã°ã©ã ã¯äŸå€ã䜿çšããŠé£è¡ããŸãã ã»ãšãã©ã®å Žåãä¿¡é Œã§ããã³ãŒããå¿ èŠã§ãã vector <T> :: atã¯ãã€ã³ããã¯ã¹å€ã®ç¯å²ãã§ãã¯ãå®è¡ããŸãã ãã®ãããªç¶æ³ã§ã®ã¿ã§ã¯ãªãããã®æ¹æ³ã䜿çšããããšã匷ããå§ãããŸãã
è¡v [0] = 1; v [1] = 2; ããªãããŸãæ©èœããŠããã®ã§ãäŸãã³ã³ãã€ã«ããããšã§çŽåŸã§ããŸãã ãã ãã ãµã€ãº/ãµã€ãºå€æŽãšäºçŽ/容éã®éããèŠããŠããå¿ èŠããããŸãã resizeã¯ãã³ã³ããå ã®èŠçŽ ã®æ°ããã³ã³ããã®æ«å°Ÿããè¿œå ãŸãã¯åé€ããããšã«ãããæå®ãããåŒæ°å€ã«å€æŽããŸãã äºçŽã¡ãœããã«ã€ããŠã¯æšæºããåŠã³ãŸããããæå®ãããæ°ã®èŠçŽ ãïŒå°ãªããšãïŒå容ã§ããããã«å éšãããã¡ã®ãµã€ãºãå¢ããããšãè¿œå ããŸããã ã æŒç®å[] ïŒãŸãã¯atã¡ãœããïŒãå®å šã«äœ¿çšã§ããã®ã¯ãã³ã³ããã«å®éã«å«ãŸããŠãããã€ãŸãå®éã«ãµã€ãºãèæ ®ãããŠããèŠçŽ ãå€æŽããå Žåã®ã¿ã§ãã ã ãã®ãããéç解æãŠãŒãã£ãªãã£ãæ確ã§ã¯ãããŸããã ããããåé¡ã¯é倧ã§ãã Sutterãä»ã®å€ãã®ããã¯ããæ°ã«å ¥ãã®C ++ã§ã®ã³ãŒãã£ã³ã°ã®åŸ®åŠãªãã€ã³ãã«ã€ããŠæ¬ã«æžããŠããŸãã äžèŠãããšãèšèªã®æ§æã®åé¡ãšç¥èã解決ããèœåã¯éçºã«ã¯ååãããããã«æãããããããŸããã ããããããªããšç§ã¯ãç§ãã¡ãèªåèªèº«ãã ãŸããŠããããšãç¥ã£ãŠããŸããåé¡ãç¹å®ããããã«ãã³ãŒãã®åè¡ã«äœäººã®äººãã³ãŒããæ¿å ¥ããŸãããïŒ 3æ¥ç®ã«æ¢ã«å®è¡ããããããã°ã«èããããªãã£ã人ã¯äœäººããŸãããïŒ ã§ããããSutter and MyersïŒããã³ä»ã®å€ãã®ïŒã®é人ã«ããæ¬ãå匷ãããå匷ãããããã®ãé¢åã§ã¯ãªãããã®ããã°ã©ãã³ã°ã¹ãã«ãæã€äººã ãå«ãããã®ãããªããšã«åºæã§ã¯ãªã人ã ãããŸãã
ããã§ããäŸ1ã§ã¯ãæåŸã®è«çãšã©ãŒã cout << v [0]ã®åºåãã©ãæããŸããã äºçŽåŸïŒ100ïŒ ïŒ ããã§ãããããããŒãïŒ äºçŽã®åã«v [0]ã«ãŒã以å€ã®å€ãæå®ããããšã«æ³šæããŠãã ããã ãåé¡ãã¯åã³ããã¯ã¢ããæ©èœã«ãããŸãã
ããã§ã¯ãäžè¬çãªå Žåã®åé¡ã¯äœã§ããïŒ v.reserveïŒ2ïŒã®å Žåã«ãããäŒãããŠãŒãã£ãªãã£ãå¿ èŠã§ãããšããäºå®ã v [0] = 1; v.resizeïŒ2ïŒã䜿çšããããšããå§ãããŸãã v [0] = 1; ãŸãã¯v.reserveïŒ2ïŒ; v.push_backïŒ1ïŒ ïŒ ãããŠãç§ãã¡ãæ€èšãããŠãŒãã£ãªãã£ã¯é»ã£ãŠé»ã£ãŠããŸããã äžæ£ãªå¢çããã§ãã¯ãããŠãŒãã£ãªãã£ãå¿ èŠã§ããvã®å Žåã¯[0]ã v.atïŒ0ïŒã䜿çšããããšããå§ãããŸãã ãããŠãç§ãã¡ã®ã¬ãã¥ãŒããããŠãŒãã£ãªãã£ã¯åã³éãã«çŽ æŽãããã£ãã§ãã ãããã£ãŠã埮åŠãªç¶æ³ã§ã¯ãé埳ã¯RATSãCppcheckãªã©ã®ãŠãŒãã£ãªãã£ã«äŸåãããããã«ã¯Grauditã«ãäŸåããŸãã æé«ã¬ãã«ã®èŠåããªã³ã«ãªã£ãŠããã³ã³ãã€ã©ãŒã§ãããæã«ã¯åå·çãªãšã³ã³ãŒããŒã®ç®ãéããŠèŠãã¹ãã§ãã
ãŸãããŠãŒãã£ãªãã£ãããžã«åã£ãŠä»£ããããšã¯ç¡é§ã§ã¯ãããŸãããéçºè ãããã«åãçµãã ã®ã¯äœã®çç±ããããŸããã
ãã¹ã2ïŒ
ããã«é²ãã§ãã³ãŒãã®ãšã©ãŒã®éãå¢ãããŠãã ããã æ°ããprettyFormaté¢æ°ã䜿çšããåãã³ãŒããæ€èšããŠãã ããïŒSatterã®äŸïŒã 圌女ã¯çªå·ãšãããã¡ãååŸããsprintfã䜿çšããŠçµæã®ãããã¡ã«ãã®çªå·ãæ¿å ¥ããŸãã
void prettyFormat(int i, char* buf) { sprintf(buf, "%4d", i); } int main() { vector<int> v; v.reserve(2); //.... char buf[5]; prettyFormat(10, buf); return 0; }
ã芧ã®ãšããã prettyFormatãè¿œå ãããæååchar buf [10] ãè¿œå ãããŸããã åã®äŸã®ã³ãŒãã¯ããå¿ èŠãããŸãããããã®ãŸãŸã«ããŠãããŸããããŠãŒãã£ãªãã£ã®1ã€ãäœããèŠããããããŸããã çµæã¯æ¬¡ã®ãšããã§ãã
RATSïŒ è¡char buf [5]; ã é«ïŒåºå®ãµã€ãºã®ããŒã«ã«ãããã¡ãŒãã¹ã¿ãã¯ã«å²ãåœãŠãããæåé åãå®å šã«äœ¿çšãããããã«ç¹ã«æ³šæããå¿ èŠããããŸãããããã¯ãããã¡ãŒãªãŒããŒãããŒæ»æã®äž»ãªã¿ãŒã²ããã§ãã ãæåé åã䜿çšããå Žåã¯ç¡åãªã®ã§ã泚æããããšããå§ãããŸããããã¡ãªãŒããŒãããŒæ»æã
Cppcheck ïŒ ãªã
ã°ã©ãŠãã£ãã ïŒ ãªã
g ++ïŒ-Wallãªãã·ã§ã³ä»ãïŒ ïŒ ãªã
ãããã¡ãªãŒããŒãããŒã«ã€ããŠå€ãã®ããšãèšãããŠããã®ã§ãç°¡åã«æ¹å€ããŸãã éç解æã®ããŒã«ã¯ããããã¡ãªãŒããŒãããŒã®å¯èœæ§ã®ããã±ãŒã¹ãèŠã€ããããšã®ã¿ãç®çãšããŠãããšããå°è±¡ãæã£ãŠããŸãã ãã ãããã®ãããªå Žåã§ãïŒCppcheckãGrauditãªã©ïŒããããã®ããã€ãã¯ãµã€ã¬ã³ãã§ãã ç§ã¯ã³ã³ãã€ã©ãéé£ãããåé¡ãèŠã€ããããšãç§ã«æ¢ããããŸããã§ããããã»ãšãã©ã®å Žåããã®ãããªããšã®ããã«ç¹å¥ã«èšèšããããŠãŒãã£ãªãã£ã¯ãšãŠãéãã§ãã ãã®ãããªç¶æ³ã§ã¯ã sprintfã®ä»£æ¿ã䜿çšããããã«ããŠãŒãã£ãªãã£ããèŠåãšæšå¥šäºé ãååŸããå¿ èŠããããŸãïŒä»ã®å¯èœãªæ©èœã«ã€ããŠïŒã 代ããã®ç¶æ³ã¯Satterã§èªãå¿ èŠããããŸãïŒ std :: stringstreamãstd :: strstreamãboost :: lexical_castã«ã€ããŠè©±ããŸãããåŸè ã¯std :: stringstreamã«åºã¥ããŠããŸãããããã¡ãªãŒããŒãããŒããïŒã
ãã¹ã3ïŒ
åæåãããŠããªããã€ã³ã¿ãŒãprettyFormaté¢æ°ã«è¿œå ããŸãã
int* prettyFormat(int i, char* buf) { sprintf(buf, "%4d", i); int* a; return a; } int main() { ...
ã¡ã€ã³ã³ãŒãã§ã¯ã以åã®ãã¹ããšæ¯èŒããŠå€æŽãããŠããŸããã ããã¯æ·±å»ãªãã¹ãã®ããã«æãããããããŸãããã䜿çšããããŠãŒãã£ãªãã£ã«é¢ããå€ãã®èå³æ·±ãæ å ±ãæäŸããŸãã ç¹ã«ãCppcheckã«ã€ããŠã¯ã³ã³ãã€ã©ã®èŠåãè€è£œããªããšèšãããŸããããããã«ãããããã«ãããã¯ãã©ãã§ãã ãããŠãä»ã®ãŠãŒãã£ãªãã£ã¯åã«åé¡ãèªèããªãããå®éã«ã³ã³ãã€ã©ã®èŠåãè€è£œããŸããã 以äžããã®çµæã§ãã
RATS ïŒ åã®ãã¹ããšåãïŒæååchar buf [5];ã«ã€ããŠè©±ããŠããïŒ
Cppcheck ïŒ è¡int * a; throwsïŒerrorïŒåæåãããŠããªãå€æ°ïŒaã
çµæ§ã§ãã...
g ++ïŒ- Wall ãªãã·ã§ã³ã䜿çšïŒ ïŒ é¢æ° 'int * prettyFormatïŒintãchar *ïŒ'ïŒ
èŠåïŒ 'a'ã¯ãã®é¢æ°ã§åæåãããã«äœ¿çšãããŸãã
ãªãã¿ã®ãããã®ã¯ãããŸãããïŒ Cppcheckãæçµçã«åºããã®ãšåãèŠåã
Graudit㯠äžè¬çã«æ²é»ããŠããŸãã
ãã¹ã4ïŒ
ãŠãŒã¢ã¢ãè¿œå ãã prettyFormaté¢æ°ã次ã®ããã«å€æŽããŸãã
int* prettyFormat(int i, char* buf) { sprintf(buf, "%4d", i); int* a; fopen("filename", "r"); char buf2[5]; strcpy(buf2, buf); return a; }
Fopenãè¿œå ãããŸãããããã¯ãããŒã«ã«ã®buf2ãããã¡ãŒãè¿œå ãããããšãç¥ããŸãããåŒæ°ãšããŠåä¿¡ããbufãstrcpyã䜿çšããŠã³ããŒãããŸãã 誰ããããäžãããèŠãŠã¿ãŸãããã
RATSïŒ æååchar buf2 [5]; char buf [10]åºå®ãµã€ãºã®æåãããã¡ã®äœ¿çšã«é¢ãã泚æã«ã€ããŠã®åãèŠåã
ãããŠæåŸã«ïŒãé«ïŒstrcpy
ãã®é¢æ°åŒã³åºãã«æž¡ãããåŒæ°2ãã³ããŒãããªãããšã確èªããŠãã ãã
åŠçå¯èœãªããŒã¿ãããå€ãã®ããŒã¿ãããããããã¡ãªãŒããŒãããŒãçºçããŸããã
2çªç®ã®åŒæ°ãbuf2ãå«ãããšãã§ãããããå€ãã®ããŒã¿ãã³ããŒãããã©ããã確èªããŸãã
æªãã¯ãããŸããããå€ãã®ç¶æ³ã§åœ¹ç«ã¡ãŸãã ããã«é²ã¿ãŸãããã
Cppcheck ïŒïŒ ãšã©ãŒïŒãŠãããåãããå€æ°ïŒaã
åã®äŸãšåãã§ãããããããªããã
g ++ïŒ-Wallãªãã·ã§ã³ã䜿çšïŒ ïŒ åã®ãã¹ããšåãã§ãã
RATSã¯å€ããå°ãªããã¿ã¹ã¯ã«å¯ŸåŠããæ®ãã¯-æã¡ãŸãïŒ ãªããã¹ãŠããããªã«æªãã®ã§ããïŒ ç¬¬äžã«ããããã®ãŠãŒãã£ãªãã£ãèªåã§æžãã人ã¯ãå®éã«ã¯C ++ã®é人ã§ã¯ãããŸãããã»ãšãã©ã®å Žåãããã°ã©ã ãå ¬éãããã ãã§ãã 第äºã«ã埮åŠãªãšã©ãŒã®å€ãã¯æ€åºãé£ãããåçšãŠãŒãã£ãªãã£ã«ãã£ãŠäž»ã«åŠçãããŸãããç§ã¯ãŸã è©ŠããŠããŸããã 第äžã«ãæ¬ãéèªãç¹ã«Habrãèªãã§ãã ããããããã®ãããªãŠãŒãã£ãªãã£ã¯å¿ èŠãããŸããã
ãæž èŽããããšãããããŸããïŒ
PSã³ãŒãæ¹èšããã°ãå ¬éãããã£ãã®ã§ãããã«ã«ããååã§ã¯ãããŸããã ãããã矀è¡ã倧声ã§å«ã°ãªãå Žåã«åããŠãããã®ããã«ã«ã«ããå¢ãããããšããããã§ã¯ãããŸãã-ããªãããã«ãªãã®ã...ãã
PPS Habrasocietyã«æè¬ããŸã-ãããç¶ããŠãã ããïŒ