?????????????????????????????????? INT ?????????????????INT????????????????????????????д??????????
????#include <iostream>
????int main()
????{
????int n;
????std::cin >> n;
????std::cout << abs(n) << std::endl;
????}
???????£??ú??????????????д????????????????3??????????????????????????????????????δ??????? (Undefined Behavior) ?????硣

????????δ???????
???????????????????????? abs ???????????n? INT_MIN ????????????????C++ ???????????????
????If during the evaluation of an expression?? the result is not mathematically defined or not in the range of representable values for its type?? the behavior is undefined.
?????????2???????У??? n ?? INT_MIN ???int abs(int n) ???????????? int ???Χ???????????????δ???????????????????????????????????????????????????????? cppreference ??????????????? abs?????????cppreference ?????????????δ?????????
????Computes the absolute value of an integer number. The behavior is undefined if the result cannot be represented by the return type.
???????????????δ????????????????????????????????????????????????????????????C++ ???????и?????????????????????????????
????????????????????????? Python ?? Java???У??????????????????????????У???????????????????? C++ ?У??????????????????????а????????У???????????????????????????????????У?C++?????и????????????У???????????????????????????????????????????????????????????????????????????????????????????
????????????????????????е?δ????????????????????????????????棬??????????????????????????????????????δ????????????????????
??????????δ???????
????C++ ??????д?????δ?????????????????в??? undefined behavior????????????????????????????δ???????????????????????????鷳????????????г??Щ??????δ?????????д???????t???????
??????????????δ??????????????????
????· ?????? nullptr ???
????· ?????????δ??????????
????· ?????? new ??????????????
????· ????????磨?????????????????????????
????· ?????????????????????????
?????????????????????????????????????????????????????????з???????????? ?Щ?????????????£?
#include <iostream>
int * get(int tmp){
return &tmp;
}
int main()
{
int *foo = get(10);
std::cout << *foo << std::endl; // Undefined Behavior;
int arr[] = {1??2??3??4};
std::cout << *(arr+4) << std::endl; // Undefined Behavior;
int *bar=0;
*bar = 2;                       // Undefined Behavior;
std::cout << *bar << std::endl;
return 0;
}
????????????δ??????????£?
????· ?з???????????????????????????
????· ?????????????????????λ?????????
????· ????????λ????????????λ?????????????λ??????int64_t i = 1; i <<= 72????
????· ???????????????????????????????
????· ??????????????и????????????в???????int i; i++; cout << i;??
????· ???з?????????????????????????
????????????δ????????б???????????????
???????????δ???????
????C++ ?????????δ?????????????????????? Bug???????????????????????????????????в????δ????????????? python???????? list ????????? list index out of range????Щ?????в??????δ??????????????????????????? C++ ??????????????δ??????????
????????????????????????????????????????????????Ч???????????????????????????y?????????????????????????????????????????????????????????????????????????????????????????????Щ??????
????· ?????????? nullptr??
????· ??????????????????????????
????· ????????????????
?????????????????????????????????????????????????????????????в????????????????????????????????????????Ч??????
?????????????δ????????????????????Υ??????????????????μ?????縳??????????????????????????????±?????????Χ????? C++????????????????????????????????????????????????????????????沢?????????????顣
????????????????????????????????????????Щ???????δ???????????????????????д?????????????
?????????Щ???
????C++ ??????漲???Щ Unspecified Behavior?????????????????????????????????????£?
#include <iostream>
using namespace std;
int get(int i){
cout << i << endl;
return i+1;
}
int Cal(int a?? int b) {
return a+b;
}
int main() {
cout << Cal(get(0)?? get(10)) << endl;
return 0;
}
???????????????????????????????????????0 10 12????????? 10 0 12??????????????????????????? Unspecified Behavior??????C++????? Unspecified Behavior ???????
????Unspecified behavior use of an unspecified value?? or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance.
????????C++????л?????ν?? implementation-defined behavior??????C++?????????????????????????????????????????????????????????洢???????????С?????
???????????£????????????????δ????????????????????3???????????????????????????????????????