如何保存指针最初指向的原始值
How to save the original value of what pointer initially points to
新程序员在这里...任何帮助将不胜感激...
在下面的示例中,我将如何在 : 0x4000000 处保留原始值,以便一旦我这样做:(整数((0x4000000( = 900;我仍然可以拥有在我将其更改为 900 之前处于 0x4000000 的原始值或我决定在那里设置的任何值......
int apples = *(int*)(0x4000000); // lets say that after doing this apples is assigned the value of 10(meaning 10 apples)
// now how do i backup this original of value 10...
*(int*)(0x4000000) = 900; // set the apples to 900 and go on...
不,在你编码之后 apples
== 10 和 *(int*)(0x4000000)
== 900
如果你做过
int *apples = (int*)(0x4000000);
那么最后*apples
会有 900 个。 你可以通过做来保存 10 个
int apples_save = *apples; .
喜欢这个
int *apples = (int*)(0x4000000);
int apples_save = *apples; .
*(int*)(0x4000000) = 900;
现在*apples
== 900 和 apples_save
== 10 .
相关文章:
- 从堆栈分配的原始指针构造智能指针
- 将unique_ptr分配给原始指针
- 如何将唯一指针的 std::vector 转换为原始指针的 std::span?
- <Base> <Derived> 具有相同原始指针共享引用的 shared_ptr 和 shared_ptr 实例是否计数?
- C++原始指针和"delete"
- 为什么 C++ 地址中的矢量无法通过原始指针访问
- 如何正确实现具有原始指针的类的复制构造函数?
- 如何在将原始指针移动到基类构造函数之前从unique_ptr中提取原始指针
- 为包含原始指针的对象C++智能指针
- c++:复制、删除和运算符=在原始指针映射中
- C++模板,用于通过常量引用和原始指针传递向量
- C++为什么原始指针不会增加shared_ptr的引用计数?
- 从shared_ptr获取原始指针以将其传递给需要 raw 的函数
- 如何包装多级原始指针以赋予其容器语义
- 为什么我可以通过原始指针而不是shared_ptr来修改对象
- 将原始指针传递给接受unique_ptr作为参数的函数
- 为什么我们不允许将纯引用参数传递给 std::thread,但允许传递原始指针?
- 更改保留指向其字段的原始指针的对象地址
- 从原始指针(衰减的 C 样式数组)和大小生成范围::视图
- 如何在C++代码中灵活使用和替换标准::shared_ptr或标准::unique_ptr或原始指针?