簡體   English   中英

C 2D陣列旋轉

[英]C 2D Array rotating

我有一個表示網格的2D指針設置,網格由包含1/0或空列的列組成(即在任何單元格中不包含1)。 此功能順時針旋轉網格90度,除了...

我認為我的malloc可能是錯誤的,因為它可以工作,但我在dmalloc中遇到了許多糾察隊列錯誤。

我分配了不正確的內存量嗎?

此外,我想交換* width和* height的值來表示網格的新寬度和高度,但是當我嘗試這個時,程序只是在第二次旋轉時出現了段錯誤。

再看一下旋轉網格的代碼。 我不認為你想要混合xy坐標,所以像*width - 1 - y這樣的索引看起來很可疑。 例如,假設*width = 3*height = 5 y范圍是0到4,你可以得到newg[3 - 1 - 4] = newg[-2]

另外,如果你以與分配newg相同的方式分配orig ,你需要像這樣釋放它:

for (x=0; x < *width; x++) {
  free (orig[x]);  // Free the individual columns
}
free (orig); // Free the array of pointers.

因此* width是orig的第一維的維度,因此它應該是newg的第二維的大小。

類似地,* height應該是newg的第一個大小,因此兩組malloc大小的翻轉方式錯誤。

我認為將值命名為orig_max_x和orig_max_y會更清楚,那么應該清楚函數是否以錯誤的方式使用值。

    newg = malloc (*height * sizeof(char *));

    // Initialise each column
    for (x = 0; x < *height; x++) {
        newg[x] = malloc (*width);
        for (y = 0; y < *width; y++)
            newg[x][y] = 0;
    }

此外,如果你想從spin()返回值,它不應該釋放任何 newg的存儲空間

編輯:我仍然有一些討厭的*寬度和*高度混合。 抱歉。 我強烈建議名稱應該與他們談論的內容有關,orig_width,orig_height會幫助我閱讀代碼。

這可能就是我這樣做的方式:

#include <stdio.h>
#include <stdlib.h>

char** alloc_rectangle(int *width, int *height);
void free_rectangle(char **orig, int *width);
char** spin (char **orig, int *width, int *height);

int main (int argc, const char * argv[]) {
    int width = 20;
    int height = 30;

    char** orig = alloc_rectangle(&width, &height);
    char** newg = spin(orig, &width, &height);


    return 0;
}

char** alloc_rectangle(int *width, int *height) 
{
    char **newg = calloc (*width, sizeof(char *));

    // Initialise each column
    for (int x = 0; x < *width; x++) {
        newg[x] = calloc (*height, sizeof(char));
    }
    return newg;
}

void free_rectangle(char **orig, int *width)
{
    // free memory for old grid
    for (int x = 0; x < *width; x++) {
        if (orig[x] != NULL) {
            free (orig[x]);
        }
    }

    free (orig);
}

char** spin (char **orig, int *width, int *height) 
{
    int x;
    int y;

    char **newg = alloc_rectangle(height, width);

    // Rotate
    for (x = 0; x < *width; x++) {
        for (y = 0; y < *height; y++)
            if (orig[x] != NULL) 
                newg[*height - 1 - y][x] = orig[x][y];
    }

    return newg;
}

警告未經測試的代碼 - 一切有趣:-)

我不認為這是自由的起作用。 我更喜歡它只是騰出空間來保持旋轉的結果。 因此,為了使事情更整潔,我將一個矩形釋放到它自己的功能中。 同樣,我總是希望一致地分配矩形,這將是它自己的功能。

我剛剛寫了這篇文章,它似乎與我運行的幾個測試工作正常。

char **rotate(char **original, int *width, int *height)
{
    int t_width = *height;
    int t_height = *width;

    char **newgrid = (char**)calloc(t_height, sizeof(char*));

    for(int y = 0; y < t_height; y++)
    {
        newgrid[y] = (char*)calloc(t_width, sizeof(char));
        for(int x = 0; x < t_width; x++)
            newgrid[y][x] = original[x][y];         
    }

    for(int y = 0; y < *height; y++)
        free(original[y]);
    free(original);

    *width = t_width;
    *height = t_height;

    return newgrid;
}

如果有任何問題,請告訴我。

暫無
暫無

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

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