写入二维数组时出现分段错误 [英] Segmentation fault by writing in a 2D Array

查看:58
本文介绍了写入二维数组时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个小的内存访问问题,我没有找到错误,也许有人可以帮助我.

I have a small memory access problem in my program and I do not find the error, maybe someone could help me.

我创建了一个新类型来存储 rgb 颜色值.这种类型看起来像:

I have created a new type to store rgb color values. That type looks like:

typedef struct pixel {
    unsigned char r;
    unsigned char g;
    unsigned char b;
} pixel;

在我的主程序中,我使用 calloc 创建了一个 2D 动态数组,用于存储红色信息.

In my main program I create with calloc a 2D dynamically array, to store the red color information’s.

pixel **pixelvalue = (pixel **) calloc(imginformation.width, sizeof(pixel));
for (i = 0; i < imginformation.width; i++) {
    pixelvalue[i] = (pixel *) calloc(imginformation.height, sizeof(pixel));
}

之后我调用我的函数,它读取颜色值以及谁应该将它们保存到数组中.该函数将数组作为参数.

After that I call my function, which read the color values and who should safe them to the array. The function gets as parameter the array.

ReadFile(file, imginformation (Stuff like height and so one), pixelvalue (The calloc array));

在该函数中,我尝试使用

In that function I try to write the values with

pixelvalue[i][j].r = (unsigned char)fgetc(in);

这里出现内存访问错误,我做错了什么?

Here I get the memory access error, what did I wrong?

编辑

首先很抱歉缺少语言,昨天我有点累:)

Hi, first of all sorry about the missing language, I was a little bit tired yesterday :).

为了理解,我创建了一个像素数组并且元素指向另一个像素数组?类似于[指向另一个一维数组像素]?

To understanding, I created an array of pixel and the elements are pointing to another array of pixel? Something like [Point to another 1D array pixel]?

使用像素 **pixelvalue = calloc(imginformation.width, sizeof(pixel *)); 我创建 imginformation.width 来自类型像素和每个指针的指针数显示为像素,对吗?

With pixel **pixelvalue = calloc(imginformation.width, sizeof(pixel *)); I create imginformation.width numbers of pointers from type pixel and each pointer show to pixel, right?

如果我错了,如果你能解释得更详细一点,那就太棒了.我真的很想了解它.

It would be awesome if you could explain it a little bit more, if I’m wrong. I would really like to understand it.

@carl-norum 你是什么意思:

@carl-norum What do you mean with:

您不应该强制转换 calloc() 的返回值.这样做可以用 #include 隐藏可能会回来咬你的错误路".

"you shouldn't be casting the return values of calloc(). Doing so can hide bugs with #include that could come back to bite you down the road".

?我使用 alloc 空间作为函数的参数,而不是返回值.

? I use the alloc space as parameter for a function, not as return value.

感谢您的帮助!

问候

推荐答案

您实际上并不是在创建一个 2D 数组,而是在创建一个指向像素数组的指针数组.这意味着您的第一个 calloc 调用应该为指针而不是像素分配足够的空间:

You're not really making a 2D array, you're making an array of pointers that point to arrays of pixels. That means your first calloc call should allocate enough space for pointers, not for pixels:

pixel **pixelvalue = calloc(imginformation.width, sizeof(pixel *));

你没有用语言标记你的问题,但假设它是 C(基于你的 typedef,这在 C++ 中是不必要的),你不应该转换返回值calloc().这样做可以隐藏 #include 的错误,这些错误可能会回来咬你.

You didn't tag your question with a language, but assuming it's C (based on your typedef, which wouldn't be necessary in C++), you shouldn't be casting the return values of calloc(). Doing so can hide bugs with #include that could come back to bite you down the road.

您问了几个后续问题.我想,第一个已经被其他几个答案很好地回答了,但我会尝试总结一下.您进行分配的方式是,您首先要分配一个指针数组 - 每个指针都将指向数组的一行.然后需要分配行本身 - 每个 pixel 对象的空间都放在那里,指向行的指针存储在第一个指针数组中.

You asked a couple of follow-up questions. The first has been answered pretty well by several other answers, I think, but I'll try to summarize. The way you're doing the allocation, you are first going to allocate an array of pointers - each of those pointers is going to point to one row of your array. The rows themselves then need to be allocated - space for each pixel object goes there, and the pointers to the rows are stored in the first array of pointers.

你的第二个问题,关于 calloc() 的返回值非常简单.如果您转换返回值,您可以对自己隐藏隐式声明错误.由于 calloc 的返回类型是 void *,如果你这样做:

Your second question, bout the return value from calloc() is pretty straightforward. If you cast the return value, you can hide implicit declaration bugs from yourself. Since the return type of calloc is void *, if you do something like:

my_ptr = calloc(1, 2);

一切正常.现在假设您没有包含 stdlib.h,因此在您的翻译单元中没有 calloc() 的原型.这将导致编译器假设 calloc() 的签名为 int calloc(int, int),这是不正确的.上面的同一行代码会向您发出关于假设该函数的默认签名的警告.使用您在代码中使用的类型转换将掩盖该警告,您可能永远不知道您错过了 #include 行.

Everything works nicely. Now imagine that you didn't include stdlib.h, and therefore had no prototype of calloc() in your translation unit. That would lead the compiler to assume the signature of calloc() to be int calloc(int, int), which isn't true. The same line of code above would throw you a warning about assuming a default signature for that function. Using a typecast like you have in your code will mask that warning and you might never know you were missing that #include line.

这篇关于写入二维数组时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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