What is causing SIGSEGV error in my code? - c++

I am neither using pointer nor using freed memory but i do not understand what is causing sigsegv error.
For some test cases the algorithm is working without any error while for other test case it is showing SIGSEGV.
#include<iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
int arr[n],arrh[m];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<m;i++)
{
cin>>arrh[i];
}
int arrcc[m][n]={0}; //Precomputation Cumulative
int val;
for(int j=0;j<m;j++)
{
val=0;
for(int i=0;i<n;i++)
{
if(arr[i]==(j+1))
{
val++;
}
arrcc[j][i]=val;
}
}
int q,l,r;
cin>>q;
int k;
int arrc[m];
while(q--)
{
k=0;
for(int i=0;i<m;i++)
{
arrc[i]=0;
}
cin>>l>>r;
// for(int j=0;j<m;j++) //Time limit exceed need precomputation
// {
// for(int i=(l-1);i<r;i++)
// {
// if(arr[i]==(j+1))
// {
// arrc[j]++;
// }
// }
// }
for(int j=0;j<m;j++) //Calculating from cumulative
{
if(l!=1)
arrc[j]=(arrcc[j][r-1]-arrcc[j][l-2]);
else
arrc[j]=(arrcc[j][r-1]-0);
}
for(int i=0;i<m;i++)
{
if(arrc[i]!=0)
{
if(arrc[i]!=arrh[i])
{
cout<<"0"<<"\n";
k++;
break;
}
}
}
if(k==0)
{
cout<<"1"<<"\n";
}
}
}
link to problem the problem-
https://drive.google.com/open?id=1sYEbtdFTT9ZE67y4Wygvv_AKJWGWgXiI
https://www.hackerearth.com/challenges/competitive/april-circuits-20/algorithm/happy-segments-e290faa6/

cin>>n>>m;
int arr[n],arrh[m];
Firstly, the program is ill-formed in C++. The size of an automatic array must be compile time constant, which n and m are not.
Secondly, in case you intentionally use an extended C++ language: The storage for objects in automatic storage is typically strictly limited. The size of the execution stack is by default about 1-8 mega bytes. For large n or m, those arrays may overflow the stack which, if you are lucky, will cause the program to crash.
If you need an array of runtime size, then use dynamic storage. Simplest solution is to use std::vector. This removes the chance of the array overflowing the stack, and makes the program well-formed C++ in regard to the array size.

Related

Segmentation fault(code dumped) in c++ after several try I cann't get the solution

#include <iostream>
using namespace std;
int main() {
int T,D;
long long int N;
long long int a[N];
long long int b[D];
cin>>T;
for(int i=0;i<T;i++)
{
cin>>N>>D;
for(int i=0;i<N;i++)
{
cin>>a[i];
}
for(int i=0;i<D;i++)
{
b[i]=a[i];
}
for(int i=0;i<(N-D);i++)
{
a[i]=a[i+D];
}
for(int i=0;i<D;i++)
{
a[i+N]=b[i];
}
for(int i=0;i<N;i++)
{
cout<<a[i];
}
cout <<endl;
}
return 0;
}
Why is this coding having segmentation fault? I have seen many solution but cann't get it right.On visual studio or any other application it is not working but on gfg it is working. Please help me solve this problem
There are several things that are wrong.
C-style arrays must be set at compile time. So if you want to use int a[N], N must to known at compile time. If you want a flexible array, one C++ way is to use std::vector.
Then the array a goes from 0 to N-1. So going a[N] is going too far. So a[i+N] is way out if bounds and will be segfault.
you declare array a with N element, but use index out of the array range.
long long int a[N]; // declare here, maximum element is N
for(int i=0;i<D;i++)
{
a[i+N]=b[i]; // use index out of array a
}

Why program is not working for n=100000 even if time complexity of sieve of eratothenes is O(nlog(log(n)))

#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cout<<"enter the no till which prime nos to be found :"<<endl;
cin>>n;
vector<int> prime(n+1,0);// we want n index too
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
for(int j=i*i;j<=n;j=j+i) {
prime[j]=1;
}
}
}
cout<<"prime numbers are: "<<endl;
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
cout<<i<<endl;
}
}
return 0;
}
above code should be working for n>=100000 since n(log(log(n))) will equal to 400000.
Your i*i overflows a 32-bit int when n is more than about 46000. This causes undefined behavior. On my system, it wraps around and initializes j with a negative value, causing a segmentation fault when prime[j] is accessed.
This has nothing to do with time complexity.

