【发布时间】:2012-10-18 11:19:33
【问题描述】:
我正在用 C 语言编写一个名为 AMESim 的仿真软件,我需要帮助来管理二维数组。
AMESim 的工作原理与 Simulink 或 LabView 等其他仿真软件一样,将一些图标放在称为草图的环境中并将它们链接在一起,模拟真实系统。在一个图标后面有一个 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 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;
}
我写信是为了检查我是否在指针的所有引用/取消引用方面搞砸了,并更好地了解如何控制堆的可用空间。错误的另一种解释(我认为应该作为最后一个资源考虑)是软件集成商中存在一些未知错误,但肯定很难验证。
【问题讨论】:
-
为什么需要单独分配每一行?这样做可能不是最理想的,并且会增加内存碎片。而内存碎片是 malloc() 无法返回更多内存的一个原因。
-
@ydroneaud 我想你建议分配类似
Matrix = malloc((L+Le) * M * sizeof(double));的东西。但是我必须用Matrix[i * ncolumns + j]调用矩阵的一个元素,这很容易出错并且与所有其余代码的逻辑相反。另外,即使只是一种感觉,我认为这不是错误的原因。 -
当 malloc() 返回 NULL 时,您的系统无法为您的程序提供更多内存。您有碎片问题或内存泄漏。尝试使用
valgrind(带有选项--track-origins=yes) -
我尝试过使用 Dr.Memory,这是一个类似于 valgrind 的工具(但它适用于 Windows,而不是 Linux)。但是,它没有发现任何内存泄漏。我会尝试向软件开发人员询问,可能是软件内存管理中的错误......
-
这段代码中没有二维数组。
标签: c multidimensional-array malloc free