savvolej
savvolej

Reputation: 13

process exited due to signal 6/11 c++

I didn't have any problems with compiling my program, but I have a big one when I try to use a bot to check output data. It says: "process exited due to signal 6" or "process exited due to signal 11". I thought it was a matter of dynamic arrays (i have two in this program) but both are deleted at the end. I would be very grateful for any tip!

#include <iostream>

using namespace std;

void input_data (int p_n, int **p_arr);
void output_data (int p_n, int **p_arr);
void check_salary(int p_n, int **p_arr);


int main()
{
    int n;
    cin >> n;
    int** arr = new int*[n];
    for(int i = 0; i < n; ++i){
    arr[i] = new int[2];}
    input_data(n, arr);
    check_salary(n, arr);
    output_data(n, arr);

    for(int i=0; i<n; i++) {
        delete [] arr[i];
    }
    delete[] arr;

    return 0;
}

void check_salary(int p_n, int **p_arr)
{
    bool fit, flag, exist;
    int *used = new int(p_n);
    int boss, salary, i_temp, i_salarless, min_sal ,i_used = 0,boss_salary, id;
    for (int i=0; i<p_n; i++)
        {
            if(p_arr[i][1] != 0)
            {
                used[i_used]=p_arr[i][1];
                i_used++;
            }
        }
    do{
        flag = false;
        exist=false;
        for (int i=0; i<p_n; i++)
        {

            i_salarless = 0;
            id = i+1;
            boss = p_arr[i][0];
            salary=p_arr[i][1];
            boss_salary = p_arr[boss-1][1];

            if(salary==0)
            {
                if(boss==id){ 
                    p_arr[i][1] = p_n;
                    flag = true;
                }else if(boss_salary>0){         
                    for (int j=0; j<p_n; j++)
                        {
                            if (p_arr[j][0]==boss && p_arr[j][1] == 0){i_salarless++;}
                        }                     

                    if (i_salarless == 1){
                        do{
                            --boss_salary;
                            exist = false;
                            for (int i=0; i< i_used; i++){
                                {
                                    if(boss_salary == used[i]){exist = true;}
                                }
                            }

                        }while(exist);
                        p_arr[i][1]= boss_salary;
                        used[i_used]= p_arr[i][1];
                        i_used++;
                        flag = true;
                    }
                }
            }else{}
        }
    } while(flag);
    delete []used;

}

void output_data (int p_n, int **p_arr)
{
    for (int i=0; i<p_n; i++)
    {
        cout<< p_arr[i][1] << "\n";
    }
}

void input_data (int p_n, int **p_arr)
{
    for (int i=0; i<p_n; i++)
    {
        for (int j=0; j<2; j++)
        {
            cin>> p_arr[i][j];
        }

    }

}

Upvotes: 0

Views: 674

Answers (1)

Ankit Kumar
Ankit Kumar

Reputation: 1281

I think the main issue is that you are doing int *used = new int(p_n);, this will create a pointer to an integer that has initial value equal to p_n. But, the problem comes here: used[i_used]=p_arr[i][1]; -- this may lead to a segmentation fault, as you may end up using memory outside the valid bounds.

I'm not sure about the algorithm, but based on the code, I think you should use this instead:

used = new int[p_n]; // assuming p_n > 0

Upvotes: 3

Related Questions