分段错误原因不明

时间:2016-12-01 12:45:03

标签: c

这是我的代码:

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

struct pixel
{
    int r, g, b;
};

int main ()
{
    int width, height, i, j, opCode;

    struct pixel **img;

    printf("opCode = ");
    scanf("%d", &opCode);

    printf("Numarul de coloane = ");
    scanf("%d", &width);

    printf("Numarul de linii = ");
    scanf("%d", &height);

    img = malloc(height * width * sizeof(int *));

    for ( i = 0; i < height; i++)
      {
         for ( j = 0; j < width; j++)
        {

             img[i][j].r = scanf("%d ", &img[i][j].r);
             img[i][j].g = scanf("%d ", &img[i][j].g);
             img[i][j].b = scanf("%d ", &img[i][j].b);

        }
      }

    free(*img);

    return 0;
}

当我阅读opCodeheightwidth时,它的工作正常。但在我尝试读取矩阵img的第一个元素后,它会给我一个分段错误(核心转储)。我试图用Valgrind来弄清楚什么是错的,但我无法弄清楚问题。

4 个答案:

答案 0 :(得分:1)

你的问题是

  1. 您尚未为2D阵列正确分配内存。
  2. 您对scanf的使用不正确。请查看手册页。
  3. 这是(不完美)可能有用的代码。您需要检查scanfmalloc的返回值,并采取适当的措施。我把那一点留给你。

    #include <stdio.h>
    #include <stdlib.h>
    
    struct pixel
    {
        int r, g, b;
    };
    
    int main ()
    {
        int width, height, i, j, opCode;
    
        struct pixel **img;
    
        printf("opCode = ");
        scanf("%d", &opCode);
    
        printf("Numarul de coloane = ");
        scanf("%d", &width);
    
        printf("Numarul de linii = ");
        scanf("%d", &height);
    
        img = malloc(height * sizeof(struct pixel *)); // Creates the array of pointers to an array or pixcels
    
        for ( i = 0; i < height; i++)
          {
             img[i] = malloc(sizeof(struct pixel) * width)); // Creates an array of pixels
             for ( j = 0; j < width; j++)
            {
    
                 scanf("%d ", &img[i][j].r); // Should really check the return value here? Perhaps error checking
                 scanf("%d ", &img[i][j].g); // Ditto
                 scanf("%d ", &img[i][j].b);
    
            }
          }
    
        for ( i = 0; i < height; i++) {
    
            free(img[i]);
         }
         free(img);
        return 0;
    }
    

答案 1 :(得分:0)

您使用了错误的'malloc'

    //img = malloc(height * width * sizeof(int *));
      img = (int **)malloc(height*sizeof(int *)); // First Init the double pointer with the number of height 

for ( i = 0; i < height; i++)
  {
     img[i] = (struct pixel *)malloc(width*sizeof(struct pixel)); 
     for ( j = 0; j < width; j++)
    {

         img[i][j].r = scanf("%d ", &img[i][j].r);
         img[i][j].g = scanf("%d ", &img[i][j].g);
         img[i][j].b = scanf("%d ", &img[i][j].b);

    }
  }

现在将自由更改,您需要首先释放每个高度的结构宽度部分。

for (i = 0 ; i < height; i++)
  free(img[i]);

free(img);

答案 2 :(得分:0)

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

struct pixel
{
    int r, g, b;
};

int main ()
{
    int width, height, i, j, opCode;

    struct pixel **img;

    printf("opCode = ");
    scanf("%d", &opCode);

    printf("Numarul de coloane = ");
    scanf("%d", &width);

    printf("Numarul de linii = ");
    scanf("%d", &height);


    /*Try to understand starting from here*/
    img = malloc(height * sizeof(struct pixel *));

    for(i= 0; i < height; i++)
    {
        img[i] = malloc(width*sizeof(struct pixel));
    }

    for(i = 0; i < height; i++)
    {
        for (j = 0; j < width; j++)
        {
            /*This part was wrong in your code*/
            scanf("%d ", &img[i][j].r);
            scanf("%d ", &img[i][j].g);
            scanf("%d ", &img[i][j].b);
        }
    }

    /*This is for you to see*/
    for(i = 0; i < height; i++)
    {
        for(j = 0; j < width; j++)
        {
            printf("%d ", img[i][j].r);
            printf("%d ", img[i][j].g);
            printf("%d ", img[i][j].b);
            printf("\n");
        }
    }

    /*And lastly free allocated spaces*/
    for(i = 0; i < height; i++)
    {
        free(img[i]);
    }

    free(img);

    return 0;
}

答案 3 :(得分:-1)

你搞砸了你的malloc并且自由了。

  1. 您在这里分配的内存太少了:

    img = malloc(height * width * sizeof(int *));

    因为struct pixel至少需要三个单个整数的大小。 (sizeof(int*)错了,因为它只是指针的大小)

  2. free(*img)free(img)对应malloc()来电。

  3. 使用img[i][j].r访问单个像素值无法使用,因为您没有2-dim数组作为存储空间。

  4. 您可以通过单个指针简化双指针指向像素(并在连续内存块中分配所有内容):

    int main() {
        int width, height, i, j, opCode;
    
        printf("opCode = ");
        scanf("%d", &opCode);
    
        printf("Numarul de coloane = ");
        scanf("%d", &width);
    
        printf("Numarul de linii = ");
        scanf("%d", &height);
    
        struct pixel* img = malloc(height * width * sizeof(pixel));
    
        for (i = 0; i < height; i++) {
            for (j = 0; j < width; j++) {
                int val;
                scanf("%d ", &val);
                img[i*width + j].r = val;
                scanf("%d ", &val);
                img[i*width + j].g = val;
                scanf("%d ", &val);
                img[i*width + j].b = val;
            }
        }
        free(img);
        return 0;
    }