c++ - 当指针作为指向另一个函数内的指针的指针传递时会发生什么?
问题描述
那么有人可以解释如果我通过 ...*p 作为 foo 函数的参数名称会发生什么
int main()
{
int i = 10;
int *const p = &i;
foo(&p);
printf("%d\n", *p);
}
void foo(int **p)
{
int j = 11;
*p = &j;
printf("%d\n", **p);
}
解决方案
Don't do that. You'll have a pointer to an undefined memory location on the stack. Any other function call between foo(&p);
and printf("%d\n", *p);
is bound to overwrite that memory location with new data.
推荐阅读
- javascript - 将 toStringTag 与 Object.defineProperties 一起使用时出现意外结果
- scala - 比较数组中的相同案例类数据
- python - 如何分离从python运行的linux命令
- r - 非准引用上下文中的 Tidyeval (psych::describeBy(group))
- azure-resource-manager - 如何获取存储容器的 resourceId?
- python - 使用 pymysql 连接到计算机上的本地 SQL 数据库
- java - 如何使用库基于 RC4 java 中的字符串创建密钥
- javascript - TypeError: navbarUserInfoComponent[0] 在 Jasmine 测试中未定义
- kentico - Kentico:如何在用户帐户停用时发送电子邮件?
- elasticsearch - 将后端侦听器添加到 JMeter 5.3 时出错