Skip to content

Commit 02544de

Browse files
committed
add better side-effect?
1 parent abfa1c0 commit 02544de

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,10 @@ const size_t mb = 513;
1515

1616
template<typename T>
1717
T use(std::vector<T>& todo) {
18-
// init with some data
19-
for (std::size_t i = 0; i < todo.size(); i++) todo[i] = T(i % 7);
20-
// do some computation that needs the memory
21-
for (int k = 0; k < 7; k++) {
22-
for (std::size_t i = 0; i < todo.size(); i++) {
23-
std::size_t j = (i + todo[i]) % todo.size();
24-
todo[j] = (todo[i] % 7) + (todo[j] % 7);
25-
}
26-
}
27-
// accumulate the result
28-
T res = 0;
29-
for (auto x : todo) res = (res >> 1) + (x >> 1);
30-
return res;
18+
if (todo.empty()) return {};
19+
volatile T* p = &to_use[0];
20+
// reading a volatile pointer is a side effect and cannot be optimized
21+
return p[0];
3122
}
3223

3324
int main() {

0 commit comments

Comments
 (0)