Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:49:00+00:00 2026-06-13T09:49:00+00:00

I am programming in C for a simulation software that is called AMESim and

  • 0

I am programming in C for a simulation software that is called AMESim and I need help for the management of 2-D arrays.

AMESim works, like other simulation software such as Simulink or LabView, putting some icons in an ambient called sketch and linking them together, simulating real systems. Behind an icon there is a C function, that is the part that I am writing (well, I am updating it from Fortran). The software has a built-in integrator, that calls the various submodels at needed time and manages the progression in time, so I have not direct access to that part of the code.

I am experiencing problems in one of these submodels: a model of an hydraulic pump where this submodel is not present works perfectly, while when this model is connected the simulation stops abruptly after a certain amount of time (after 1.8… seconds of simulated time, or after about 1970-1980 calls from the integrator).

This submodel does some heavy calculation on tribology using Reynolds equation applied on a 2-D surface, that is represented in the code with a matrix, whose dimensions are decided by the user through graphical interface and then are passed to the function as parameters. On these basis, I need to implement the matrix with dynamical arrays, or better with an array of pointers to pointers (I say one matrix, but really there are several ones of them, some of integer type and some floating, and also about a dozen of mono-dimensional arrays). Matrices are of dimension (L+Le)*M, while some arrays have dimension (L+Le) and others have dimension M.

After experiencing the problem, I tried to narrow the possible causes of error by progressively disabling parts of the code, until I reached the state that is posted below. Doing various tests, I came to understand that the problem is in the allocation of the matrices/arrays: at a certain moment, malloc will return an error (a NULL) when trying to allocate a row of one of the matrices. I tried various configurations of the function and of the subfunctions that regulate allocation/deallocation, but I am stuck with the error. This happens also when changing compiler (I have tried with Intel and MS VisualC 32-bit).

Here is the code:

#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
}

These are the two functions for allocation/deallocation, for space reasons I will write only the integer ones:

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;
}

I was writing to check if I have messed up something with all the referencing/dereferencing of the pointers and to better understand how to control the free space of the heap. Another explanation of the error (that should be taken into account as last resource, I believe) is that there is some unknown bug in the software integrator, but it will surely be difficult to verify.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:49:01+00:00Added an answer on June 13, 2026 at 9:49 am

    It turned out that the parameter Le was not correctly initialized (it was initialized after the allocation of the arrays).

    So, during allocation it had a casual value (let’s say 20), allocating a space of L+20 memory blocks, while when initialized its value was 0. When free was called, it freed only L+0 memory blocks, causing a memory leakage during long runs of the program.

    Unfortunately, the structure of AMESim is a bit complex, particularly regarding parameter assegnation. Also, Fortran is less prone to errors of this kind (or maybe it simply skips them), IMO, so during conversion from one to the other all this kind of mess is showing up…

    Thanks to the ones that have read/replied!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm programming an agent-based simulation and have decided that Boost's MultiIndex is probably the
I've got a site with tags like robotics, programming, simulation, quizzes etc. and I
I would like to experiment with some kernel mode programming. What simulation environments could
I'm programming a Monte-Carlo Simulation that should give the user quite some flexibility. Consequently,
I'm programming a simulation using repast simphony groovy API. There's a method count(myTurtles) which
I am programming an online PHP-based fantasy pet simulation game. I am not very
I'm just programming a Minecraft Redstone Simulator for Android. I'm doing the simulation with
LATER EDIT: Please mention not only game programming books, but also more scientific/simulation oriented
G'day, Edit: While this question covers a situation that can arise in programming a
Programming language: Java Ok, so I want to have a BufferedImage that keeps rotating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.