简体   繁体   中英

Getting segmentation fault in C

In my C code, I am getting a segmentation fault that I can't figure out. Does anyone know what the problem is?

#include <stdio.h>
#include <time.h>   

#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))

int main() {    
    printf("a");
    time_t start_t, end_t;
    double diff_t;
    time(&start_t);

    int width = 440;
    int height = 280;
    int w = 25;
    int b = 15;
    int oo = 5;
    int pheight = 70;

    int m1, m2, m3, m4;
    double *points;
    printf("a");
    int data[2][width][height][width][height][height];
    printf("b");
    int side, ballx, bally, oballx, obally, py;

    for (side = 0; side < 2; side++) {
        for (ballx = w; ballx < width - w - b; ballx++) {
            for (bally = 0; bally < height - b; bally++) {
                if (side == 1) {
                    m1 = MAX(w, ballx - oo);
                    m2 = MIN(width - w - b, ballx);
                } else {
                    m1 = MAX(w, ballx);
                    m2 = MIN(width - w - b, ballx + oo);
                }
                for (oballx = m1; oballx < m2; oballx++) {
                    m3 = MAX(0, bally - oo);
                    m4 = MIN(height - b, bally + oo);
                    for (obally = m3; obally < m4; obally++) {
                        for (py = 0; py < height - pheight; py++) {
                            time(&end_t);
                            diff_t = difftime(end_t, start_t);
                            printf("side %d ballx %d bally %d oballx %d obally %d py %d time %f",
                                   side, ballx, bally, oballx, obally, py, diff_t);
                        }
                    }
                }
            }
        }
    }
    return 0;
}

It seems that this is maybe not the cause of the problem but still worth to mention, that the data mega array is clearly too large for the stack size of your environment, thus your program is causing a Stack Overflow .

It uses 33999257600000 ( assuming sizeof(int) to be 4 which almost surely is the case ) bytes or 31664GB, that is a lot even to allocate on the heap,

#include <stdlib.h>

int main(void)
{
    char *example;
    example = malloc(33999257600000);
    if (example == NULL)
        fprintf(stderr, "Impossible to allocate so much memory\n");
    free(example);
    return 0;
}

the code above will print Impossible ... on almost any nice machine with say 64GB of ram.

It is uncertain why you define data this way:

int data[2][width][height][width][height][height];

But although data is not used in the main function, the compiler generates code to try and allocate it from automatic storage (aka on the stack ). This is way too large, so you invoke undefined behavior.

Your first and second printf do not make it to the console, most probably because you do not manually flush stdout before the crash. Try fflush(stdout); after each debug printf .

Note that it is confusing to use variable names ending in _t such as start_t , end_t and diff_t . This suffix is commonly reserved for types.

Also note for fun that making the screen larger may fix the problem:

int width = 8192;
int height = 4096;

will make data so large that its size will overflow 64 bits and boil down to 0 bytes. The compiler may notice the overflow and complain or just generate code to allocate 0 bytes and run smoothly... Both accepted instances of undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM