简体   繁体   English

我正在尝试为分配创建代码,但我不断收到我无法弄清楚的 memory 错误

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

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. Riverfield Country Day School 今晚将举办舞会之夜。今晚的舞会上将有 M 个男孩和 N 个女孩。 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.输入:第一行包含 T 表示测试用例的数量。 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.第一行包含 M 和 N。第二行包含 M 个整数,每个整数表示男孩的身高。 The third contains N integers each denoting the height of girl.第三个包含 N 个整数,每个整数表示女孩的身高。

Output: Print YES if it is possible for each boy to dance with a girl else print NO. Output:如果每个男孩都可以与女孩跳舞,则打印 YES,否则打印 NO。

Sample input:样本输入:

1 1

4 5 4 5

2 5 6 8 2 5 6 8

3 8 5 1 7 3 8 5 1 7

Output from sample input: Output 来自样本输入:

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.我的代码计划是用女孩和男孩的身高填充两个 arrays,然后用每个男孩搜索每个女孩。 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.我更改了代码以修复一些语法错误,但现在我得到一个不同的错误,它不是 output。 The issue is I get this error:问题是我收到此错误:

Exited with return code -11 (SIGSEGV).以返回码 -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));

} }

In remove_girl() if you want to set girlHeights it needs to be a pointer to pointer:remove_girl()中,如果要设置girlHeights它需要是指向指针的指针:

void remove_girl(int **girlHeights, int index, int numGirls, int size) {
      int p;
      // ...
      *girlHeights = malloc(sizeof(int)*(numGirls-1));      
  }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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