Xcode crashes when I clear the console after succesful program execution

I was coding in C and Xcode crashes every time I clear the console (or I rerun the program). The code is the following.

#define N 3
#define M 3

int Peak(float a[][M]);

int main(int argc, const char * argv[])
{
    int i, j, k;
    float matrix[N][M];
    
    printf("\nInitialising [%d][%d] matrix\n", N, M);
    
    for(i=0;i<N;i++)
        for(j=0;j<M;j++)
        {
            printf("\nInsert element (%d,%d):\n", i,j);
            scanf("%f", &matrix[i][j]);
        }
    
    k = Peak(matrix);
    
    printf("\nNumber of peaks: %d\n", k);
    
    
    return 0;
}

int Peak(float a[][M])
{
    int i, j, varbool, peaks=0;
    
    float x;
    
    for(i=0;i<N;i++)
        for(j=0;j<M;j++)
        {
            varbool=1;
            
            x = a[i][j]/2.0;
            
            if(j-1>=0){
                if(x<=a[i][j-1])
                    varbool=0;
            } else if(j+1<M) {
                    if(x<=a[i][j+1])
                        varbool=0;
            } else if(i-1>=0) {
                    if(x<=a[i-1][j])
                        varbool=0;
            } else if(i+1<N) {
                    if(x<=a[i+1][j])
                        varbool=0;
            }
        if(varbool)
            peaks++;

    }

    return peaks;
}

I notice this behavior happens frequently with this kind of C programs.

The crash log reports error code 6.

I tested on two different macs, mine is a macbook pro 2020 13-inch and get the same behavior.

Removing this line printf("\nInsert element (%d,%d):\n", i,j); solves the problem.

Switching your code to use pointers yields the same outcome. You can't simultaneously have both the printf and scanf in the nested for-loop. It's one or the other. The best bet is to file a bug report.


#include <stdlib.h>
#include <stdio.h>
#include <malloc/malloc.h>
#define N 3
#define M 3

int Peak(float *a);

int main(int argc, const char * argv[])
{
    int i, j, k;
    float *matrix = (float*)malloc((N * M) * sizeof(float));
    
    printf("\nInitialising [%d][%d] matrix\n", N, M);
    
    for(i=0;i<N;i++)
        for(j=0;j<M;j++)
        {
            printf("\nInsert element (%d,%d)", i,j);
           scanf("%f", (matrix+i)+j);
        }
    
    k = Peak(matrix);
    free(matrix);
    
    printf("\nNumber of peaks: %d\n", k);
    
    
    return (0);
}

int Peak(float *a)
{
    int i, j, varbool, peaks=0;
    
    float x;
    
    for(i=0;i<N;i++)
        for(j=0;j<M;j++)
        {
            varbool=1;
            
            x = *((a+i)+j)/2.0;
            
            if(j-1>=0){
                if(x<=*((a+i)+j-1))
                    varbool=0;
            } else if(j+1<M) {
                    if(x<=*((a+i)+j+1))
                        varbool=0;
            } else if(i-1>=0) {
                    if(x<=*((a+i-1)+j))
                        varbool=0;
            } else if(i+1<N) {
                    if(x<=*((a+i+1)+j))
                        varbool=0;
            }
        if(varbool)
            peaks++;

    }

    return peaks;
}

Sean.

Xcode crashes when I clear the console after succesful program execution
 
 
Q