【问题标题】:Data corruption of local variable when passing to another function传递给另一个函数时局部变量的数据损坏
【发布时间】:2013-05-30 09:26:35
【问题描述】:

将堆栈上的局部变量传递给函数调用时,如何可能导致数据损坏?

{
    int Array[4];

    Array[0] = 1;
    Array[1] = 2;
    Array[2] = 3;
    Array[3] = 4;

    test_struct->arr = Array;
    //(Where arr is a pointer to array)
    Demo(test_struct);
}

如果在这里将 Array 传递给另一个函数会出现什么问题?有没有可能 数据损坏?

【问题讨论】:

  • “数据损坏”是什么意思?能否给我们介绍一下test_struct的定义和Demo的实现?
  • 请发布更多代码。另外,这种“数据损坏”发生在哪里?

标签: c corruption


【解决方案1】:

好吧,如果Demo() 有错误导致它在Array 的范围之外写入,它对此一无所知,它当然会破坏内存,是的。

请注意,您不能在 Demo() 中使用 sizeof 来计算大小,您必须有一个单独的参数(或结构成员)来告知可用元素的数量。

【讨论】:

    猜你喜欢
    • 2014-01-12
    • 2012-05-21
    • 2014-04-02
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多