c - 使用全局结构变量时出现分段错误
c - Segmentation fault when using global struct variable
这是一个旨在处理 ppm 图像文件的程序。
我在尝试从文件中读取图像并将该图像分配给我的全局结构图像时遇到分段错误。
这些是我的 ppmIO.c 文件的相关部分:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <ppmIO.h>
struct Image *instance;
void ImageRead(char *filename)
{
printf("hi 0!");
int width, height, num, size;
//unsigned char *p;
//Image *image = (Image *) malloc(sizeof(Image));
FILE *fp = fopen(filename, "r");
//if (!image) die("cannot allocate memory for new image");
if (!fp) die("cannot open file for reading");
readPPMHeader(fp, &width, &height);
size = width * height * 3;
printf("hi!");
//instance->data = (unsigned char *) malloc(size);
printf("hi 2!");
instance->width = width;
printf("hi 3!");
instance->height = height;
printf("hi 4!");
if (!instance->data) die("cannot allocate memory for new image");
num = fread((void *) instance->data, 1, (size_t) size, fp);
if (num != size) die("cannot read image data from file");
fclose(fp);
}
这是我的 ppmIO.h 文件:
#ifndef PPMIO_H
#define PPMIO_H
struct Image
{
int width;
int height;
unsigned char *data;
};
extern struct Image *instance;
//extern Image *ImageCreate(int width, int height);
//extern void ImageClear(struct Image *image, unsigned char red, unsigned char green, unsigned char blue);
extern void ImageRead(char *filename);
extern void ImageWrite(char *filename);
extern void ImageSetPixel(int x, int y, int chan, unsigned char val);
extern unsigned char ImageGetPixel(int x, int y, int chan);
#endif /* PPMIO_H */
这是 GDB 报告的分段错误:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400fff in ImageRead (filename=0x7fffffffdc32 "nika.ppm")
at ppmIO.c:126
126 instance->width = width;
我认为我尝试使用 Image *instance
的方式存在问题...但我真的不知道是什么导致了这个混乱。 :(
您收到此错误是因为您没有为 instance
分配任何内存。在尝试使用实例的任何成员(即 width
或 data
)之前,您必须分配内存(从函数内部),即:
instance = malloc(sizeof *instance);
您不应强制转换实例的 return 值(参见:this), and there is no need to specify the type since the compiler already knows. You can't allocate the memory when you declare the variable as static initializations must be to constant values (See: this)(函数的 return 值不是常量)。
您还需要根据从文件中读取的大小为结构的 instance->data
部分分配内存。
这是一个旨在处理 ppm 图像文件的程序。
我在尝试从文件中读取图像并将该图像分配给我的全局结构图像时遇到分段错误。
这些是我的 ppmIO.c 文件的相关部分:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <ppmIO.h>
struct Image *instance;
void ImageRead(char *filename)
{
printf("hi 0!");
int width, height, num, size;
//unsigned char *p;
//Image *image = (Image *) malloc(sizeof(Image));
FILE *fp = fopen(filename, "r");
//if (!image) die("cannot allocate memory for new image");
if (!fp) die("cannot open file for reading");
readPPMHeader(fp, &width, &height);
size = width * height * 3;
printf("hi!");
//instance->data = (unsigned char *) malloc(size);
printf("hi 2!");
instance->width = width;
printf("hi 3!");
instance->height = height;
printf("hi 4!");
if (!instance->data) die("cannot allocate memory for new image");
num = fread((void *) instance->data, 1, (size_t) size, fp);
if (num != size) die("cannot read image data from file");
fclose(fp);
}
这是我的 ppmIO.h 文件:
#ifndef PPMIO_H
#define PPMIO_H
struct Image
{
int width;
int height;
unsigned char *data;
};
extern struct Image *instance;
//extern Image *ImageCreate(int width, int height);
//extern void ImageClear(struct Image *image, unsigned char red, unsigned char green, unsigned char blue);
extern void ImageRead(char *filename);
extern void ImageWrite(char *filename);
extern void ImageSetPixel(int x, int y, int chan, unsigned char val);
extern unsigned char ImageGetPixel(int x, int y, int chan);
#endif /* PPMIO_H */
这是 GDB 报告的分段错误:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400fff in ImageRead (filename=0x7fffffffdc32 "nika.ppm")
at ppmIO.c:126
126 instance->width = width;
我认为我尝试使用 Image *instance
的方式存在问题...但我真的不知道是什么导致了这个混乱。 :(
您收到此错误是因为您没有为 instance
分配任何内存。在尝试使用实例的任何成员(即 width
或 data
)之前,您必须分配内存(从函数内部),即:
instance = malloc(sizeof *instance);
您不应强制转换实例的 return 值(参见:this), and there is no need to specify the type since the compiler already knows. You can't allocate the memory when you declare the variable as static initializations must be to constant values (See: this)(函数的 return 值不是常量)。
您还需要根据从文件中读取的大小为结构的 instance->data
部分分配内存。