C语言 AMESim中malloc/free of 2D阵列的问题



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

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

我在其中一个子模型中遇到了问题:不存在此子模型的液压泵模型可以完美运行,而当连接此模型时,模拟在一定时间后突然停止(在 1.8 秒的模拟时间之后,或者在大约 1970-1980 年来自集成商的调用之后)。

该子模型使用应用于二维曲面的雷诺方程对摩擦学进行一些繁重的计算,该方程在代码中用矩阵表示,其尺寸由用户通过图形界面确定,然后作为参数传递给函数。在这些基础上,我需要用动态数组实现矩阵,或者最好用指向指针的指针数组来实现矩阵(我说一个矩阵,但实际上有几个矩阵,一些是整数型的,有些是浮点型的,还有大约十几个单维数组)。矩阵是维数(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 VectRn");   //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 pointersn");
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 不太容易出现此类错误(或者可能只是跳过它们),IMO,因此在从一个转换到另一个的过程中,所有这些混乱都出现了......

感谢那些阅读/回复的人!

最新更新