尝试在C中复制4字符结构的2D数组时出现memcpy错误分段错误

尝试在C中复制4字符结构的2D数组时出现memcpy错误分段错误,c,multidimensional-array,segmentation-fault,memcpy,C,Multidimensional Array,Segmentation Fault,Memcpy,嘿,我正在尝试复制一个SDL_颜色数组,该数组是从另一个图像中创建的。但对于一些图像,我得到: 流程结束,退出代码为1073741819(0xC0000005) 它适用于20 x 20像素的图像,但适用于50 x 50像素的图像。。。 这是我的密码: FILE *debugFile = fopen("C:\\Users\\Clement\\Documents\\coding\\ImageOfCLife\\debug.txt", "w+"); int img

嘿,我正在尝试复制一个SDL_颜色数组,该数组是从另一个图像中创建的。但对于一些图像,我得到:

流程结束,退出代码为1073741819(0xC0000005)

它适用于20 x 20像素的图像,但适用于50 x 50像素的图像。。。 这是我的密码:

FILE *debugFile = fopen("C:\\Users\\Clement\\Documents\\coding\\ImageOfCLife\\debug.txt", "w+");
int imgWidth, imgHeight, channels;
unsigned char *img = stbi_load("C:\\Users\\Clement\\Documents\\coding\\ImageOfCLife\\star.jpg", &imgWidth,
                               &imgHeight, &channels, 0);
fprintf(debugFile, "Loaded image with a width of %dpx, a imgHeight of %dpx and %d channels\n", imgWidth, imgHeight, channels);
dRulesLen = sizeof(deathRules);
bRulesLen = sizeof(birthRules);
if (img == NULL) {
    fprintf(debugFile, "Error in loading the image\n");
    exit(3);
}

int ch, pix;
SDL_Color **stateMatrix1 = (SDL_Color **) malloc(imgHeight * sizeof(SDL_Color*));
if (stateMatrix1 == NULL) {
    fprintf(debugFile,"Unable to allocate memory\n");
    exit(1);
}
for (int i = 0; i < imgHeight; ++i) {
    stateMatrix1[i] = (SDL_Color *) malloc(imgWidth * sizeof(SDL_Color));
}
for (ch = 0; ch < imgHeight; ch++) {
    printf("{");
    for (pix = 0; pix < imgWidth; pix++) {
        unsigned bytePerSDL_Color = channels;
        unsigned char *SDL_ColorOffset = img + (pix + imgHeight * ch) * bytePerSDL_Color;
        SDL_Color p = initSDL_Color(SDL_ColorOffset);
        stateMatrix1[ch][pix] = p;
        printSDL_Color(p);
        printf(", ");
    }
    printf("}\n");
}
SDL_Color stateMatrix2[imgHeight][imgWidth];
memcpy(stateMatrix2, stateMatrix1, imgWidth*imgHeight*sizeof(SDL_Color)); 
但我也有同样的问题

我忘了说,但我希望ant能够将这两种状态矩阵都用作函数的参数

为了解决这个问题,我使用了下面解释的Olaf解决方案: 我保持:

