【问题标题】:How am I being able to access function stack memory after returning? [duplicate]返回后如何访问函数堆栈内存? [复制]
【发布时间】:2022-01-14 20:22:56
【问题描述】:

这是我的代码:

使用命名空间标准;

// 如果内存是静态分配的,那么在函数返回且堆栈消失后,您将无法访问内存地址。

int* function(int x)
 {
  int *x_ptr = &x;
  return x_ptr;
 }

int main()
{
 int x=10;
 int *x_ptr = function(x);
 cout<<*x_ptr<<endl;
}

【问题讨论】:

    标签: c++ heap-memory stack-memory


    【解决方案1】:

    函数返回后不能访问内存地址

    正确。

    程序的行为未定义。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2014-12-23
      • 2011-03-16
      • 2019-10-16
      • 1970-01-01
      • 2019-07-05
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多