-1

I was trying to solve a question on HackerRank forum,but always i am getting the memory allocation error. Tried to fix the issue but lastly after lots of headbang,failed :( Please help me what's wrong here.(Thanks in advance) Question link : Hackerrank

int main(){
int testcases;
long  N,*segment,*range,max,*temp,*ptr,k,y1,y2,count;
scanf("%d",&testcases);
for(int t=1;t<=testcases;t++){
    max=0;
    scanf("%ld",&N);
    segment=malloc(sizeof(long)*N*2);
    temp=segment;
    for(long i=0;i<N*2;i++){
        scanf("%ld",(temp+i));
        if(*(temp+i)>max)
            max=*(temp+i);
    }
    range=malloc(sizeof(long));
    memset(range,0,sizeof(long)*max);
    ptr=range;

    k=0;

   while(k<(N*2-1)) {
    for(long a=*(temp+k);a<=*(temp+k+1);a++){
        ptr[a]=ptr[a]+1;
    }
       k=k+2;
   }
     y1=0,y2=0;  
    ptr=range;
     for(long i=0;i<max;i++){
        if(ptr[i]>y2){
            y2=y1;
            y1=ptr[i];
        }
     }
    count=0;
    temp=segment;
    for(long i=0;i<(N*2-1);i+=2 ){
        if((y1>=(*(temp+i))&&y1<=(*(temp+i+1))) || y2>=(*(temp+i))&&y2>=(*(temp+i+1)))
            count=count+1;
    }
    printf("Case %d:%ld\n",t,count);
    free(segment);
    free(range);
         }

return 0;}

Error:

solution: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
user4291320
  • 302
  • 1
  • 12
  • On which line does the error occur? – Wai Ha Lee Apr 22 '15 at 07:47
  • 2
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Apr 22 '15 at 07:48
  • @Wai,Please see the below answer added by Alter and comment in reply by me.I am getting Segmentation fault now. – user4291320 Apr 22 '15 at 07:55
  • @Sourav,shouldn't we need to cast the pointer returned by malloc according to our requirement as pointer returned by malloc is of type (void *)? – user4291320 Apr 22 '15 at 07:57
  • 1
    @user4291320 Nopes. Please follow the link in my comment. :-) – Sourav Ghosh Apr 22 '15 at 07:58
  • @user4291320 Have you tried using a debugger (gdb ?) and a memory tool such as valgrind? – Eregrith Apr 22 '15 at 07:58
  • @Sourav,that's a totally new thing i came to know,,Thanks buddy.Btw i am getting Segmentation fault after correcting the mistakes pointed out by Alter in below Answer. – user4291320 Apr 22 '15 at 08:03
  • @Eregrith,nope.I will hop on that.Did you see any silly conceptual mistake in the program that might be the cause of error(anykind) – user4291320 Apr 22 '15 at 08:05
  • 1
    @user4291320 To be honest I did not read your code. We expect people to show they tried to solve their problem before asking, and I don't find your code writing/indenting style pleasant or convenient for reading :) – Eregrith Apr 22 '15 at 08:10

1 Answers1

2
scanf("%ld",&N);
segment=(long *)malloc(sizeof(long)*2);

should be

scanf("%ld",&N);
segment=malloc(sizeof(long)*N*2); /* You forget N (and don't cast malloc)*/

and

for(long i=0;i<(N*2-1);i+2 ){

should be

for(long i=0;i<(N*2-1);i+=2 ){

Running your code in a debugger:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004007f0 in main () at demo.c:28
28          ptr[a]=ptr[a]+1;

You are accessing ptr outside of its bounds in the following block:

while(k<(N*2-1)) {
    for(long a=*(ptr+k);a<=*(ptr+k+1);a++){
        ptr[a]=ptr[a]+1;
    }
    k=k+2;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94