SDL\u Color**stateMatrix1=(SDL\u Color**)malloc(imghight*sizeof(SDL\u Color*));
if(stateMatrix1==NULL){
fprintf(调试文件,“无法分配内存\n”);
出口(1);
}
对于(int i=0;i
要将内存分配给矩阵和已用内存,请执行以下操作:

for (int i = 0; i < imgHeight; ++i) {
memcpy(stateMatrix2[i], stateMatrix1[i], imgWidth * sizeof(SDL_Color));
}
for(int i=0;i
执行复制。
我还验证了两个矩阵没有链接,没有问题。

您的错误在评论中解释。您可以通过为矩阵分配一个连续的内存块来修复它,而不是像您那样分配许多块。对于分配、解除分配和复制,这种方法更简单(因为您可以使用对memcpy的单个调用):

#包括
#包括
结构SDL_颜色{
无符号字符rgba[4];
};
int main(){
int-ch,pix;
int imgWidth=100;
int imgHeight=100;
结构SDL_颜色(*stateMatrix1)[imgWidth][imgHeight]=malloc(sizeof(*stateMatrix1));
如果(*stateMatrix1==NULL){
printf(“无法分配内存\n”);
}否则{
自由(*1);
printf(“成功”\n);
}
返回0;
}
每次使用矩阵时,您只需注意取消对矩阵指针的引用。

复制时

memcpy(stateMatrix2, stateMatrix1, imgWidth * imgHeight * sizeof(SDL_Color));
您将超越
状态矩阵1
的结尾,它不是
imgWidth*imgHeight*sizeof(SDL\u Color)
,而是
imgHeight*sizeof(SDL\u Color*)


在循环中从
statemartrix1
复制到
statemartrix2
是解决此问题的一种方法

for (int i = 0; i < imgHeight; ++i) {
    memcpy(stateMatrix2[i], stateMatrix1[i], imgWidth * sizeof(SDL_Color));
}
以后使用与上述相同的
memcpy
,您仍将超越
statemartix1
,现在也将超越
statemartix2
的末尾


正确的复制方式(但由于另一个原因仍然是错误的)是

就大小而言,这是正确的,但仍然是错误的,因为它将
statemartrix1
的指针复制到
statemartrix2
。这有两个效果

  • 当您使用自己的指针初始化stateMatrix2时,将出现内存泄漏
  • 现在两个矩阵都指向同一个内存,这意味着改变一个,也会改变另一个

  • stateMatrix1
    不是一个连续的内存块。它由许多独立的
    malloc
    内存块组成,因此不能用单个
    memcpy
    进行复制
    stateMatrix2
    是一个二维数组,但
    stateMatrix1
    是一个指针数组,指向不同类型的
    SDL\u Color
    ->数组。@kaylum oh所以解决方案应该是使用
    memcpy(stateMatrix2[i],stateMatrix1[i],imgWidth*sizeof(SDL\u Color)循环
    ?@OlafDietsche所以我必须以与矩阵1相同的方式声明matrix2?在C中,没有必要强制转换
    malloc
    的返回值,这是不必要的。请参阅:问题是我必须将这些2D数组作为参数传递到函数中。为了实现它,我必须将stateMatrix构建为指针的指针,对吗?除非
    imgWidth
    imgHeight
    是常量文本,最好为每行分配指针和存储,而不是使用VLA(因为对VLA的支持是可选的,从C11开始)。如果您可以选择使用单个块模拟2D数组,这对于单分配/单自由的简单性来说总是首选。如果SDL函数需要指向指针的指针,那么这就是您需要的。此处
    (*stateMatrix1)[imgWidth][imgHeight]
    是指向-2D-VLA的指针。我通过编辑我的问题,使用您的答案发布了我的修复方案,如果您对此有任何其他意见,请告诉我。再次感谢!
    #include <stdio.h>
    #include <stdlib.h>
    
    struct SDL_Color {
        unsigned char rgba[4];
    };
    
    int main() {
        int ch, pix;
        int imgWidth = 100;
        int imgHeight = 100;
        struct SDL_Color (*stateMatrix1)[imgWidth][imgHeight] = malloc(sizeof(*stateMatrix1));
        if (*stateMatrix1 == NULL) {
            printf("Unable to allocate memory\n");
        } else {
            free(*stateMatrix1);
            printf("Success\n");
        }
        return 0;
    }
    
    memcpy(stateMatrix2, stateMatrix1, imgWidth * imgHeight * sizeof(SDL_Color));
    
    for (int i = 0; i < imgHeight; ++i) {
        memcpy(stateMatrix2[i], stateMatrix1[i], imgWidth * sizeof(SDL_Color));
    }
    
    SDL_Color **stateMatrix2 = (SDL_Color **) malloc(imgHeight * sizeof(SDL_Color*));
    
    memcpy(stateMatrix2, stateMatrix1, sizeof(*stateMatrix1));