0

它在我的 Linux 机器(Ubuntu)以及 x86 和 x64 的其他 linux 机器上编译并运行,但在 SunOS Generic_142900-02 sun4u sparc unix 机器上,它在线崩溃

matrix->col_head[i] = col_h;

出现总线错误,另外,当我用 GCC -G 编译它时,GDB 找不到任何调试符号

这是代码:

typedef unsigned short short_u;
typedef struct node{
  short_u         row;
  short_u         col;
  int             value;
  struct node*    row_l;
  struct node*    col_l;
}node_t;

typedef struct matrix{
  short_u     N;
  node_t**    row_head;
  node_t**    col_head;
}matrix_t;

matrix_t* init_matrix(int N){
  matrix_t* matrix = malloc(sizeof(matrix_t*));
  matrix->row_head = malloc(sizeof(node_t*)*N);
  matrix->col_head = malloc(sizeof(node_t*)*N);
  matrix->N = N;
  for (int i = 0; i < N; i++){
      /* row */
      node_t* row_h = malloc(sizeof(node_t*));
      row_h->col = 0;
      row_h->row = i+1;
      row_h->value = 0;
      row_h->col_l = row_h;
      if (i != 0)
          matrix->row_head[i-1]->row_l = row_h;
      matrix->row_head[i] = row_h;
      /* col */
      node_t* col_h = malloc(sizeof(node_t*));
      col_h->col = i+1;
      col_h->row = 0;
      col_h->value = 0;
      col_h->row_l = col_h;
      if (i != 0)
          matrix->col_head[i-1]->col_l = col_h;
      matrix->col_head[i] = col_h;
  }
  matrix->row_head[N-1]->row_l = matrix->row_head[0];
  matrix->col_head[N-1]->col_l = matrix->col_head[0];

  return matrix;
}
4

2 回答 2

2

当你这样做

matrix_t* matrix = malloc(sizeof(matrix_t*));

您分配指向 a 的指针的空间matrix_t,这对于整个matrix_t. 您的总线错误可能是在 和 访问未分配内存的matrix->row_head结果matrix->col_head

你重复这个错误

  node_t* row_h = malloc(sizeof(node_t*));

  node_t* col_h = malloc(sizeof(node_t*));

您没有在 Linux 机器上遇到段错误这一事实是一个令人高兴的意外。

于 2012-04-15T03:00:07.767 回答
1

valgrind您的代码在 Mac OS X下无法正常运行。

我将它包装成一个完整的程序,因此:

#include <stdlib.h>
#include <stdio.h>

typedef unsigned short short_u;
typedef struct node{
  short_u         row;
  short_u         col;
  int             value;
  struct node*    row_l;
  struct node*    col_l;
}node_t;

typedef struct matrix{
  short_u     N;
  node_t**    row_head;
  node_t**    col_head;
}matrix_t;

static matrix_t* init_matrix(int N){
  matrix_t* matrix = malloc(sizeof(matrix_t*));
  matrix->row_head = malloc(sizeof(node_t*)*N);
  matrix->col_head = malloc(sizeof(node_t*)*N);
  matrix->N = N;
  for (int i = 0; i < N; i++){
      /* row */
      node_t* row_h = malloc(sizeof(node_t*));
      row_h->col = 0;
      row_h->row = i+1;
      row_h->value = 0;
      row_h->col_l = row_h;
      if (i != 0)
          matrix->row_head[i-1]->row_l = row_h;
      matrix->row_head[i] = row_h;
      /* col */
      node_t* col_h = malloc(sizeof(node_t*));
      col_h->col = i+1;
      col_h->row = 0;
      col_h->value = 0;
      col_h->row_l = col_h;
      if (i != 0)
          matrix->col_head[i-1]->col_l = col_h;
      matrix->col_head[i] = col_h;
  }
  matrix->row_head[N-1]->row_l = matrix->row_head[0];
  matrix->col_head[N-1]->col_l = matrix->col_head[0];

  return matrix;
}

#if 0
static void free_matrix(matrix_t *matrix)
{
    for (int i = 0; i < matrix->N; i++)
    {
        free(matrix->row_head[i]);
        free(matrix->col_head[i]);
    }
    free(matrix->row_head);
    free(matrix->col_head);
    free(matrix);
}
#endif /* 0 */

int main(void)
{
    matrix_t *m = init_matrix(100);
    //free_matrix(m);
    return(0);
}

它编译干净。它在 Mac OS X 10.7.3 上运行而不会真正崩溃。

输出自valgrind

但是,valgrind这样的抱怨(我调用了程序xx,来源是xx.c):

