简体   繁体   English

C 练习 malloc 并在 function 中重新分配

[英]C exercise with malloc and realloc in a function

I have this excercise:我有这个练习:

Write a C function RandArr2() that takes as parameters three values:编写一个 C function RandArr2() ,其参数为三个值:

  • an array of integers A整数数组 A
  • an integer n representing the size (number of elements) of A一个 integer n 表示 A 的大小(元素数)
  • an integer n'一个 integer n'

The function RandArr2() returns an array as follows: function RandArr2() 返回一个数组,如下所示:

  • if n == n', it returns A如果 n == n',则返回 A
  • if n > n', it reallocs the array A, creating a new array of integers A' of size n'如果 n > n',它重新分配数组 A,创建一个新的整数数组 A',大小为 n'
  • if n < n', it reallocs the array A, creating a new array of integers A' of size n';如果 n < n',它重新分配数组 A,创建一个新的整数数组 A',大小为 n'; it also fills all the positions of A' between n and n' with random integers between -10 and 10.它还用 -10 和 10 之间的随机整数填充 A' 在 n 和 n' 之间的所有位置。

This is my implementation that return a Segmentation faults.这是我返回分段错误的实现。

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

int * RandArr2(int * A, int n, int n1) {
  int i;
  if (n == n1) {
    for (i = 0; i < n; i++) {
      A[i] = rand();
    }
    return A;

  } else if (n < n1) {
    //reallocs the array A, creating a new array of integers A’ of size n’
    A1 = (int * ) realloc(A, n1 * sizeof(int));
    return A1;
  } else {
    /*reallocs the array A, creting a new array of integers A’ of size n’; it also fills all the
    positions of A’ between n and n’ with random integers between -10 and 10 */
    A1 = (int * ) realloc(A, n1 * sizeof(int));
    for (i = n; i < n1; i++) {
      A1[i] = -10 + rand() % (-10 - 10 + 1);
    }
    return A1;
  }
}

int main() {
  int n, n1;
  int * A;
  int i;
  printf("insert two integer:");
  scanf("%d, %d", & n, & n1);
  A = (int * ) malloc(n * sizeof(int)); //inizializzo l'array
  if (A != NULL) {
    A = RandArr2(A, n, n1);
    for (i = 0; i <= n; i++) {
      printf("\n Elemento del vettore: %d", A[i]);
    }
    free(A);
  } else {
    printf("Error!");
    exit(0);
  }
  return 0;
}

I'm new in C, so you're welcome for every kind of help.我是 C 的新手,欢迎您提供各种帮助。

I have fixed some issues related to segmentation fault in your program.我已经修复了您程序中与分段错误相关的一些问题。 I have removed the rand() function to make things more clear.我删除了rand() function 以使事情更清楚。 You can add rand() wherever you want to add as per your requirements.您可以根据需要添加rand()任何想要添加的位置。

The main issue was however, in the for loop in the main() you are printing the contents of the returned array.然而,主要问题是,在main()for循环中,您正在打印返回数组的内容。 I have fixed it.我已经修好了。 Please check the test-expression in the for loop in main() for understanding the issue.请检查main()for循环中的test-expression以了解问题。 The index of the array cannot be more than array_size-1 as index for array starts from 0 .数组的索引不能超过array_size-1 ,因为数组的索引从0开始。

The fixed code is below.固定代码如下。

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

int* RandArr2(int* A, int n, int n1) {
    int i;
    int* A1 = NULL;
    if (n == n1) {
        for (i = 0; i < n; i++) {
            A[i] = 0xDEAD;
        }
        return A;

    }
    else if (n > n1) {
        //reallocs the array A, creating a new array of integers A’ of size n’
        A1 = (int*)realloc(A, n1 * sizeof(int));
        return A1;
    }
    else {
        /*reallocs the array A, creting a new array of integers A’ of size n’; it also fills all the
        positions of A’ between n and n’ with random integers between -10 and 10 */
        A1 = (int*)realloc(A, n1 * sizeof(int));
        for (i = 0; i < n; i++) {
            A[i] = 0xDEAD;
        }
        for (i = n; i < n1; i++) {
            A1[i] = 0xBEAD;
        }
        return A1;
    }
}
#define _CRT_SECURE_NO_WARNINGS
int main() {
    int n, n1;
    int* A;
    int i;
    printf("insert two integer:");
    scanf_s("%d, %d", &n, &n1);
    A = (int*)malloc(n * sizeof(int)); //inizializzo l'array
    if (A != NULL) {
        A = RandArr2(A, n, n1);
        for (i = 0; i < ((n > n1) ? n1 : n); i++) {
            printf("\n Elemento del vettore: 0x%x", A[i]);
        }
        free(A);
    }
    else {
        printf("Error!");
        exit(0);
    }
    return 0;
}

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

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