问题描述
这是一个用于处理ppm图像文件的程序.
This is in a program meant to work with ppm image files.
尝试修改结构,然后将其写入新文件时出现编译错误.
I'm getting a compilation error when trying to modify a struct and then writing it to a new file.
这是全局结构(在ppmIO.c和ppmIO.h中声明):
This is the global struct (declared in ppmIO.c and ppmIO.h):
ppmIO.c:
struct Image *instance;
ppmIO.h:
struct Image
{
int width;
int height;
unsigned char *data;
};
extern struct Image *instance;
这是我的imageManip.h文件:
This is my imageManip.h file:
#include <ppmIO.h>
void ImageInvert(struct Image **toInvert);
void ImageSwap(struct Image **toSwap);
这些是我的imageManip.c文件的相关部分:
These are the relevant parts of my imageManip.c file:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <ppmIO.h>
#include <imageManip.h>
void ImageInvert(struct Image **toInvert) {
int i;
int pix = (*toInvert->width) * (*toInvert->height);
*toInvert = realloc(*toInvert, 2* sizeof *instance);
for (i = 0; i < pix; i++)
{
*(toInvert)->data = ((unsigned char)255 - *(toInvert)->data));
*(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
*(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
}
}
void ImageSwap(struct Image **toSwap) {
int i;
int pix = (*toSwap)->width * (*toSwap)->height;
*toSwap = realloc(*toSwap, 2* sizeof *instance);
unsigned char what = (*toSwap)->data;
for (i = 0; i < pix-1; i++)
{
(*toSwap)->data = (*toSwap)->data++;
(*toSwap)->data = (*toSwap)->data++;
(*toSwap)->data = what;
what = (*toSwap)->data++;
}
}
这是ImageWrite方法,用于将修改后的图像结构写入文件:
This is the ImageWrite method, used to write the modified image structure to a file:
void ImageWrite(char *filename)
{
int num;
printf("%d", num);
int size = (instance->width) * (instance->height) * 3;
FILE *fp = fopen(filename, "w");
if (!fp) die("cannot open file for writing\n");
fprintf(fp, "P6\n%d %d\n%d\n", instance->width, instance->height, 255);
num = fwrite((void *) instance->data, 1, (size_t) size, fp);
if (num != size) die("cannot write image data to file\n");
fclose(fp);
}
这就是我从main调用修改函数的方式:
This is how I call my modifying functions from main:
ImageInvert(&instance);
ImageSwap(&instance);
ImageWrite(first); //where first is a filename
这是gdb报告的分段错误:
This is the segmentation fault that gdb reports:
Program received signal SIGSEGV, Segmentation fault.
__mempcpy_sse2 () at ../sysdeps/x86_64/memcpy.S:166
166 ../sysdeps/x86_64/memcpy.S: No such file or directory.
有人建议在修改功能(ImageInvert和ImageSwap)中,我会不断更改指针,而不是指向的数据.如果是这种情况,我该如何更改指向的数据而不仅仅是指针?
Someone suggested that in my modifying funtions (ImageInvert and ImageSwap), I keep changing the pointer, not the data pointed at. If that is the case, how can I change the data pointed at instead of just the pointer?
推荐答案
如,可以简化ImageInvert()
的代码.
使用下标符号:
void ImageInvert(struct Image **toInvert)
{
struct Image *image = *toInvert;
int n_colour_vals = (image->width * image->height) * 3;
unsigned char *data = image->data;
for (int i = 0; i < n_colour_vals; i++)
data[i] = 255 - data[i];
}
使用指针:
void ImageInvert(struct Image **toInvert)
{
struct Image *image = *toInvert;
unsigned char *data = image->data;
unsigned char *end = data + (image->width * image->height) * 3;
while (data < end)
{
*data = 255 - *data;
data++;
}
}
也许可以对imageSwap()
进行等效的更改,尽管我不完全确定它应该做什么.上一个问题中存在一些问题,这些问题可能表明重新分配是个好主意,但是(至少对于ImageInvert()
而言)确实不需要.
Equivalent changes can probably be made to imageSwap()
, though I am not entirely sure what it is supposed to be doing. There were problems in the previous question that might have suggested that the reallocation was a good idea, but (at least for ImageInvert()
), that really isn't needed.
这篇关于c-尝试编写修改后的结构时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!