==73807== Memcheck, a memory error detector
==73807== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==73807== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==73807== Command: ./xx
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000D11: init_matrix (xx.c:21)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005128 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000D2F: init_matrix (xx.c:22)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005130 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000D8A: init_matrix (xx.c:30)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005840 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000D5A: init_matrix (xx.c:26)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000DB7: init_matrix (xx.c:33)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005128 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000E0A: init_matrix (xx.c:39)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005888 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000DDA: init_matrix (xx.c:35)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000E37: init_matrix (xx.c:42)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005130 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000D9A: init_matrix (xx.c:32)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005128 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000DAF: init_matrix (xx.c:32)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005838 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000D5A: init_matrix (xx.c:26)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000E1A: init_matrix (xx.c:41)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005130 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000E2F: init_matrix (xx.c:41)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005890 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000DDA: init_matrix (xx.c:35)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000E64: init_matrix (xx.c:44)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005128 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000E79: init_matrix (xx.c:44)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005128 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000E80: init_matrix (xx.c:44)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100009618 is 0 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000D5A: init_matrix (xx.c:26)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000E88: init_matrix (xx.c:45)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005130 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid read of size 8
==73807==    at 0x100000E9D: init_matrix (xx.c:45)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100005130 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000CF2: init_matrix (xx.c:20)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== Invalid write of size 8
==73807==    at 0x100000EA4: init_matrix (xx.c:45)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807==  Address 0x100009670 is 8 bytes after a block of size 8 alloc'd
==73807==    at 0xB823: malloc (vg_replace_malloc.c:266)
==73807==    by 0x100000DDA: init_matrix (xx.c:35)
==73807==    by 0x100000EE3: main (xx.c:66)
==73807== 
==73807== 
==73807== HEAP SUMMARY:
==73807==     in use at exit: 5,303 bytes in 235 blocks
==73807==   total heap usage: 235 allocs, 0 frees, 5,303 bytes allocated
==73807== 
==73807== LEAK SUMMARY:
==73807==    definitely lost: 1,608 bytes in 3 blocks
==73807==    indirectly lost: 1,600 bytes in 200 blocks
==73807==      possibly lost: 0 bytes in 0 blocks
==73807==    still reachable: 2,095 bytes in 32 blocks
==73807==         suppressed: 0 bytes in 0 blocks
==73807== Rerun with --leak-check=full to see details of leaked memory
==73807== 
==73807== For counts of detected and suppressed errors, rerun with: -v
==73807== ERROR SUMMARY: 804 errors from 16 contexts (suppressed: 1 from 1)

当我启用该free_matrix()功能时,我又遇到了一些错误,但我相信它们是init_matrix(). 然后我没有得到任何泄漏的内存(“仍然可以访问”的内存在系统库中;所有的运行valgrind在 Mac OS X 上都有大量的此类内存)。

修改后的代码

George Skoptsov在他的回答中的诊断是准确的。此固定代码free_matrix()启用后,在valgrind. 注意成语的使用:

SomeType *variable = malloc(sizeof(*variable));
SomeType *arrayvar = malloc(sizeof(*arrayvar) * N);

虽然一开始有点不舒服,但这些避免了你遇到的问题。我试图记住使用它们;我仍然在重新学习很久以前第一次学习的技术,以至于我正在遭受“很难教老狗新技巧”的痛苦。

#include <stdlib.h>
#include <stdio.h>

typedef unsigned short short_u;
typedef struct node{
  short_u         row;
  short_u         col;
  int             value;
  struct node*    row_l;
  struct node*    col_l;
}node_t;

typedef struct matrix{
  short_u     N;
  node_t**    row_head;
  node_t**    col_head;
}matrix_t;

static matrix_t* init_matrix(int N){
  matrix_t* matrix = malloc(sizeof(*matrix));
  matrix->row_head = malloc(sizeof(*matrix->row_head)*N);
  matrix->col_head = malloc(sizeof(*matrix->col_head)*N);
  matrix->N = N;
  for (int i = 0; i < N; i++){
      /* row */
      node_t* row_h = malloc(sizeof(*row_h));
      row_h->col = 0;
      row_h->row = i+1;
      row_h->value = 0;
      row_h->col_l = row_h;
      if (i != 0)
          matrix->row_head[i-1]->row_l = row_h;
      matrix->row_head[i] = row_h;
      /* col */
      node_t* col_h = malloc(sizeof(*col_h));
      col_h->col = i+1;
      col_h->row = 0;
      col_h->value = 0;
      col_h->row_l = col_h;
      if (i != 0)
          matrix->col_head[i-1]->col_l = col_h;
      matrix->col_head[i] = col_h;
  }
  matrix->row_head[N-1]->row_l = matrix->row_head[0];
  matrix->col_head[N-1]->col_l = matrix->col_head[0];

  return matrix;
}

static void free_matrix(matrix_t *matrix)
{
    for (int i = 0; i < matrix->N; i++)
    {
        free(matrix->row_head[i]);
        free(matrix->col_head[i]);
    }
    free(matrix->row_head);
    free(matrix->col_head);
    free(matrix);
}

int main(void)
{
    matrix_t *m = init_matrix(100);
    free_matrix(m);
    return(0);
}

有五个地方malloc()被称为;也有五个地方free()被称为。五个分配和释放中的两个在 0..N-1 循环中,所有这些都很好地平衡。

修订后的输出valgrind

==73943== Memcheck, a memory error detector
==73943== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==73943== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==73943== Command: ./xx
==73943== 
==73943== 
==73943== HEAP SUMMARY:
==73943==     in use at exit: 2,095 bytes in 32 blocks
==73943==   total heap usage: 235 allocs, 203 frees, 8,519 bytes allocated
==73943== 
==73943== LEAK SUMMARY:
==73943==    definitely lost: 0 bytes in 0 blocks
==73943==    indirectly lost: 0 bytes in 0 blocks
==73943==      possibly lost: 0 bytes in 0 blocks
==73943==    still reachable: 2,095 bytes in 32 blocks
==73943==         suppressed: 0 bytes in 0 blocks
==73943== Rerun with --leak-check=full to see details of leaked memory
==73943== 
==73943== For counts of detected and suppressed errors, rerun with: -v
==73943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)

要学习的教训

因此,可以从这个简短的故事中吸取各种教训:

  1. 感谢太阳向您指出了问题。
  2. 了解如何使用valgrind
  3. 考虑使用Type *pointer = malloc(sizeof(*pointer));习语进行分配。
于 2012-04-15T04:42:40.673 回答