Why segsig run time error ocuur

What does sigsegv runtime error means?
In my case:
it gave me sigsegv runtime error with more number of out put than I assigned to print out from an array in c code

Hello,below is my code turbo sort I do not know why I AM getting wrong output.it work fine when I run with my compiler
#include<stdio.h>
void sort(int **arr) {
for(int i=0; i<(sizeof arr)-1; i++) {
if(arr[i]>arr[i+1]) {
int temp;
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
int main()
{ int t;scanf("%d",&t);
int a[t];

    for(int i=0; i<t; i++) {
        scanf("%d",& a[i]);
    }
    sort(a);
   for(int i=0; i<t; i++) {
       printf("%d\n",a[i]);
   }


return 0;

}

The code has few syntax errors in it.
Few things to mention:-

  • In the function parameter, where you have taken a double-pointer, why double pointer? Even if you just pass arr[], it will work the same way.
  • sizeof() will return the size of the array, which happens to be a number of elements multiplied by the size of int. So it will fail here.

It is always preferable to pass the number of elements of the array to the function.

Try this code:-

#include<stdio.h>
void sort(int arr[], int size_arr) {

	for(int i=0; i< size_arr; i++) {
		if(arr[i]>arr[i+1]) {
			int temp;
			temp=arr[i];
			arr[i]=arr[i+1];
			arr[i+1]=temp;
		}
	}
}

int main(){ 
	int t;
	scanf("%d",&t);
	int a[t];

    for(int i=0; i<t; i++) {
        scanf("%d",& a[i]);
    }
    int n = (sizeof(a)/sizeof(a[0]));   //Number of elements in the array
    sort(a,n);

   	for(int i=0; i<t; i++) {
       printf("%d\n",a[i]);
   	}


return 0;

}
2 Likes

Merely compiling this as C++ would have revealed most of the issues:

[simon@simon-laptop][10:32:59]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling chintu_288-TSORT.cpp
+ g++ -std=c++14 chintu_288-TSORT.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
chintu_288-TSORT.cpp: In function ‘void sort(int**)’:
chintu_288-TSORT.cpp:3:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<(sizeof arr)-1; i++) {
                  ~^~~~~~~~~~~~~~~
chintu_288-TSORT.cpp:6:23: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
             temp=arr[i];
                  ~~~~~^
chintu_288-TSORT.cpp:8:22: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
             arr[i+1]=temp;
                      ^~~~
chintu_288-TSORT.cpp: In function ‘int main()’:
chintu_288-TSORT.cpp:19:11: error: cannot convert ‘int*’ to ‘int**’ for argument ‘1’ to ‘void sort(int**)’
     sort(a);
           ^
chintu_288-TSORT.cpp:13:14: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 { int t;scanf("%d",&t);
         ~~~~~^~~~~~~~~
chintu_288-TSORT.cpp:17:14: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",& a[i]);
         ~~~~~^~~~~~~~~~~~~
+ set +x
Successful

Even compiling at C issues some alarming warnings:

[simon@simon-laptop][10:33:58]
[~/devel/hackerrank/otherpeoples]>gcc chintu_288-TSORT.c -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
chintu_288-TSORT.c: In function ‘sort’:
chintu_288-TSORT.c:3:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<(sizeof arr)-1; i++) {
                   ^
chintu_288-TSORT.c:6:17: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
             temp=arr[i];
                 ^
chintu_288-TSORT.c:8:21: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
             arr[i+1]=temp;
                     ^
chintu_288-TSORT.c: In function ‘main’:
chintu_288-TSORT.c:19:10: warning: passing argument 1 of ‘sort’ from incompatible pointer type [-Wincompatible-pointer-types]
     sort(a);
          ^
chintu_288-TSORT.c:2:6: note: expected ‘int **’ but argument is of type ‘int *’
 void sort(int **arr) {
      ^~~~
chintu_288-TSORT.c:13:9: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
 { int t;scanf("%d",&t);
         ^~~~~~~~~~~~~~
chintu_288-TSORT.c:17:9: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",& a[i]);
         ^~~~~~~~~~~~~~~~~~
2 Likes

why i am getting run time error in this code to reverse an array?

#include
using namespace std;

int main() {
// your code goes here
int N ;
int arr[20];
cin>>N;
for (int i=0;i<N;i++){
cin>>arr[i];
}
for(int i=N-1;i>=0;i–){
cout<<arr[i]<<" ";
}
return 0;
}

What test input are you giving it?