I am trying to pass an DMA array and its size as an argument but it is giving an error

I am trying to pass a dynamic memory allocated array and its size to a function 'sum' but it is giving error of permissive what should I do?
#include<conio.h>
#include<iostream>
using namespace std;
int sum(int n[], int *m)
{
for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}
}
int main()
{
int *n,*m,a; //declaration is done here**strong text**
cout<<"enter the size of array = ";
m=new int;
cin>>*m;
n=new int[*m];
for(int i=0;i<*m;i++)
{
cout<<"\n enter the "<<i+1<<" array = ";
cin>>n[i];
cout<<"\n";
}
/* for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}*/
int sum(n,&m);//here "m" is an pointer and I am trying to pass int in a function with an array
return 0;
}
Your code should, probably, look like the following (Linux Ubuntu + gcc):
#include <iostream>
using namespace std;
int sum(int n[], int m)
{
int s=0;
for(int z=0; z<m; z++)
{
cout<<"\n array["<<z<<"]= "<<n[z]<<"\n";
s+=n[z];
}
return s;
}
int main()
{
int *n,m;
cout<<"enter the size of array = ";
cin>>m;
n=new int[m];
for(int i=0; i<m; i++)
{
cout<<"\n enter array["<<i+1<<"] value = ";
cin>>n[i];
cout<<"\n";
}
int s = sum(n, m);
cout<<"s="<<s<<endl;
return 0;
}
There is no use allocating the size of the array m dynamically. It is an ordinary int variable and can be initialized as
cin>>m;
You may also write the sum prototype in the form
int sum(int * n, int m)
It is another way of passing a 1-dimensional array as a function parameter.
Speaking frankly, these questions are the very basics of the language.
You should, probably, read something like
Dynamic memory allocation/dynamic arrays
about dynamic memory allocation and dynamic arrays and
Simple cases of std::cin usage
about the simplest cases of std::cin usage in C++.

Why is IDEone showing runtime error even after getting the output?

What is wrong with my program ?
It works fine on my PC but in IDEone it gives the correct output but shows runtime error. Please help.
#include<bits/stdc++.h>
using namespace std;
struct student
{
int vote;
};
int main()
{
int t;
cin>>t;
while(t--)
{
int count=0;
int n;
cin>>n;
vector <int> a(n);
student s[n];
int k;
cin>>k;
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int temp=0;
for(int i=1;i<=n;i++)
{
if(a[i] != i)
{
temp=a[i];
s[temp].vote++;
}
}
for(int i=1;i<=n;i++)
{
if(s[i].vote==k)
{
count++;
}
}
printf("%d\n",count);
}
return 0;
}
This is the error shown in IDEone :-
Error in `./prog': free(): invalid next size (fast): 0x085cca10
student s[n];
This declares an array called s. It contains n values. The values are s[0] through s[n-1] (you can count them all on your fingers, if you'd like, using a small number of n, such as 5).
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
This attempts to initialize values s[1] through s[n]. The only problem is that s[n] doesn't exist. The last value in the array is s[n-1]. This code will corrupt memory on the stack, resulting in undefined behavior.
The same bug also occurs with the a array.
The index values for both vector and array are zero based and goes up to n - 1. So
for(int i = 0; i != n; ++i)
would be better.
Now you write one element too far, which free finds out later when the data after the memory block is invalid.

Count of Maximum code chef.Runtime Error(SIGSEGV)

Hi I started doing code chef beginner problems and got stuck at this one.I tried reducing the stack memory by declaring the arrays globally but that doesn't work too.Here is my code
#include<iostream>
using namespace std;
#define max 101
int main()
{
int t,n;
cin>>t;
while(t--)
{
int a[max];
int c[max]={0};
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
c[a[j]]++;
}
int temp=0;
int x=0;
for(int k=0;k<n;k++)
{
if(c[k]>temp)
{
temp=c[k];
x=k;
}
}
cout<<x<<" "<<temp<<endl;
}
}
You may need a data structure that does not limit the input value to be bounded, or just change your algorithm.
Either use std::map<int, int> in place of c to count occurence of each number, or just sort a to aggregate same values and count.