gpt4 book ai didi

c - C 数组中的动态内存分配

转载 作者:行者123 更新时间:2023-11-30 18:12:23 26 4
gpt4 key购买 nike

我花了几个小时尝试调试我的代码,但我失败了。我认为问题在于我没有完全理解动态内存分配,但是我也可能犯了一些其他错误。这里的问题更多的是个人问题,如果有人发现这个问题没有满足“使其与他人相关”的要求,我很抱歉。

我被分配了下一个任务:

使用 0-100 区间内的 n 个随机元素创建一个数组 A[]。创建一个拆分两个数组的函数,例如:数组 B[] 包含元素 > 50,而 C[] 包含其余元素。使用动态内存分配创建数组 A 和 B。函数的参数必须是所有三个数组及其各自的长度。

#include <stdio.h>
#include <stdlib.h>

void Array(int *A, int *nA, int *B, int *nB, int *C, int*nC){

int i;
int nB1 = 0;
int nC1 = 0;
int *tmpB;
int *tmpC;
B = malloc((nB1+1)*sizeof(int));
C = malloc((nC1+1)*sizeof(int));

printf("\n");

for(i = 0 ; i < nA ; i++){
if(A[i] <= 50){
C[i] = A[i];
nC1++;
// The idea here is to have a new array with basically
// no length so that each time one element passes to either
// B or A array that array gets increased at the same
// time as nC or nB
tmpC = realloc(C, sizeof(int) * nC1);
if(tmpC == NULL){
printf("ERROR: realloc failed.\n");
}
C = tmpC;
// C = realloc(C, nC1 + 1);
}
else{
B[i] = A[i];
nB1++;
tmpB = realloc(B, sizeof(int) * nB1);
if(tmpB == NULL){
printf("ERROR: realloc failed.\n");
}
B = tmpB;
// B = realloc(B, nB1 + 1);

}
}

printf("\n");
printf("Array B: ");
nB = nB1;
for(i = 0 ; i < nB ; i++){
printf("%d ", B[i]);
}
printf("\n");
printf("Number of elements in array B: %d\n", nB);

printf("\n");
printf("Array C: ");
nC = nC1;
for(i = 0 ; i < nC ; i++){
printf("%d ", C[i]);
}
printf("\n");
printf("Number of elements in array C: %d\n", nC);

}

void main(){

int *A;
int *B;
int *C;
int nA, nB, nC, i, r, j;
nB = 0;
nC = 0;

printf("Enter the length of array A: ");
scanf("%d", &nA);
printf("\n");

A = malloc(nA * sizeof(int));
if (A == NULL){
printf("ERROR: malloc failed.\n");
return 1;
}

time_t t;
srand((unsigned)time(&t));

printf("Array A: ");

for(i = 0 ; i < nA ; i++){
r = rand() % 101;
A[i] = r;
printf("%d ", r);
}
printf("\n");
Array(A, nA, B, nB, C, nC);
}

到目前为止,我的代码在以下情况下会被破坏:

nA 的用户输入高于 6。
代码工作正常,而数组 A 具有可以放入单个数组的所有元素,例如 BC。但是,如果元素可以拆分,则数组 A 的最后一个元素在 BC 数组中时不会在屏幕上正确显示。

编辑:更新了代码,以便更容易跟踪我的错误。

#include <stdio.h>
#include <stdlib.h>
void Array(int *A, int nA, int *B, int nB, int *C, int nC){

int i;
int nB1 = 0;
int nC1 = 0;
int *tmpB;
int *tmpC;
B = malloc(1*sizeof(int));
if(B == NULL){
printf("ERROR: malloc B failed.\n");
return 1;
}

C = malloc(1*sizeof(int));
if(C == NULL){
printf("ERROR: malloc C failed.\n");
return 1;
}

printf("\n");

for(i = 0 ; i < nA ; i++){
if(A[i] <= 50){
// C[nC1] = A[i];
// nC1++;
// if( nC1 > 1){
// tmpC = realloc(C, sizeof(int) * nC1);
// if(tmpC == NULL){
// printf("ERROR: realloc C failed.\n");
// return 1;
// }
// C = tmpC;
// }
tmpC = realloc(C, sizeof(int) * nC1);
if(tmpC == NULL){
printf("ERROR: realloc C failed.\n");
return 1;
}
C = tmpC;

C[nC1++] = A[i];
// nC1++;
}
else{
// B[nB1] = A[i];
// nB1++;
// if(nB1 > 1){
// tmpB = realloc(B, sizeof(int) * nB1);
// if(tmpB == NULL){
// printf("ERROR: realloc B failed.\n");
// return 1;
// }
// B = tmpB;
// }
tmpB = realloc(B, sizeof(int) * nB1);
if(tmpB == NULL){
printf("ERROR: realloc B failed.\n");
return 1;
}
B = tmpB;
B[nB1++] = A[i];
// nB1++;
}


}

printf("\n");
printf("Array B: ");
nB = nB1;
for(i = 0 ; i < nB ; i++){
printf("%d ", B[i]);
}
printf("\n");
printf("Number of elements in array B: %d\n", nB);

printf("\n");
printf("Array C: ");
nC = nC1;
for(i = 0 ; i < nC ; i++){
printf("%d ", C[i]);
}
printf("\n");
printf("Number of elements in array C: %d\n", nC);
}

