Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is running properly on other compliers and ide and is giving correct output but when i am trying to submit my code it is showing SIGSEGV error.

What is causing SIGSEGV error and why it is not shown by other compilers?

What I have tried:

C++
#include <iostream>
#include<vector>

using namespace std;

int main()
{
    int T,i,N,M,Q,j,k,NEW[300][300],X,Y;
    cin>>T;
    
    while(T--)
    {
        cin>>N>>M>>Q;        
         
        for(i=1;i<=N;++i)
        {
            for(j=1;j<=M;++j)
            {
               NEW[i][j]=0; 
            }
        }
        
        for(i=1;i<=Q;++i)
        {
            cin>>X>>Y;            
            
        for(k=X;k<=X;k++)
        {
            for(j=1;j<=M;j++)
            {
                NEW[k][j]+=1;
            }
        }
        
        for(j=Y;j<=Y;j++)
        {
            for(k=1;k<=N;k++)
            {
                NEW[k][j]+=1;
            }
        }        
        }
       
        int c;
        c=0;
        
        for(i=1;i<=N;++i)
        {
            for(j=1;j<=M;++j)
            {
               if(NEW[i][j]%2!=0)
               {
                   c++;
               }
            }
        }
        
        cout<<c<<" \n";        
    }

    return 0;
}
Posted
Updated 14-Oct-19 10:28am
v2
Comments
[no name] 13-Oct-19 21:56pm    
i haven't posted the question as i don't need solution . i just need to know why is this error caused and how to remove it as it is not shown by other compilers and ide
Mohibur Rashid 13-Oct-19 22:17pm    
in c/c++ array index start from 0, not 1 <---- reason for your application to throw memory access violation error.
you do not have any check for value of N and M
KarstenK 14-Oct-19 3:37am    
on which ide is this code running correct? plz answer!
[no name] 14-Oct-19 7:40am    
https://www.onlinegdb.com/online_c++_compiler
CPallini 15-Oct-19 3:37am    
Why are you writing C-like code in C++?

Your loops should be from 0 to <N, or <M, not <=N or <=M.

If you want 10 elements in your array, the elements are numbered 0 through 9, not 1 through 10.
 
Share this answer
 
Quote:
What is causing SIGSEGV error and why it is not shown by other compilers?

SIGSEGV is a runtime error saying that you try to access memory that you do not own.
Different compilers have different way to deal with memory allocation, so depending on compiler choices, a wrong read/write outside of an array will still be in your memory or not.

If M or N go up to 300, you will trash memory outside of array, resulting in potential wrong result.
One way to fix the problem is to add 1 to the size of array
C++
int NEW[301][301];

It is OK with toy programs, but should be avoided as much as possible.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900