为什么我会出现段错误? [英] Why do I segfault?

查看:133
本文介绍了为什么我会出现段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,出现分段错误,我不知道为什么... 我正在创建一个n是其大小的网格,tab是一个其类型为cellule的数组:一个单元格具有2个值.因此,我在函数creer_grille中创建一个数组我malloc(大小可以为4 6或8),并使用-1和0初始化单元格值.然后在以下函数中测试creer_grille函数.

Here is my code, I'm getting a segmentation fault and I don't know why... I'm creating a grid which n is its size, tab is an array which his type is cellule : a cell has 2 values. So I'm creating in the function creer_grille an array I malloc it (size can be 4 6 or 8) and I initialize the cells values with -1 and 0. Then in the following function I'm testing the creer_grille function.

typedef struct
{
    int val;
    int initial;
} cellule;

typedef struct
{
    cellule  *tab;
    int      n;
} grille;

grille  *creer_grille(int n)
{
    grille *g;
  int    i;

  assert(n == 4 || n == 6 || n == 8 && "Taille de la grille différent de 4,6 ou 8");
    g->n = n;
    g = malloc(sizeof(int) * (n*n));
        if (g->tab == NULL)
                exit(-1);
    for (i = 0; i < n*n; i++)
      {
        g->tab[i].val = -1;
        g->tab[i].initial = 0;
      }
      return g;
}

void detruire_grille(grille * g)
{
  free(g);
}

void test_creer_grille(){
    int i,k;
    for(k = 4; k <= 8 ; k+=2){
        grille * g = creer_grille(k);
        assert(g->n == k && "Problème dans le champ n de la grille");

        //Vérification que les cellules sont vides
        for(i = 0 ; i < k * k ; i++){
            assert(g->tab[i].val == -1 && "Problème : cellule non vide !");
            assert(g->tab[i].initial == 0 && "Problème : cellule initiale !");
        }
        detruire_grille(g);
    }
    printf("Test de la fonction creer_grille OK !\n");
}

int    main()
{
   test_creer_grille();
}

推荐答案

g->n = n;

这是一个未初始化的值-在代码中调用未定义行为.使用malloc分配后,将行移动到.

This is accessing an uninitalized value - invoking Undefined Behavior in your code. Move the line to after you allocate using malloc.

同样,g = malloc(sizeof(int) * (n*n));是错误的,您不希望grille*指向分配给int的块.因为如果没有足够的内存,则会出现不确定的行为,使内存分配不足.

Also g = malloc(sizeof(int) * (n*n)); is wrong you don't want grille* to point to a chunk which is allocated for int's. because in case there is not enough memory there will be undefined behavior acessing memory out of your allocation.

g = malloc(sizeof(*g) * (n));

分配了n*n个存储grille的位置后,应通过建立索引对其进行访问

As you have allocated the n*n locations for storing grille you should access them by indexing

      for (i = 0; i < n; i++)
      {
        // for some x
        g[i].tab[x].val = -1;
        g[i].tab[x].initial = 0;
      }

再次g->tab[i].val = -1;这是错误的,因为前面指出的原因相同.您必须将内存分配给g[i].tab.否则,它是未定义的行为.您必须为g[i].tab分配内存.

Again g->tab[i].val = -1; this is wrong because of the same reason pointed earlier. You have to allocate memory to g[i].tab. Otherwise it's undefined behavior. You have to allocate memory for g[i].tab.

   g[i].tab = malloc(sizeof *g[i].tab * someSize);

您的逻辑也有缺陷.首先分配nxn内存并不意味着您具有nxn网格.您遵循的方法将为您提供连续的nxn元素块,但不会有这种用途. (您可以使用它,但这太过分了.)

Also there is a flaw in your logic. First of all allocating nxn memory doesn't mean you have nxn grid. The method you followed will give you a contiguous chunk of nxn elements and that will not be of that use. (You can make a use of it but that's an overkill).

您能做的最好的事情是一个锯齿状的数组,这里显示了它的示例.

The best thing you can do is a jagged array and it's example is shown here.

示例代码:-

grille  *creer_grille(int n)
{
  grille *g;

  g = malloc(sizeof *g * n);
  if( g == NULL){
    fprintf(stderr,"%s\n","Error in malloc");
    exit(1);
  }

  for (size_t i = 0; i < n; i++)
  {
    g[i].tab = malloc(sizeof *g[i].tab * n);
    if( g[i].tab == NULL){
        fprintf(stderr, "%s\n", "Error in malloc");
        exit(1);
    }
    g[i].n   = n;
    for(size_t j = 0; j < n; j++){
        g[i].tab[j].val = -1;
        g[i].tab[j].initial = 0;
    }
  }
  return g;
}

使用完动态分配的内存后,您必须free. free逻辑类似于-您将首先释放在tab中分配的内存,然后在释放所有这些内存之后,您将释放在g中分配的内存.

You have to free the dynamically allocated memory after you are done working with it. The free logic would be something like - you will first free the memory allocated in tab and then after all of those memory is freed, you will free memory allocated in g.

这篇关于为什么我会出现段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