当作为另一个数组的索引传递时更改数组元素

标签 c arrays immutability

我是菜鸟,希望这个问题不要幼稚!

我有以下函数,其中我使用一个数组的元素作为另一个数组的索引。然而,尽管我没有对前者进行任何更改,但我看到元素正在被修改。代码如下:

void convert_to_bitmap(int n_shapes, int sizex, int sizey,
                       int ll_x[n_shapes], int ll_y[n_shapes],
                       int ur_x[n_shapes], int ur_y[n_shapes],
                       int shapes_ll_bitmap[sizex][sizey],
                       int shapes_ur_bitmap[sizex][sizey] )
{
    int i;

    for (i = 0; i < n_shapes; i++)
    {
        printf("%d, %d, %d, %d check1\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);

    }

    for (i = 0; i < n_shapes; i++)
    {
        shapes_ll_bitmap[ll_x[i]][ll_y[i]] = 1;
        shapes_ur_bitmap[ur_x[i]][ur_y[i]] = 1;

        printf("%d, %d, %d, %d check2\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
    }
}

而且,当我这样做时,输出显示第一个数组已经改变。有什么方法可以让它保持不变吗?

输出:

0, 0, 0, 7 check1
0, 9, 0, 15 check1
1, 0, 1, 7 check1
1, 9, 1, 15 check1
2, 13, 2, 15 check1
2, 17, 2, 24 check1
2, 26, 2, 32 check1
0, 0, 0, 7 check2
0, 9, 0, 15 check2
1, 0, 1, 7 check2
1, 9, 1, 15 check2
1, 13, 2, 15 check2
2, 1, 2, 1 check2
1, 26, 2, 32 check2

这是我在 main() 中调用函数的方式:

convert_to_bitmap(n_shapes, sizex, sizey, ll_x, ll_y, ur_x, ur_y, shapes_ll_bitmap, shapes_ur_bitmap);

而int main()中矩阵的声明和初始化如下:

int ll_x[n_shapes];
int ll_y[n_shapes];
int ur_x[n_shapes];
int ur_y[n_shapes];

int sizex;
int sizey;

int shapes_ll_bitmap[sizex][sizey];
int shapes_ur_bitmap[sizex][sizey]; 

for (i=0; i < sizex; i++)
{
    for (j = 0; j < sizey; j++)
    {
        shapes_ll_bitmap[i][j] = 0;
        shapes_ur_bitmap[i][j] = 0;
    }
    printf("\n");
}

谢谢!

编辑:

这是一些独立的代码:

int main(void)
{   
    enum { MAX_SHAPES = 100000 };
    struct Rectangle rect_array[MAX_SHAPES];
    int n_shapes = read_shapes_rpt("shapes.rpt", MAX_SHAPES, rect_array);
    int i, j;

    float pitch_x = 0.044;
    float pitch_y = 0.042;

    float ll_x_flt[n_shapes];
    float ll_y_flt[n_shapes];
    float ur_x_flt[n_shapes];
    float ur_y_flt[n_shapes];

    int ll_x[n_shapes];
    int ll_y[n_shapes];
    int ur_x[n_shapes];
    int ur_y[n_shapes];

    int sizex;
    int sizey;

    int shapes_ll_bitmap[sizex][sizey];
    int shapes_ur_bitmap[sizex][sizey]; 

    for (i=0; i < sizex; i++)
    {
        for (j = 0; j < sizey; j++)
        {
            shapes_ll_bitmap[i][j] = 0;
            shapes_ur_bitmap[i][j] = 0;
        }
        printf("\n");
    }

    if (n_shapes > 0)
    {
        transform_to_shape_bit_locations(n_shapes, rect_array, ll_x_flt, ll_y_flt, ur_x_flt, ur_y_flt, ll_x, ll_y, ur_x, ur_y, &pitch_x, &pitch_y, &sizex, &sizey);

        convert_to_bitmap(n_shapes, sizex, sizey, ll_x, ll_y, ur_x, ur_y, shapes_ll_bitmap, shapes_ur_bitmap);

        printf("%d\n%d\n%d\n", n_shapes, sizex, sizey);
        /* Use the shapes that were read */
    }



    return 0;
}

我的 shapes.rpt 文件包含以下 csv 值:

1.408,529.237,1.43,529.523
1.408,529.597,1.43,529.883
1.452,529.237,1.474,529.523
1.452,529.597,1.474,529.883
1.496,529.777,1.518,529.883
1.496,529.957,1.518,530.243
1.496,530.317,1.518,530.564

最佳答案

您的变量 sizexsizey 未初始化。这意味着您的矩阵 shapes_ll_bitmapshapes_ur_bitmap 具有不可预测的大小(行为实际上是未定义的)。

请注意,当您稍后实际为您的 sizexsizey 分配有意义的值时,已经太迟了。矩阵已经用不确定 大小声明,这是最终的。声明矩阵后,对 sizexsizey 值的任何更改都不会影响矩阵。

您的矩阵最终具有一些不确定的大小,这会导致在 convert_to_bitmap 函数内进行越界访问并破坏不相关的内存值。

关于当作为另一个数组的索引传递时更改数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31793337/

相关文章:

c++ - 数组的非常量声明

python - 为什么可变变量不能在没有链接的情况下一起分配?

javascript - 具有多个更改的扩展属性 : one-liner ecma immutability

java - 可变类作为不可变类的子类

c - 为什么使用 float 时会打印这种格式化输出?

c - 结构输出被最后的用户输入覆盖

c - 外部变量 C

c++ - 在 C++ 中保存和加载大数组

c - "expected ' 字符 * ' but argument is of type ' 字符 ' "- 回文 + 反向

c++ - 将字符串数组转换为整数数组