is there some sort of memory limit in the solutions we submit..is it okay to use unsigned long long arr[10^7] or does it give sigsegv??

i have this code for TSORT…it works fine on my computer but gives run time error SIGSEGV …pls have a look at it and tell me whats the bug…thanks…

#include<stdio.h>

void partition(unsigned long long arr[],unsigned long long first,unsigned long last);
void m_sort(unsigned long long arr[],unsigned long long low,unsigned long long mid,unsigned long long high);

int main(void)
{
    unsigned long long t,n,i=0;
    unsigned long long arr[10^7];
    scanf("%llu",&n);
    t=n;
    //accepting elements
    while(t--)
    {
        scanf("%llu",&arr[i++]);
    }
    partition(arr,0,n-1);
    
    for(i=0;i<n;i++)
        printf("%llu\n",arr[i]);
    return 0;
}
void partition(unsigned long long arr[],unsigned long long first,unsigned long last)
{
    unsigned long long mid;
    if(first<last)
    {
      mid=(first+last)/2;
      partition(arr,first,mid);
      partition(arr,mid+1,last);
      m_sort(arr,first,mid,last);
    }
}
void m_sort(unsigned long long arr[],unsigned long long low,unsigned long long mid,unsigned long long high)
{
    unsigned long long temp[10^7];
    unsigned long long k=low;
    unsigned long long i=low;
    unsigned long long m=mid+1;
    while(i<=mid&&m<=high)
    {
        if(arr[i]<=arr[m])
        {
            temp[k++]=arr[i++];
        }
        else
        {
            temp[k++]=arr[m++];
        }
    }
    if(i>mid)
    {
        while(m<=high)
        {
            temp[k++]=arr[m++];
        }
    }
    else
    {
        while(i<=mid)
        {
            temp[k++]=arr[i++];
        }
    }
    for(i=low;i<=high;i++)
    {
        arr[i]=temp[i];
    }
}

Use global declaration for big array size.
In main function it will cause stack overflow.

3 Likes

also write ‘10000000’ not 10^7.
‘^’ is bitwise xor opertion.

There is simply not that much memory on the stack. There is much more memory available on the heap so you can either allocate the array on the heap or globally :slight_smile:

1 Like

@abbas tried global declaration…it still gives SIGSEGV and @password…10^7 works…i have tried it in other programs and got AC

^ is Bitwise XOR operator in C/C++. Therefore 10^7 = 13 so 10^7 will only allocate an array of size 13. If you are not convinced, you can try printf("%d\n",10^7);

@n2n_…i tried replacing the 10^7 in the array declarations to 10000000 but it is still giving SIGSEGV…any idea why

Did you make the array a global variable?

generally the maximum array size you can declare is int array of 4*10^6 for most of the problems…And declare it globally.10^7,generally,that much memory is not provided.

@orchidmajumder …thanks it worked…

@orchidmajumder…can you tell me what the error is in the PCYCLE…question in the forum…it also has SIGSEGV