簡體   English   中英

如何調試為什么未分配內存或未存儲分配結果?

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

我正在建立一個有限元求解器。 以下是我的結構。

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

現在,我正在嘗試為domain內的此int** subdomain_vertex_map字段分配空間,如下所示:

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

目前,一些尺寸已進行了硬編碼,以說明問題。 我遇到的問題是未分配內存或未分配內存,但未將其存儲在雙指針數組cartesian_domain->subdomain_vertex_map如下面的gdb跟蹤所示:

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) 

關於如何調試此問題的任何想法? 謝謝!

取消引用指針以獲取cartesian_domain的大小(並且不要calloc

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

應該

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

要么

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

如何調試為什么未分配內存或未存儲分配結果?

已分配但大小錯誤,請看valgrind

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM