22 lines · cpp
1const int global_const = 42;2 3struct TestStruct {4 const int x;5 int y;6};7 8void immutable_violation_examples() {9 *(int *)&global_const = 100; // warn: Trying to write to immutable memory10 11 const int local_const = 42;12 *(int *)&local_const = 43; // warn: Trying to write to immutable memory13 14 // NOTE: The following is reported in C++, but not in C, as the analyzer15 // treats string literals as non-const char arrays in C mode.16 char *ptr_to_str_literal = (char *)"hello";17 ptr_to_str_literal[0] = 'H'; // warn: Trying to write to immutable memory18 19 TestStruct s = {1, 2};20 *(int *)&s.x = 10; // warn: Trying to write to immutable memory21}22