c - AMESim 中二维数组的 malloc/free 问题

标签 c multidimensional-array malloc free

我正在使用 C 语言为名为 AMESim 的仿真软件编程,我需要二维数组管理方面的帮助。

AMESim 的工作原理与其他模拟软件(如 Simulink 或 LabView)一样,将一些图标放在称为草图的环境中并将它们链接在一起,模拟真实系统。在图标后面有一个 C 函数,这是我正在编写的部分(好吧,我正在从 Fortran 更新它)。该软件有一个内置的集成器,可以在需要的时候调用各种子模型并及时管理进度,所以我无法直接访问那部分代码。

我在其中一个子模型中遇到问题:没有此子模型的液压泵模型运行完美,而当连接此模型时,模拟会在一定时间后突然停止(1.8 之后...模拟时间的秒数,或在集成商发出大约 1970-1980 次调用之后)。

此子模型使用应用于二维表面的雷诺方程对摩擦学进行了一些繁重的计算,在代码中用矩阵表示,其尺寸由用户通过图形界面决定,然后传递给函数作为参数。在这些基础上,我需要用动态数组来实现矩阵,或者更好的是用一个指向指针的指针数组(我说一个矩阵,但实际上有几个,一些是整数类型,一些是 float ,还有大约一个十几个一维数组)。矩阵的维度为 (L+Le)*M,而一些数组的维度为 (L+Le),而其他数组的维度为 M

遇到问题后,我尝试通过逐步禁用部分代码来缩小可能的错误原因,直到达到下面发布的状态。做了各种测试,我开始明白问题出在矩阵/数组的分配上:在某个时刻,malloc 将返回一个错误(NULL)当试图分配一行矩阵之一。我尝试了调节分配/解除分配的函数和子函数的各种配置,但我仍然遇到错误。更改编译器时也会发生这种情况(我已尝试使用 Intel 和 MS VisualC 32 位)。

代码如下:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "ameutils.h"    //it is a library of AMESim that has various I/O utilities
#include <malloc.h>
#include <string.h>
#include <direct.h>
#include <errno.h>

//various functions prototypes
void allocate_matrix(int ***table, int rows, int columns) ;
void free_matrix(int*** table, int rows);
void allocate_matrixd(double ***table, int rows, int columns) ;
void free_matrixd(double*** table, int rows);

//bla bla check of parameters and so on

//this is my main function
void barreldin27wpprova_( \*bla bla, there are a lot of parameters 
for the calculation that I will skip */ , 
double *rp, //array with real parameters chosen by the user
int *ip, //array with integer parameters
double *c, //array with static double that are saved between calls of the function
int ic[6] //array with static int
) 
{

int loop, i;
double *Xdib=NULL, *Xwib=NULL, *Xddb=NULL, *Xwdb=NULL;
double **MatH=NULL, **MatPdim=NULL, **Matx=NULL, **Maty=NULL, **DummyMat=NULL, **MatZp=NULL;
int **DummyMatInt=NULL, **Matrixt=NULL, **Matrixtn=NULL, **Matrixp=NULL, **Matrixpn=NULL;
double *VectR=NULL, *DummyL=NULL, *DummyM=NULL, *tetar=NULL, *tetag=NULL, *radim=NULL;
//these are all of my arrays

//allocation of dynamic blocks
   allocate_matrix(&DummyMatInt,(L+Le),M);
      //repeat for all int matrices
   allocate_matrixd(&Matx,(L+Le),M);
      //repeat for all double matrices

//the program stops with an error from malloc during one of these allocations

   VectR= malloc((L+Le) * sizeof(double));
   if (VectR == NULL) { 
    amefprintf(stdout,"Error in allocation of VectR\n");   //amefprintf is internal
                            of AMESim, the same as fprintf
    AmeExit(1); //exit function of AMESim
    }
   //repeated for all dynamic arrays, then initialized to 0.e0 with "for" cycles

//a lot of calculation and subfunctions, that are all disabled in this example; function outputs 
  are set to zero

//Deallocation of dynamic blocks
free_matrix(&DummyMatInt, (L+Le));    //repeated for all int matrices
free_matrixd(&Matx, (L+Le));          //repeated for all double matrices
free(VectR); VectR =NULL;             //repeated for all arrays
}

这是分配/释放的两个函数,由于篇幅原因我只写整数:

void allocate_matrix(int ***table, int rows, int columns) 
{
 int i,j;

 *table =  malloc(rows * sizeof **table );
  if (*table == NULL) { 
    amefprintf(stdout,"Error in memory allocation array of pointers\n");
    AmeExit(1);
    }

 for (i = 0; i < rows; i++) {
     (*table)[i] = malloc(columns * sizeof *(*table)[i]);
     if ((*table)[i] == NULL) { 
        amefprintf(stdout,"Error in memory allocation row %d \n",i);
        AmeExit(1);
        }
     for (j=0; j < columns; j++) {
        (*table)[i][j] = 0;
     }
    }

 }   

void free_matrix(int*** table, int rows)
{
int i;
for (i = 0; i < rows; i++)
    {
    free ((*table)[i]);
    (*table)[i] = NULL;
    }
free (*table);
*table = NULL;
return;
}

我写信是为了检查我是否搞砸了指针的所有引用/取消引用,并更好地了解如何控制堆的可用空间。错误的另一种解释(我认为应该将其作为最后一个资源考虑在内)是软件集成商中存在一些未知错误,但肯定难以验证。

最佳答案

事实证明,参数Le 没有正确初始化(它是在分配数组后初始化的)。

因此,在分配期间它有一个随意的值(比如 20),分配一个 L+20 内存块的空间,而在初始化时它的值是 0。当调用 free 时,它只释放了 L+0 个内存块,导致程序长时间运行时发生内存泄漏。

不幸的是,AMESim 的结构有点复杂,尤其是在参数分配方面。此外,在我看来,Fortran 不太容易出现此类错误(或者它可能只是跳过它们),因此在从一个到另一个的转换过程中,所有这些困惑都会出现......

感谢阅读/回复的 friend 们!

关于c - AMESim 中二维数组的 malloc/free 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12953431/

相关文章:

c - C 中矩阵的每个单元格的递归枚举

c - 使用恒定大小的二维数组时获取 "execvp error: Bad address"

c++ - 在 C++ 中使用箭头符号指向多维 vector 的每个维度

java - 将文本文件读入二维数组

C 分配动态 double 组并使用 memset 初始化它

c - 不同的字符串初始化会产生不同的行为?

c - C 中宏定义 (#define) 的良好编程习惯

c++ - 仅打印数组的某些元素

C 字符串函数可以工作,但在多次调用 malloc 后会失败

多次调用malloc