![](/img/trans.png)
[英]I'm getting Curl error code 4 in C program but I can't figure out what is wrong
[英]I'm trying to create code for an assignment and I keep getting a memory error that I can't figure out
提示是:
Riverfield Country Day School 今晚将举办舞会之夜。今晚的舞会上将有 M 个男孩和 N 个女孩。 每个男孩都想和一个比他矮的女孩跳舞。 一个女孩只能和一个男孩跳舞,反之亦然。 鉴于所有男孩和女孩的身高,判断是否所有男孩都可以与女孩跳舞。
输入:第一行包含 T 表示测试用例的数量。 每个测试用例包含三行。 第一行包含 M 和 N。第二行包含 M 个整数,每个整数表示男孩的身高。 第三个包含 N 个整数,每个整数表示女孩的身高。
Output:如果每个男孩都可以与女孩跳舞,则打印 YES,否则打印 NO。
样本输入:
1
4 5
2 5 6 8
3 8 5 1 7
Output 来自样本输入:
是的
我的代码计划是用女孩和男孩的身高填充两个 arrays,然后用每个男孩搜索每个女孩。 如果男孩的身高大于该位置的女孩,我想删除该索引处的女孩并将女孩身高数组移回一个并调整其大小。 我更改了代码以修复一些语法错误,但现在我得到一个不同的错误,它不是 output。 问题是我收到此错误:
以返回码 -11 (SIGSEGV) 退出。
这是我的代码:
//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));
}
在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.