Closed coolwanglu closed 9 years ago
@farseerfc
@coolwanglu 你給的那個鏈接:
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
If the converted value would be out of the range of representable values by an int, it causes undefined behavior.
非整數字符串不是UB,會返回0爲結果。超出int範圍纔會UB,不過我覺得這個不用考慮,內核裏已經保證pid不可能超過int範圍了,大多數unix-like系統上pid限制在32768以內(Linux可以改上限)。
大多數Linux上pid 0是個內核線程所以也不會出現在/proc,所以用 >0 判斷就夠了吧。 似乎有的android上pid 0是init,我不知道需不需要考慮這個環境……
啊,跟std::stoi搞混了,没仔细看清。抱歉!
原來C++有std::stoi啊,這是我孤陋寡聞了……還自己實現了一個str2i…… 似乎stoi也不是UB: http://www.cplusplus.com/reference/string/stoi/ http://www.cplusplus.com/reference/cstdlib/strtol/ 似乎是C++11開始有的……嗯一會兒去把C++14的改成stoi……
我正在尝试优化c++14 UB是啥
Undefined behavior……
加油! 不過C++14似乎已經比C++98快了…所以不知道有多少優化的空間… 以及我實現了一個用openmp的C++98版本在PR裏,只加了兩行omp指示符,用多線程之後可以比C的單線程快不少…… 趕腳C要用openmp需要挺大工作量所以就還沒改……
openmp 不是作弊吗 :D
2015-01-07 13:54 GMT+08:00 Jiachen Yang notifications@github.com:
加油! 不過C++14似乎已經比C++98快了…所以不知道有多少優化的空間… 以及我實現了一個用openmp的C++98版本在PR裏,只加了兩行omp指示符,用多線程之後可以比C的單線程快不少…… 趕腳C要用openmp需要挺大工作量所以就還沒改……
— Reply to this email directly or view it on GitHub https://github.com/lilydjwg/swapview/issues/37#issuecomment-68982090.
是的……不過NodeJS_async和coffee版本的也用了異步從而間接用了v8的多線程,所以……
刚才验证了一下std::stoi(".")
会崩的
对。。说错了,不是ub,是exception。
嗯會拋異常,然後再catch住嘛,別的很多語言裏都是這麼寫的…… 性能怎樣就不知道了……
readdir包含'.'和'..',python的不包含
atoi()如果给的字符串不是整数,是undefined behavior http://www.cplusplus.com/reference/cstdlib/atoi/