簡體   English   中英

嘗試在 C 中復制 4 個字符結構的二維數組時出現 memcpy 錯誤分段錯誤

[英]memcpy error segmentation fault while trying to copy 2D array of 4 char structure in C

嘿,我正在嘗試復制從另一個圖像中創建的 SDL_Color 數組。 但是對於某些圖像,我得到:

進程以退出代碼 -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)); 

最后一行是根據我試過的調試器的問題

memcpy(stateMatrix2, stateMatrix1, sizeof(stateMatrix2))

也是,但我得到了相同的結果。

我與 minGW 和 Clion 一起在 windows 10 上工作。 我希望你能幫助我解決這個問題。

我也嘗試替換SDL_Color stateMatrix2[imgHeight][imgWidth]; 經過:

    SDL_Color **stateMatrix2 = (SDL_Color **) malloc(imgHeight * sizeof(SDL_Color*));
if (stateMatrix2 == NULL) {
    fprintf(debugFile,"Unable to allocate memory\n");
    exit(1);
}
for (int i = 0; i < imgHeight; ++i) {
    stateMatrix2[i] = (SDL_Color *) malloc(imgWidth * sizeof(SDL_Color));
}

但我遇到了同樣的問題。

我忘了說,但我希望 ant 能夠使用兩個 stateMatrix 作為 function 的參數。

為了解決這個問題,我使用了下面解釋的 Olaf 解決方案:我保留了:

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));
}

將 memory 分配給矩陣和使用:

for (int i = 0; i < imgHeight; ++i) {
memcpy(stateMatrix2[i], stateMatrix1[i], imgWidth * sizeof(SDL_Color));
}

執行復制。 我還驗證了這兩個矩陣沒有鏈接,沒有問題。

復印時

memcpy(stateMatrix2, stateMatrix1, imgWidth * imgHeight * sizeof(SDL_Color));

您將 go 超出stateMatrix1的末尾,這不是imgWidth * imgHeight * sizeof(SDL_Color) ,而是imgHeight * sizeof(SDL_Color*)


stateMatrix1循環復制到stateMatrix2是解決此問題的一種方法

for (int i = 0; i < imgHeight; ++i) {
    memcpy(stateMatrix2[i], stateMatrix1[i], imgWidth * sizeof(SDL_Color));
}

另一種方法是將兩個矩陣設為同一類型。 但是當將stateMatrix2分配為

SDL_Color **stateMatrix2 = (SDL_Color **) malloc(imgHeight * sizeof(SDL_Color*));

稍后使用與上面相同的memcpy ,您仍然會 go 超出stateMatrix1 現在也超出stateMatrix2的末尾。


正確的復制方法(但由於另一個原因仍然是錯誤的)是

memcpy(stateMatrix2, stateMatrix1, sizeof(*stateMatrix1));

這在大小方面是正確的,但仍然是錯誤的,因為它將stateMatrix1指針復制到stateMatrix2 這有兩個效果

  1. 當您使用自己的指針初始化stateMatrix2時,將會出現 memory 泄漏。
  2. 現在兩個矩陣都指向同一個 memory,這意味着改變一個,也會改變另一個。

您的錯誤在評論中解釋。 您可以通過為您的矩陣分配一個連續的 memory 塊而不是像您所做的許多塊來修復它。 對於分配、解除分配和復制,這種做法更加簡單(因為您可以使用一次對 memcpy 的調用):

#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;
}

您只需要在每次使用它時取消對矩陣的指針的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM