Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong allocation size lead to runtime error #107

Open
fbrunodr opened this issue Apr 13, 2023 · 1 comment
Open

Wrong allocation size lead to runtime error #107

fbrunodr opened this issue Apr 13, 2023 · 1 comment

Comments

@fbrunodr
Copy link

P2.assign(L2_n, 0);
L2.assign(1<<L2_n, 0);
for (int i = 0; i <= L2_n; ++i) {
P2[i] = (1<<i); // to speed up 2^i
L2[(1<<i)] = i; // to speed up log_2(i)
}

In line 17 we assign P2 a size of L2_n, implying we have the range [0,L2_n) available. Then we iterate i through [0,L2_n] and assign P2[i]. This leads to a problem when i = L2_n. This is a really hard to catch bug, as C++ usually allocate more size than necessary, but this very mistake has got me a runtime error in problem https://www.codechef.com/problems/TALCA?tab=statement

@wqweto
Copy link

wqweto commented Jul 12, 2023

Both P2 and L2 has to be bigger i.e. should be

    P2.assign(L2_n + 1, 0); 
    L2.assign((1<<L2_n) + 1, 0); 

These off-by-one errors both show up when _GLIBCXX_DEBUG is defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants