Fundamental doubt of memset

with reference to codforces round 630 div2 problem B,i have a fundamental doubt that memset doesnt works but normal initialisation works…P.S I have declared int as long long

Hey,there might be some other problem with your code other than with memset


In this code i am using the same code as yours and it works fine.

mayb because of the template memset is not working…but there is some problem with memset only

Yes i think that it is due to template. Since it is working fine without template.

@Mohitpandey29
The problem is occuring because in your first code, ans is a pointer. You allocated it memory dynamically.
When you run the statement,
memset(ans, -1ll, sizeof(ans));

It takes the size of that of a pointer. Which in this case would be equal to sizeof(ll). It would only assign -1 to first box of ans.

To correct this, try
memset(ans, -1ll , sizeof(ll) * n);

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.