Skip to content

Commit 8e9dfe4

Browse files
committed
fix side-effect code
1 parent 02544de commit 8e9dfe4

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

test/problems/hellounix/submissions/run_time_error/test-memsize.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ using namespace std;
1313

1414
const size_t inc_mb = 128;
1515

16+
template<typename T>
17+
T use(std::vector<T>& todo) {
18+
if (todo.empty()) return {};
19+
volatile T* p = &todo[0];
20+
// reading a volatile pointer is a side effect and cannot be optimized
21+
return p[0];
22+
}
23+
1624
int main() {
1725
vector<vector<char>> vs;
1826
while(true) {
1927
vs.emplace_back(inc_mb * 1024 * 1024);
20-
std::cerr << "Allocated: " << inc_mb * vs.size() << " MB" << std::endl;
28+
std::cerr << "Allocated: " << inc_mb * vs.size() << " MB (" << use(vs.back()) << ")" << std::endl;
2129
}
2230
return 0;
2331
}

test/problems/hellounix/submissions/run_time_error/test-memsize_512.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const size_t mb = 513;
1616
template<typename T>
1717
T use(std::vector<T>& todo) {
1818
if (todo.empty()) return {};
19-
volatile T* p = &to_use[0];
19+
volatile T* p = &todo[0];
2020
// reading a volatile pointer is a side effect and cannot be optimized
2121
return p[0];
2222
}

0 commit comments

Comments
 (0)