代码之家  ›  专栏  ›  技术社区  ›  Bwebb

如何在C中将数组的一维记忆为零

  •  2
  • Bwebb  · 技术社区  · 7 年前

    假设我有一个全局(在数据段中)声明的3D数组,我想将它的1d memset为0。

    int multi_dimension_array[x][y][z];
    

    我可以用这句话来回忆整件事:

    memset(multi_dimension_array, 0, sizeof(multi_dimension_array));
    

    但是现在假设我只想为某个值memset x维(比如2),这意味着多维数组[2][0][0]到多维数组[2][Y-1][Z-1]的值都应该为零。我不认为有一个聪明的方法使用记忆集为Y或Z,因为他们不是连续的。以下行应该有效:

    memset(&multi_dimension_array[2][0][0], 0, sizeof(multi_dimension_array[2][0][0]) * y * z);
    

    我的“问题”是我不喜欢memset参数的*y*z部分。数组中是否有表示size of(multi_dimension_array[2])==byte_size_of_type*y*z的内容?

    我想使用数组的一个属性,在这个示例中,sizeof将计算为“x”维度的正确字节数。我不想在有人更改声明中的大小并且他们不更改此memset时使用*y*z,另外我不喜欢它的外观。

    2 回复  |  直到 7 年前
        1
  •  6
  •   Eric Postpischil    7 年前

    memset(&multi_dimension_array[2], 0, sizeof multi_dimension_array[2]);

    这需要 multi_dimension_array[i] 是数组的数组,而不是指针。因为,当 sizeof 应用于数组,它返回数组的大小。数组不会像在大多数表达式中那样自动转换为指针。

    当然,它只适用于第一个维度(如果您使用多个维度,则为前几个维度)。例如,您可以使用一个memset array[i] array[i][j] 但不包括中间尺寸,如 array[???][j] .

        2
  •  0
  •   Bwebb    7 年前

    对于这个问题的未来读者,EricPostischil的回答是正确的,我接受了它,因为它是正确的。下面是一个示例程序和输出,演示应用于多维数组的sizeof运算符/函数。 通常,如果声明为整数数组

    int mda[x][y][z]; 
    then mda[a][b][c] == *(mda + ((a *( y* z)) + (b*z) + (c))
    

    最重要的是,至少对我最初提出的问题来说,索引到比用程序声明的“维数”更少的数组仍然会编译并维护下一个数组的“sizeof”。

    mda[a] == *(mda + (a * (y*z))) AND sizeof(mda[a]) == sizeof(array_type) * y * z
    mda[a][b] == *(mda + (a * (y*z)) + (b * z)) AND sizeof(mda[a][b]) == sizeof(array_type) * z
    

    好的,下面是一个示例程序,您可以在联机IDE中运行并验证:

    #include <stdio.h>
    
    #define X (4)
    #define Y (10)
    #define Z (5)
    int multi_dimension_array[X][Y][Z];
    
    void print_array(){
        for(int i=0; i<X; i++){
            printf("THIS IS THE %dth value of X\n", i);
            for(int j=0; j<Y; j++){
                for(int k=0; k<Z; k++){
                    printf("%d ", multi_dimension_array[i][j][k]);
                }
                printf("\n");
            }
        }
    }
    
    int main(void) {
        printf("%d %d %d %d\n", sizeof(multi_dimension_array[0][0][0]), sizeof(multi_dimension_array[0][0]), sizeof(multi_dimension_array[0]), sizeof(multi_dimension_array));
    
        memset(multi_dimension_array,0x01,sizeof(multi_dimension_array));
        print_array();
    
        memset(&multi_dimension_array[2],0x0,sizeof(multi_dimension_array[2]));
    
        printf("\n\n\nNEXT MEMSET with the X=2 zeroed out\n\n\n");
        print_array();
    
        return 0;
    }
    

    下面是程序输出:

    4 20 200 800
    THIS IS THE 0th value of X
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    THIS IS THE 1th value of X
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    THIS IS THE 2th value of X
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    THIS IS THE 3th value of X
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    
    
    
    NEXT MEMSET with the X=2 zeroed out
    
    
    THIS IS THE 0th value of X
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    THIS IS THE 1th value of X
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    THIS IS THE 2th value of X
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    THIS IS THE 3th value of X
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    16843009 16843009 16843009 16843009 16843009 
    

    *请注意,memset将每个字节设置为第二个参数的值,在本例中是0x01,这使得4个字节的ints 0x0101010101==16843009。

    推荐文章