基本C矩阵分段内存分配

Basic C Matrix segmentation memory allocation

我试图通过终端 运行 这个程序,但出现了这个错误。 "Segmentation Fault: 11"

我想知道为什么。这个程序所做的是,它读取一个 .ppm 文件并将其信息保存在像素类型的矩阵变量中,因此,一个 PPM 文件基本上由以下部分组成:第一行默认为 "P3",第二行是矩阵的大小,第三行是像素属性可能的最大值,其他行将有 3 个最大值为 255 的整数,因此对于矩阵的每个成员都有一个像素 R、G、 B. 我在函数 save_image 中尝试做的事情,首先识别我们是否正在处理 ppm 文件(检查第一行是否有 P3),然后读取矩阵的行数和列数,然后它使用 malloc 函数创建一个新矩阵,然后它将文件行中的数据保存到变量 myImg 的 .r 和 .g 和 .b 中。 我是 debugging/programming 的新手,所以如果信息不够,我很抱歉,但我已经尽力了。

#include <stdio.h>
#include <stdlib.h>
typedef struct{
    int r;
    int g;
    int b;
}Pixel;

void save_image(FILE* img, Pixel ** newImg) {
    int i;
    int j;
    int fcount;
    int scount;
    int count;
    int dcc;
    char init[3];
    fscanf(img,"%s",init);
    if(init[0]=='P' && init[1]=='3'){
        printf("worked!\n");
        fscanf(img,"%d %d",&j,&i);
        fscanf(img, "%d",&dcc);
        *newImg = (Pixel*)malloc(sizeof(Pixel) * i);
        for ( count = 0; count < i ; ++count)
        {
            newImg[count] = (Pixel*)malloc(sizeof(Pixel) * j);
        }
        for (fcount = 0; fcount <= i ; ++fcount)
        {
         for (scount = 0; scount <= j; ++scount)
         {
            fscanf(img,"%i %i %i",&newImg[i][j].r,&newImg[i][j].g,&newImg[i][j].b);
         }
        }
    }
    else 
        printf("Type of file not recognized\n");

    fclose(img);
}


int main(int argc, char const *argv[])
{
    FILE* image;
    Pixel myImg;
    Pixel** newImg;
    **newImg = myImg;
    image = fopen(argv[1],"r");
    save_image(image,newImg);
    return 0;
}

程序失败,因为 newImg[] 的初始 malloc 正在 malloc's Pixel 大小的倍数,而不是指向 Pixel 的指针的大小,加上将指向 newImg 的指针作为参数传递给save_image() 函数。请参阅我关于应在何处定义变量 newImg 以及对 save_image() 函数

声明的理想修改的评论

鉴于已编写发布的代码,似乎需要 'plain' .ppm 文件格式

并且发布的代码不允许在文件中嵌入任何注释

给定 .ppm 文件格式的描述:

格式定义如下。您可以使用libnetpbm C子程序库方便准确地读取和解释格式。

PPM 文件由一系列一个或多个 PPM 图像组成。图片之前、之后或之间没有数据、分隔符或填充。

每个 PPM 图像包含以下内容:

A "magic number" for identifying the file type. A ppm image's magic number is the two characters "P6".
Whitespace (blanks, TABs, CRs, LFs).
A width, formatted as ASCII characters in decimal.
Whitespace.
A height, again in ASCII decimal.
Whitespace.
The maximum color value (Maxval), again in ASCII decimal. Must be less than 65536 and more than zero.
A single whitespace character (usually a newline).
A raster of Height rows, in order from top to bottom. Each row consists of Width pixels, in order from left to right. Each pixel is a triplet of red, green, and blue samples, in that order. Each sample is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first.

A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous.

In the raster, the sample values are "nonlinear." They are proportional to the intensity of the ITU-R Recommendation BT.709 red, green, and blue in the pixel, adjusted by the BT.709 gamma transfer function. (That transfer function specifies a gamma number of 2.2 and has a linear section for small intensities). A value of Maxval for all three samples represents CIE D65 white and the most intense color in the color universe of which the image is part (the color universe is all the colors in all images to which this image might be compared).

ITU-R Recommendation BT.709 is a renaming of the former CCIR Recommendation 709. When CCIR was absorbed into its parent organization, the ITU, ca. 2000, the standard was renamed. This document once referred to the standard as CIE Rec. 709, but it isn't clear now that CIE ever sponsored such a standard.

Note that another popular color space is the newer sRGB. A common variation on PPM is to substitute this color space for the one specified.

Note that a common variation on the PPM format is to have the sample values be "linear," i.e. as specified above except without the gamma adjustment. pnmgamma takes such a PPM variant as input and produces a true PPM as output. 

以“#”开头的字符串可能是注释,与 PBM 相同。

请注意,您可以使用 pamdepth 在每个样本 1 个字节的格式和每个样本 2 个字节的格式之间进行转换。

此处提及的所有字符均以 ASCII 编码。 "newline" 是指 ASCII 中称为换行符或 LF 的字符。 "white space" 字符是 space、CR、LF、TAB、VT 或 FF(即 ANSI 标准 C 是 space() 函数调用 white space)。 普通 PPM

实际上还有一个非常罕见的 PPM 格式版本:"plain" PPM 格式。上面的格式通常被认为是正常格式,被称为 "raw" PPM 格式。有关普通格式和原始格式如何相互关联以及如何使用它们的一些评论,请参阅 pbm。

普通格式的区别是:

There is exactly one image in a file.
The magic number is P3 instead of P6.
Each sample in the raster is represented as an ASCII decimal number (of arbitrary size).
Each sample in the raster has white space before and after it. There must be at least one character of white space between any two samples, but there is no maximum. There is no particular separation of one pixel from another -- just the required separation between the blue sample of one pixel from the red sample of the next pixel.
No line should be longer than 70 characters. 

这是此格式的小图片示例。

 P3
 # feep.ppm
 4 4
15
 0  0  0    0  0  0    0  0  0   15  0 15
 0  0  0    0 15  7    0  0  0    0  0  0
 0  0  0    0  0  0    0 15  7    0  0  0
15  0 15    0  0  0    0  0  0    0  0  0

每一行的末尾都有一个换行符。

读取这种格式的程序应该尽可能宽松,接受任何看起来像 PPM 图像的东西。