how to fix run time error 'segmentation fault' in c++ - c++

This is code to find if a 2nd string is cipher of the 1st one or not.
To find it I am storing the encryption shift in another variable and then comparing all the corresponding letters of the strings to check if they have equal shift or not.
#include<iostream>
#include<string>
using namespace std;
int main(){
int q;
cin>>q;
int cip[q],flag[q];
string s[q],t[q];
for(int i=0;i<q;i++){
cin>>s[i];
cin>>t[i];
}
for(int i=0;i<q;i++){
cip[i]=(s[i][1]+t[i][1])%26;
}
for(int i=0;i<q;i++){
for(int j=0;j<s[i].size();i++){
if((s[i][j]+t[i][j])%26==cip[i]){
flag[i]=1;
}
else flag[i]=0;;
}
}
for(int i=0;i<q;i++){
if(flag[i]==1){
cout<<cip[i];
}
else cout<<flag[i]-1;
}
}
runtime error: segmentation fault

This block of code is not correct:
for(int i=0;i<q;i++){
for(int j=0;j<s[i].size();i++){
if((s[i][j]+t[i][j])%26==cip[i]){
flag[i]=1;
}
else flag[i]=0;;
}
}
the line
for(int j=0;j<s[i].size();i++)
changing it to j++ should fix the segmentation fault.

Related

tried everything still not understanding why am i getting runtime error

#include <iostream>
using namespace std;
int main() {
int T,X,N;
cin>>T;
for (int i=0;i<T;i++){
cin>>N>>X;
string S;
cin>>S;
int arr[N]={};
arr[0]=X;
for(int i=0;i<(S.length());i++){
if(S[i]=='L'){
arr[i+1]=arr[i]-1;
}
else{
arr[i+1]=arr[i]+1;
}
}
int count=0;
for(int i=1;i<N+1;i++){
for(int j=0;j<i;j++){
if (arr[j]==arr[i]){
count++;
break;
}
}
}
cout<<((N+1)-count);
}
// your code goes here
return 0;
}
Why am i getting a runtime error(SIGSEGV) in in it?
I am not getting any error in vs code for it.
could someone explain plzz
I know the error occurs if the program is trying to take the value of an out of bound array but i have given all the necessary conditions in the program.
EDIT:- I just changed the for loop of T to while loop and it solved it.Could someone explain what just happened.
you have allocated an Array size of N
int arr[N]={};
and in for loop you are looping till N ie i < N+1 or i<=N
for(int i=1;i< N+1 ;i++){
accessing index of arr[N] will throw an error as it goes out of boundry

Segmentation fault in C++ in macOS in code of spirally transversing matrix

I am a beginner and cannot understand why my code is giving segmentation fault error please tell my mistake there are no other errors in the code. the error occurred in vs code in macOS device.the given code is for spirally transversing matrix
#include<iostream>
using namespace std;
# define n 4
#define m 4
void spiral(int r, int c,int arr[n][m])
{
int last_row=r-1 , last_col =c-1;
int left=0, right = last_col;
int top=0, bottom = last_row;
while(top<=bottom && left<=right)
{
for (int i=left;i<=right ;i++){
cout<<arr[top][i]<<" ";
}
top++;
for(int i=top; i<=bottom;i++){
cout<<arr[i][right]<<" ";
}
right--;
if(top<=bottom){
for(int i=right; i>=left;i++){
cout<<arr[i][bottom]<<" ";
}
bottom--;
}
if(left<=right){
for(int i=bottom; i>=top;i++){
cout<<arr[i][left]<<" ";
}
left++;
}
}
}
int main(){
/*int arr[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}*/
int arr[n][m]={
{1,2,3,4},
{1,2,3,4},
{5,6,7,8},
{5,6,7,8},
};
spiral(n,m,arr);
return 0;
}
You just need to look carefully at your code. For instance look at this loop, half way through your function
for(int i=right; i>=left;i++){
cout<<arr[i][bottom]<<" ";
When you get here right equals 2, and left equals 0. So i starts at 2, and keeps increasing, it's always bigger than left, so this loop continues 'for ever'. Very quickly i will be too big for the array you are accessing so you get an access violation.
I guess you meant to write this
for(int i=right; i>=left;i--){
but I'm not sure.
Sometimes all that you need to do is look carefully at your code and see that what you actually wrote is different from what you thought you wrote.
BTW you have the same mistake here
for(int i=bottom; i>=top;i++){
perhaps, again I'm guessing, you meant
for(int i=bottom; i>=top;i--){

Why am I not getting any output(linear search)?

#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}

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.

My DOS crashes everytime I gave large input set for my C++ program

I have written a code in C++ and tried to compile it using MinGW and TDMGCC compiler. It get compiles very well. But the problem occurs when i tried to use .exe file generated by these compiler and i give a large input set to my program my dos stops working and says windows has stopped working. After clicking on OK it terminates my execution. How can i fix it.
Here is my code
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int t,n,k,c,x,w;
ofstream myfile;
myfile.open ("example.txt");
cin>>t;
for(w=1;w<=t;w++)
{
cin>>n>>k>>c>>x;
int a[n],b[n],array[n][n];
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
array[i][j]=(a[i]*i+b[j]*j+c)%x;
}
}
int re[1000],z=0;
if(k>1){
for(int i=1;i+k-1<=n;i++)
{
for(int j=1;j+k-1<=n;j++)
{
re[z]=array[i][j]+array[i][j+1]+array[i+1][j]+array[i+1][j+1];
z++;
}
}}
else
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
re[z]=array[i][j];
z++;
}
}
}
int lar=re[0];
for(int i=1;i<z;i++)
{
if(re[i]>lar)
lar=re[i];
}
myfile<<"Case #"<<w<<": "<<lar<<endl;
}
myfile.close();
return 0;
}
You have a basic off-by-one error:
int a[n],b[n],array[n][n];
for(int i=1;i<=n;i++)
cin>>a[i];
a has length n, meaning valid indices are between 0 and n-1 inclusive. You're writing a value at index n, which is past the end.
Structure your loops like this instead:
for (int i = 0; i < n; i++)
cin >> a[i];
All your loops show this problem.
Also, like #NathanOliver says, use std::vector instead of variable-length arrays (VLA). VLA is in the C standard, but it's not in C++. So your use of it here is non-standard. Simply add #include <vector> and replace
int a[n];
with
std::vector<int> a(n);
and everything else can stay the same.