int main(){

int *A;
int *B;
int *C;
int nA, nB, nC, i, r;

printf("Enter the length of array A: ");
scanf("%d", &nA);
printf("\n");

A = malloc(nA * sizeof(int));
if (A == NULL){
printf("ERROR: malloc A failed.\n");
return 1;
}

time_t t;
srand((unsigned)time(&t));

printf("Array A: ");

for(i = 0 ; i < nA ; i++){
r = rand() % 101;
A[i] = r;
printf("%d ", r);
}
printf("\n");
Array(A, nA, B, nB, C, nC);

return 0;
}

最佳答案

我想我会尝试解决这个问题。我发现您的代码存在一些值得注意的问题,这些问题可以通过对实现进行一些结构性更改来解决。一些较小的:

void main() {

// Code above here left out.
A = malloc(nA * sizeof(int));
if (A == NULL){
printf("ERROR: malloc failed.\n");
return 1; // Might be a problem here.
}

当没有足够的内存分配给 A 时,您在 main 中返回一个整数。 。这可能会导致某些编译器出现问题 - 或者如果您通常讨厌警告,可以从 void main 进行简单修复至int main将解决这个问题。

第二,在你的函数中Array你有

void Array(int *A, int *nA, int *B, int *nB, int *C, int *nC)

但是,您开始使用nA nBnC正如 int而不是int *这是类型不匹配。您可以更改为

void Array(int *A, int nA, int *B, int nB, int *C, int nC)

最后也是最重要的一点是,使用未初始化的 ints 会遇到堆错误。为传入的数组创建内存:

int nB1;
int nC1;

B = malloc((nB1 + 1)*sizeof(int));
C = malloc((nC1 + 1)*sizeof(int));

最好只添加一个 int不使用 nB1 到数组和nC1 .

B = malloc(1*sizeof(int));
C = malloc(1*sizeof(int));

您应该首先检查是否 BC传入时是空数组,然后根据需要向它们添加内存。

if (A[i] <= 50){
C[i] = A[i];
nC1++;
// The idea here is to have a new array with basically
// no length so that each time one element passes to either
// B or A array that array gets increased at the same
// time as nC or nB
tmpC = realloc(C, sizeof(int) * nC1);
if (tmpC == NULL){
printf("ERROR: realloc failed.\n");
}
C = tmpC;
}

tmpC已重新分配但 C因此,在分配值 C[i] = A[i] 时,永远不会给予更多空间它崩溃了。您需要扩展维护数组 CB

编辑

忽略有关重新分配的评论,realloc您的操作没问题。

您迭代数组 A 中的每个元素,如果该值小于或等于 50,则插入数组 C否则B 。但是,您可能有一些元素满足第一个条件,但随后可以满足下一个条件。然后你像这样分配 B[i] = A[i]但如前所述,您位于 A 中的第 3 个元素但您在 B 中的元素 1 处插入,所以使用 i对两者进行迭代是不正确的。遵循 SGM1 的建议,使用 nB1 读取每个数组。和nC1 ,还有 ryyker 对重新分配的评论。

我每次都能编译并运行它,但是现在你必须处理现在实际分配的值:

for (i = 0; i < nA; i++){
if (A[i] <= 50){
C[nC1] = A[i];
nC1++;
tmpC = (int *)realloc(C, sizeof(int) * nC1);
if (tmpC == NULL){
printf("ERROR: realloc failed.\n");
}
C = tmpC;
// C = realloc(C, nC1 + 1);
}

关于c - C 数组中的动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com