J
J
Jasge2021-10-11 20:58:28
C++ / C#
Jasge, 2021-10-11 20:58:28

How to fix and what is the reason for stack smashing detected?

#include <stdio.h>

int get_max(int [], int );


int main(){
const int N = 14;
int  arr[] = {};
int  l;
l = get_max(arr, N);
return 0;
}




int get_max(int arr[], int size)
{
for (int i = 0; i < size; i++){
  scanf("%d", &arr[i]);
  }
int max = 0;
for (int i = 0; i < size; i++){
        if(arr[i] > max){
                max = arr[i];
        }

}
printf("%d\n", max);
}


After executing a program written in C, it gives an error:
*** stack smashing detected ***: terminated
Emergency stop (memory stack flushed to disk)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Demin, 2021-10-11
@Jasge

Please format the code normally, with indents. The array needs a size, but yours is zero. The get_max function does not return anything, even though the declared type is int.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question