Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (60 loc) · 2.3 KB
/
Copy pathmain.cpp
File metadata and controls
76 lines (60 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Violation: Code appears before include directives.
int dummyGlobal = 0;
#include <iostream>
#include <cstring>
// Violation: Global variable with external linkage not declared in a header.
int globalVar = 0;
int main() {
// Violation: Sections of code shall not be commented out.
// int unused = 100;
// Violation: Use of octal constant (other than 0).
int octalValue = 0123;
// Violation: String literals with different encoding prefixes shall not be concatenated.
auto str = u8"Hello, " L"World!";
// Violation: Literal zero (0) used as the null-pointer constant.
int* ptr = 0;
// Violation: Function declared at block scope.
void blockFunction() {
std::cout << "Inside block function" << std::endl;
}
blockFunction();
// Violation: Overlapping object assignment.
char buffer[20];
std::strcpy(buffer, "Overlapping copy");
std::memcpy(buffer + 5, buffer, 10); // overlapping copy
// Violation: Pointer subtraction applied to pointers from different arrays.
int array1[5] = {1, 2, 3, 4, 5};
int array2[5] = {5, 4, 3, 2, 1};
int diff = (&array1[2] - &array2[2]); // undefined behavior
// Violation: Lambda expression used in the same scope should capture by reference
// and its non-void return type should be explicitly specified.
int x = 10;
auto lambda = [x]() { return x + 1; };
std::cout << "Lambda result: " << lambda() << std::endl;
// Violation: Increment operator mixed with other operators.
int i = 0;
int mixed = i++ + 5;
// Violation: Use of dynamic_cast (should not be used).
class Base { public: virtual ~Base() = default; };
class Derived : public Base {};
Base* b = new Derived();
Derived* d = dynamic_cast<Derived*>(b);
delete b;
// Violation: Use of reinterpret_cast (shall not be used).
long addr = reinterpret_cast<long>(ptr);
// Violation: Overloading the unary '&' operator.
class Overloaded {
public:
int value;
Overloaded(int v) : value(v) {}
int operator&() { return value; }
};
Overloaded obj(42);
std::cout << "Overloaded operator& result: " << &obj << std::endl;
// public:
// int value;
// Overloaded(int v) : value(v) {}
// int operator&() { return value; }
// };
return 0;
}