Home > Back-end >  Why do I get the stack smashing error while working with arrays in c?
Why do I get the stack smashing error while working with arrays in c?

Time:07-08

I'm trying to solve the 3Sum problem but I don't know why I always get the ''*** stack smashing detected ***: terminated'' error, here's the code:

#include <stdio.h>
int proc(int t[], int n, int* r) {
  int c=0;
   for(int i=0; i<n; i  ){
       for(int j=0; j<n; j  ){
           for(int k=0; k<n; k  ){
               if(i!=j!=k){
                  if(t[i] t[j] t[k]==0){
                     r[c]=t[i];
                      c  ;
                     r[c]=t[j];
                     c  ;
                     r[c]=t[k];
                     c  ;
                   }
                }
            }
        }
    }

    return c;

}
void main(){
    
     int t[6]={-1,0,1,2,-1,-4}, r[6], c;
    
  c=add(t, 6, r);
  
  for(int i=0; i<c; i  )
     printf("%d ", r[i]);


}

CodePudding user response:

There are many problems with your program (as pointed out, i!=j!=k will not work as you think for example).

But the reason for stack smashing is that you are incrementing c to the point where it exceeds the size of your arrays then you do r[c]. When you try to access an index that is above the size of the array you get the error.

For example, if you replace your

int t[6]={-1,0,1,2,-1,-4}, r[6], c;

by

int t[6]={-1,0,1,2,-1,-4}, r[1000], c;

(don't actually solve this problem this way) you will see that the error no longer occur because c never exceeds the size of the r array...

The solution is to compute the maximum value of c beforehand, or use a different datastructure.

Note: for the 3sum problem, it should be possible to calculate the maximum, but you have many more problems here, and most likely it's expected that r should be a 2 dimensional array anyways. At least in leetcode it's the approach.

CodePudding user response:

You can easily find problem if you use debugger and run your program step by step. i!=j!=k is not correct, you probably meant i!=j && j!=k or i!=j && j!=k && i!=k

But most importantly c after few loops is greater than 6, so you're out of array bounds

CodePudding user response:

This if statement

if(i!=j!=k){

is equivalent to

if ( ( i != j ) != k ){

The expression i != j evaluates either to 1 if i is unequal to j or 0 otherwise.

So you will have either

if ( 1 != k ){

or

if ( 0 != k ){

Instead of

if(i!=j!=k){

you need to write

if ( ( i != j ) && ( j != k ) && ( i != k ) ){

Another problem is that this code snippet

  if(t[i] t[j] t[k]==0){
     r[c]=t[i];
      c  ;
     r[c]=t[j];
     c  ;
     r[c]=t[k];
     c  ;
   }

can result in writing to memory outside the array pointed to by the pointer r.

Also it seems that instead of these for loops

for(int i=0; i<n; i  ){
    for(int j=0; j<n; j  ){
        for(int k=0; k<n; k  ){

you mean the following for loops

for(int i=0; i<n; i  ){
    for(int j=i 1; j<n; j  ){
        for(int k=j 1; k<n; k  ){

And pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )
  • Related