You are not logged in. Please login at www.codechef.com to post your questions!

×

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];
    }
}

asked 18 Dec '13, 20:04

codepunch's gravatar image

2★codepunch
31246
accept rate: 0%


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

link

answered 18 Dec '13, 22:15

abbas's gravatar image

4★abbas
4118
accept rate: 28%

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

(18 Dec '13, 22:16) abbas4★
1

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 :)

(19 Dec '13, 00:44) password1★

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

(19 Dec '13, 11:39) codepunch2★

^ 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);

link

answered 19 Dec '13, 17:32

n2n_'s gravatar image

5★n2n_
1.8k61319
accept rate: 9%

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

(19 Dec '13, 20:26) codepunch2★

Did you make the array a global variable?

(19 Dec '13, 20:35) ivan100sic6★

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.

link

answered 19 Dec '13, 22:18

orchidmajumder's gravatar image

3★orchidmajumder
3584815
accept rate: 0%

@orchidmajumder ..thanks it worked..

(19 Dec '13, 22:27) codepunch2★

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

(19 Dec '13, 22:40) codepunch2★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×280
×38

question asked: 18 Dec '13, 20:04

question was seen: 2,918 times

last updated: 19 Dec '13, 22:40