【问题标题】:Converting 2d array C code to malloc将二维数组 C 代码转换为 malloc
【发布时间】:2015-02-28 23:46:18
【问题描述】:

我编写了一个程序,将两个矩阵相加并显示它们的和,最大维度为 100。

/* This program asks the user for 2 matrices called A and B, as integers,
   and displays their sum, C. The max dimension of each matrix is 100. */

#include <stdio.h>

// Construct function
void construct()
{
   int m, n, i, j;      // Variables
   int first[100][100], second[100][100], sum[100][100]; // Matrices variables

   printf("Please enter the number of rows: ");
   scanf("%d", &m);
   printf("Please enter the number of columns: ");
   scanf("%d", &n);

   // User enters m x n amount whole numbers for the Matrix A
   printf("Enter Matrix A\n");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &first[i][j]);

   // User enters m x n amount whole numbers for the Matrix B
   printf("Enter Matrix B\n");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
            scanf("%d", &second[i][j]);

   // Adds the sum of Matrix A and Matrix B
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         sum[i][j] = first[i][j] + second[i][j];

   // Display the sum of Matrix A and Matrix B
   printf("A + B =\n");
   for (i = 0; i < m; i++)
   {
      for (j = 0; j < n; j++)
         printf("%d ", sum[i][j]);

      printf("\n"); // Prints new line
   }

   return ;
}

// Main Function
int main()
{
    construct(); // Calls construct function
    return 0;
}

现在我需要更改它,以便每个矩阵没有最大大小。 所以我需要使用 malloc 来创建我的数组。 所以我不能使用 int A[rows][cols]。 这就是我将数组转换为 malloc 所做的。它可以编译,但是在我输入所有整数后它崩溃了。需要帮忙。

/* This program asks the user for 2 matrices called A and B, as integers,
   and displays their sum, C. The max dimension of each matrix is 100. */

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

// Construct function
void construct()
{
   int m, n, i, j;      // Variables
   int *first = NULL;
   int *second = NULL;
   int *sum = NULL;    // Matrices variables

   printf("Please enter the number of rows: ");
   scanf("%d", &m);
   printf("Please enter the number of columns: ");
   scanf("%d", &n);

   first = (int*)malloc(m * sizeof(int));
   second = (int*)malloc(n * sizeof(int));
   sum = (int*)malloc(m * n * sizeof(int));


   // User enters m x n amount whole numbers for the Matrix A
   printf("Enter Matrix A\n");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &first);

   // User enters m x n amount whole numbers for the Matrix B
   printf("Enter Matrix B\n");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
            scanf("%d", &second);

   // Adds the sum of Matrix A and Matrix B
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         sum = *first + *second;

   // Display the sum of Matrix A and Matrix B
   printf("A + B =\n");
   for (i = 0; i < m; i++)
   {
      for (j = 0; j < n; j++)
         printf("%d ", sum);

      printf("\n");
   }

   return ;
}

// Main Function
int main()
{
    construct(); // Calls construct function
    return 0;
}

【问题讨论】:

  • 1) 应始终检查从 scanf() 返回的值,以确保输入/转换成功。 2) 在 C 中,不应强制转换 malloc 的返回值。 3) 应始终检查 malloc 的返回值 (!= NULL) 以确保操作成功
  • 这种行:'c​​anf("%d", &first);'将始终将输入值放在 malloc';d 区域中的相同位置,特别是在前 '4' 字节中。\

标签: c arrays matrix malloc


【解决方案1】:

首先,您不需要使用 malloc。只需将数组定义移动到您输入mn 之后:

int first[m][n];

printf("Enter Matrix A\n");
for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
        scanf("%d", &first[i][j]);

secondsum 也是如此。

如果矩阵可能大于 100x100,那么最好使用malloc 来避免堆栈溢出的风险;这样做的方法是:

int (*first)[n] = malloc(m * sizeof *first);

printf("Enter Matrix A\n");
// etc.

最后,free(first);。无需进行其他更改。


在您尝试使用 malloc 时,您没有分配足够的空间,并且您也没有 scanf 进入您分配的空间(而是覆盖了指向已分配空间的指针)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 2015-10-15
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2020-03-26
    相关资源
    最近更新 更多