#include <stdio.h>
#include <stdlib.h>
long i,j,k,*ni,qi,a,*arr,n,q,p,no,c=0,m,max=0;

int main()
{
    ni = malloc(sizeof(long) * 100000);
    arr=malloc(sizeof(long)*10000);
    scanf("%ld",&no);
    for(i=1;i<=no;i++)
    {
        scanf("%ld %ld",&n,&qi);
        for(j=1;j<=n;j++)
        {
            scanf("%ld",&ni[j]);
        }
        for(k=1;k<=qi;k++)
        {
            scanf("%ld %ld %ld",&a,&p,&q);
            while(p<=q)
            {
                arr[++c]=ni[p++]^a;
            }
           for(m=1;m<=c;m++)
           {

               if(arr[m]>max)
                {
                    max=arr[m];
                }
           }
           printf("\n%ld",max);
           max=0;
           c=0;
        }
    }
    return 0;
}

Bold Text Here

What on earth are you trying to achieve with this program? My eyes have started watering just looking at your code!

A segmentation fault is when you try to access memory outside of your address space, or protected memory that you don't own. It's nearly always the result of a bad pointer or array index value, so checking those values should be your first line of attack.

I totally agree with deceptikon's remark above. You do not check whether malloc() returns a non-NULL value, neither do you check in lines 15, 22 and 29 if you are writing inside or outside the reserved memory area.

help me .. people out there...

I already gave you a direction for troubleshooting. Were you expecting us to do it for you and then tell you exactly what to fix? Or even fix it for you and give you the working code? :rolleyes:

hey deceptikon.. can u be my coach in doing some programming stuffs....

i have gone through ur discussions in this forum... u look quite good with programming methodology....
i have my logics and i just need ur help in applying those in the real world problems...
i have a through knowledge of c and c++.
please help me... we together can top the world... please listen to me...

You scan variables n and qi, malloc ni and arr, and nowhere do you do any bounds checking or verifying that you got a valid memory address. In addition, malloc() returns a void pointer, so assigning it's output to a long pointer is invalid code unless you make an explicit cast.

All that aside, running this code in a debugger will help you find out what is going on and where it is crashing. You say you have a thorough knowledge of C and C++, but that is not the impression I get from your code. :-(

A couple of final comments/questions: why are you starting your array indexes at 1? C/C++ are indexed from 0, and main() without argc/argv should have the signature int main(void)

thanks @rubberman... i got wat u pointed... let me check my code and verify whether it works or not for the issue...

This article has been dead for over six months. Start a new discussion instead.