fork download
  1. #include <iostream>
  2. #include <cassert>
  3. #include <climits>
  4.  
  5. using namespace std;
  6.  
  7. using uint = unsigned int;
  8.  
  9. struct Array { uint length; };
  10.  
  11. void test1() {
  12. cout << "test1" << endl;
  13.  
  14. Array array = {1000};
  15. uint startIndex = 100;
  16. //uint length = 0xFFFFFFF0;
  17. uint length = 900;
  18.  
  19. assert(startIndex <= UINT_MAX - length);
  20. assert(0 <= startIndex && startIndex + length <= array.length);
  21.  
  22. cout << "array.length = " << array.length << endl
  23. << "startIndex = " << startIndex << endl
  24. << "length = " << length << endl
  25. << "startIndex + length = " << (startIndex + length) << endl;
  26.  
  27. }
  28.  
  29. void test2() {
  30. cout << "test2" << endl;
  31.  
  32. Array array = {1000};
  33. uint startIndex = 0xFFFFFFF0;
  34. uint length = 100;
  35.  
  36. assert(startIndex <= UINT_MAX - length);
  37. assert(0 <= startIndex && startIndex + length <= array.length);
  38.  
  39. cout << "array.length = " << array.length << endl
  40. << "startIndex = " << startIndex << endl
  41. << "length = " << length << endl
  42. << "startIndex + length = " << (startIndex + length) << endl;
  43.  
  44. }
  45.  
  46.  
  47. int main() {
  48. test1();
  49. test2();
  50. return 0;
  51. }
Runtime error #stdin #stdout #stderr 0s 4424KB
stdin
Standard input is empty
stdout
test1
array.length = 1000
startIndex = 100
length = 900
startIndex + length = 1000
test2
stderr
prog: prog.cpp:36: void test2(): Assertion `startIndex <= UINT_MAX - length' failed.