首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AMESim中二维数组的malloc/free问题

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

Stack Overflow用户
提问于 2012-10-18 19:19:34
回答 1查看 769关注 0票数 1

我正在为一个名为AMESim的模拟软件用C语言编程,我需要帮助来管理2-D阵列。

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

我在其中一个子模型中遇到了问题:一个液压泵的模型在该子模型不存在的情况下工作得很好,而当这个模型连接起来时,模拟在一段时间后突然停止(在1.8...模拟时间的秒数,或者在来自积分器的大约1970-1980呼叫之后)。

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

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

代码如下:

代码语言:javascript
复制
#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
}

这是用于分配/释放的两个函数,出于空间原因,我将只编写整数:

代码语言:javascript
复制
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;
}

我写这篇文章是为了检查我是否搞乱了指针的所有引用/取消引用,并更好地了解如何控制堆的空闲空间。错误的另一种解释(我认为应该作为最后的资源考虑)是软件集成器中有一些未知的错误,但肯定很难验证。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-23 16:10:49

结果是参数Le没有正确初始化(它是在分配数组之后初始化的)。

当调用free时,它仅释放L+0内存块,从而导致程序长时间运行期间的内存泄漏。

不幸的是,AMESim的结构有点复杂,特别是在参数关联方面。此外,Fortran不太容易出现这种错误(或者干脆跳过它们),所以在从一种错误转换到另一种错误的过程中,所有这种混乱都会出现……

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12953431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档