[Solved] Segmentation Fault on return of main function [closed]


Look at your loop here:

    for(int i=0; i<3*n; i++)
    {
        all[j].h = b[i].h;
        all[j].w = min(b[i].w,b[i].l);
        all[j].l = max(b[i].w, b[i].l);

        j++;  // increment 

        all[j].h = b[i].l;
        all[j].w = min(b[i].w,b[i].h);
        all[j].l = max(b[i].w, b[i].h);

        j++;  // increment again

        all[j].h = b[i].w;
        all[j].w = min(b[i].l,b[i].h);
        all[j].l = max(b[i].l, b[i].h);

        j++;  // increment once again
    }

Look at your allocations before the loop:

box * all = new box[3*n];
int j = 0;
int * msh = new int[3*n];

The all has 3*n items, but your loop not only loops up to 3*n, but increments j 3 times within that loop. The value of j will eventually go beyond the allocated space for all.

1

solved Segmentation Fault on return of main function [closed]