简体   繁体   中英

How can I debug why memory is not allocated or the result of the allocation is not stored?

I am building a finite element solver. The following are my structures.

// Describes the cartesian grid used in the problem
typedef struct
{
  int N;                            // Number of segments along each direction
  double lb_x;
  double ub_x;
  double lb_y;
  double ub_y;
  double h;                         // Max grid resolution
  double* grid_nodes_x;             // Partition in x direction
  double* grid_nodes_y;             // Partition in y direction
} grid;

// subdomain object is defined before this

// Domain object
typedef struct domain
{
  grid* cartesian_grid;              // Reference to the grid object
  vertex* vertices;                  // Array of all vertexes in the domain
  int subdomain_count_x;             // Subdomains in X direction
  subdomain* subdomains;             // List of all subdomains
  int** subdomain_vertex_map;        // Map of subdomain to vertices, gives the local vertex order for assembly
} domain;

Now, I am trying to allocate space for this int** subdomain_vertex_map field inside domain as below:

// Create one big domain
domain* build_cartesian_domain(grid* cartesian_grid, int subdomain_count_x)
{
  int i,j;
  domain* cartesian_domain;
  #define CGN (cartesian_grid->N + 1)
  cartesian_domain = (domain*) calloc(1, sizeof(cartesian_domain));
  cartesian_domain->cartesian_grid = cartesian_grid;
  cartesian_domain->subdomain_count_x = subdomain_count_x;
  cartesian_domain->subdomain_vertex_map = (int**) calloc(2, sizeof(int*));
  #define CDM (cartesian_domain->subdomain_vertex_map)
  for(i = 0; i < 2; i++)
  {
    cartesian_domain->subdomain_vertex_map[i] = (int*) calloc(10, sizeof(int));
    for(j = 0; j < CGN*CGN; j++)
    {
      CDM[i][j] = -1;
    }
  }
  #undef CGN
  #undef CDM

  return cartesian_domain;
}

For the time being some of the sizes are hard-coded for illustration of the issue. The issue I have is that the memory is not getting allocated or is allocated but does not get stored in the array of double pointers cartesian_domain->subdomain_vertex_map as the gdb trace below shows:

Breakpoint 1, build_cartesian_domain (cartesian_grid=0x606010, subdomain_count_x=1) at domain.c:16
16    cartesian_domain = (domain*) calloc(1, sizeof(cartesian_domain));
(gdb) n
17    cartesian_domain->cartesian_grid = cartesian_grid;
(gdb) 
18    cartesian_domain->subdomain_count_x = subdomain_count_x;
(gdb) 
19    cartesian_domain->subdomain_vertex_map = (int**) calloc(2, sizeof(int*));
(gdb) 
21    for(i = 0; i < 2; i++)
(gdb) 
23      cartesian_domain->subdomain_vertex_map[i] = (int*) calloc(10, sizeof(int));
(gdb) 
24      for(j = 0; j < CGN*CGN; j++)
(gdb) p cartesian_domain->subdomain_vertex_map
$1 = (int **) 0x606700
(gdb) p cartesian_domain->subdomain_vertex_map[i]
$2 = (int *) 0x0
(gdb) p i
$3 = 0
(gdb) 

Any thoughts on how I can debug this issue? Thanks!

Dereference the pointer in order to get the size for cartesian_domain (and don't cast calloc )

cartesian_domain = (domain*) calloc(1, sizeof(cartesian_domain));

should be

cartesian_domain = calloc(1, sizeof(*cartesian_domain));

or

cartesian_domain = calloc(1, sizeof(domain));

How can I debug why memory is not allocated or the result of the allocation is not stored?

It is allocated but with a wrong size, take a look to valgrind

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM