Memset, Big size array global declaration confusion

#include
#include
using namespace std;
#define ll long

ll a[1000005],pre[1000005];

int main()
{

int t;
cin>>t;
while (t--)
{
    int n;
    cin>>n;
    memset(pre,0,sizeof(pre)); //sets the memory block from pre till end of pre as 0

    pre[0]=1;
    int sum=0;
    for (int i = 0; i < n; i++)
    {
        cin>>a[i];
        sum+=a[i];
        sum%=n;
        sum= (sum+n)%n;
        pre[sum]++; //storing values of sum in frequency array
    }
    ll ans=0;
    for (int i = 0; i < n; i++)
    {
        ll m=pre[i];
        ans+=(m)*(m-1)/2;
    }
    cout<<ans<<endl;

}

return 0;

}

  1. In this program i tried using pre[1000005] = {0} which should do the same thing that is initializing whole array with 0 but test cases failed. What is the difference between memset and {0} ?
  2. If we declare pre and a array in the main function the program terminates immediately why is that and why is global declaration working ?
    Please elaborate

For your second question you can follow this https://discuss.codechef.com/t/maximum-size-of-an-array/3633.
And for the first point are you declaring your array inside the main? Because that will give error anyway.

  1. Ok so arrays declared globally can have greater array size because they are stored in heap segment of the memory ??
  2. No i tried with declaring globally also but it did not work with pre[100000] = {0}.
  1. Yes.
  2. You had to reinitialise the array for each testcase, were you doing that?

Yes i was .
This is what i was doing

#include
using namespace std;
#define ll long long int
ll a[1000005], pre[1000005];

int main()
{

int t;
cin >> t;
while (t--)
{
    int n;
    cin >> n;
    pre[1000005] = {0};
    pre[0] = 1;
    //  read input
    ll sum = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        sum += a[i];
        sum %= n;
        sum = (sum + n) % n;
        pre[sum]++; //We are directly building the frequency bucket array
    }
    ll ans = 0;
    for (int i = 0; i < n; i++)
    {
        ll m = pre[i];
        ans += m * (m - 1) / 2;
    }
    cout << ans << endl;
}

return 0;

}

How is this reinitialisation? It’s like accessing an element and setting it 0.

It is reinitialisation . We are not just accessing an element , we are initializing whole array to 0 with pre[100005] = {0};

Look at this example and try to run it you will understand:

#include
#include <bits/stdc++.h>

using namespace std;
int arr[15];

int main()

{

int t;

cin >> t;

while (t--)

{

    int arr[15] = {0};

    for (int i = 0; i < 15; i++)

    {

        cout << arr[i] << " ";

    }

    cout << endl;

}
return 0;

}
at each iteration array is getting reinitialised.

you are declaring the array here.
int arr[15] = {0), works.

this arr[15] = {0} doesn’t do anything.

Also in your case, you are not using global array, instead you are creating a new array for every t.


Read the highlighted text in the screenshot and also have a look at this:

And see the following program we are not declaring a new array in this .
#include

#include <bits/stdc++.h>

using namespace std;

int arr[15];

int main()

{

int t;

cin >> t;

while (t--)

{

     arr[15] = {0};

    for (int i = 0; i < 15; i++)

    {

        cout << arr[i] << " ";

    }

    cout << endl;

}

return 0;

}

Can’t you see that DATA TYPE that they are giving before the array?
it’s always int arr[] = {0} or char arr[] = {}. This is called initialisation.
As far as your code goes. I made the change, set an element equal to 1, and you can clearly see the output, it stays 1.

include <bits/stdc++.h>

using namespace std;

int arr[15] = {};

int main()

{

int t;

cin >> t;

arr[1] = 1;

while (t–)

{

 arr[15] = {0};

for (int i = 0; i < 15; i++)

{

    cout << arr[i] << " ";

}

cout << endl;

}

return 0;

}

This is because we are not REINITIALISING it.

yes i got it.

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.