I'm trying to create code for an assignment and I keep getting a memory error that I can't figure out

Viewed 69

The prompt is:

Riverfield Country Day School will be hosting its Prom Night tonight.There would be M boys and N girls at the prom tonight. Each boy wants to dance with a girl who is strictly shorter than him. A girl can dance with only one boy and vice-versa. Given the heights of all the boys and girls tell whether it is possible for all boys to dance with a girl.

Input: The first line contains T denoting the number of test cases. Each test case contains three lines. The first line contains M and N. The second line contains M integers each denoting the height of boy. The third contains N integers each denoting the height of girl.

Output: Print YES if it is possible for each boy to dance with a girl else print NO.

Sample input:

1

4 5

2 5 6 8

3 8 5 1 7

Output from sample input:

YES

The plan with my code is to fill two arrays with the heights of the girls and boys and then to search through each girl with each boy. If the boy's height is greater than the girl at that spot, I want to remove the girl at that index and shift the girl heights array back one and resize it. I changed the code around to fix some syntax errors but now I get a different error and it doesn't output. The issue is I get this error:

Exited with return code -11 (SIGSEGV).

Here is my code:

//Jacob Hathaway September 11, 2022 Sorting
#include <stdio.h>
#include <stdlib.h>

void remove_girl(int**, int, int, int);

int main() {
int j, h, i, g, testCases, numBoys, numGirls, *boyHeights, *girlHeights;


scanf("%d", &testCases);

for (g = 0; g < testCases; g++) { // For each test case
    scanf("%d", &numBoys);
    scanf("%d", &numGirls);

    boyHeights = (int*)malloc(sizeof(int)*numBoys); //Allocating Memory
    girlHeights = (int*)malloc(sizeof(int)*numGirls); //Allocating Memory

    for(j = 0; j < numBoys; j++){ // Filling Array
        scanf("%d", &h);
        *(boyHeights+j) = h;
    }

    for(j = 0; j < numGirls; j++){ // Filling Array
        scanf("%d", &h);
        *(girlHeights+j) = h;
    }

    for(j = 0; j < numBoys; j++){ // For each boy
        for(i = 0; i < numGirls; i++){ // Go through each girl
            if (girlHeights[i] < boyHeights[j]) { //If the height of the girl at position i is less than boy at position j
                remove_girl(&girlHeights, i, numGirls, sizeof(girlHeights)); // Call to function that removes the girl at i and shifts
                //numGirls -= 1;
            }
    }
    }

        if (((int(sizeof(girlHeights)) - int(sizeof(boyHeights))) <= (numGirls - numBoys))) {
            printf("YES\n");
        }
        else {
            printf("NO\n");
        }

}

free(boyHeights);
free(girlHeights);
return 0;

}

void remove_girl(int **girlHeights, int index, int numGirls, int size) { //removes girl at index and shifts array back
    long unsigned int p;
    for(p = index; p < sizeof(girlHeights) - 1; p++) girlHeights[p] = girlHeights[p+1];
    *girlHeights = (int*)malloc(sizeof(int)*(numGirls-1));

}

1 Answers

In remove_girl():

  1. Pass in a pointer to pointer to girlHeights as you want to change it.

  2. Eliminate the int size argument as you are not using it.

  3. Your loop is wrong. sizeof(pointer) is the number of bytes to store the pointer, probably 8, irregardless of what data it points to.

  4. (minor, not fixed) Prefer using an unsigned type for your index and size (for example size_t). You want to update all variables as to not mix, and guard against any of those going negative (for instance, what should happen if numGirls == 0 in your double loop?).

  5. You need to use realloc() instead of malloc() to resize an array. When you usemalloc() you create a new chunk of memory, lose the data stored before, and leak the old memory.

void remove_girl(int **girlHeights, int index, int numGirls) {
    for(int i = index; i < numGirls - 1; i++)
        (*girlHeights)[i] =  (*girlHeights)[i + 1];

    void *tmp = realloc(*girlHeights, (numGirls - 1) * sizeof(**girlHeights));
    if(!tmp) {
        printf("realloc failed\n");
        exit(1);
    }
    *girlHeights = tmp;
}

and update the call to be:

remove_girl(&girlHeights, i, numGirls);

This resolves your segfault:

$ ./a.out
1
4 5
2 5 6 8
3 8 5 1 7
YES
Related