c++ - 动态分配中的内存地址

标签 c++ memory-management

#include <iostream>

int main()
{
  int anything[] = {5};
  int *something = new int;
  *something = 5;

  std::cout << &anything  << "==" << &anything[0]  << "==" << anything  << std::endl;
  std::cout << &something << "!=" << &something[0] << "==" << something << std::endl;
}

为什么&something中的内存地址与&something[0]something不同?虽然是动态分配,但是不明白为什么内存地址不一样。我尝试了不止一个值;这是同一件事。为简单起见,我在这里对两者都使用了一个值。

最佳答案

&something 是指针本身的内存地址(嘿,它需要将该值存储在某处!),而 &something[0] 是实际指针的地址存储你的东西的内存。

关于c++ - 动态分配中的内存地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7642865/

相关文章:

c++ - Qt + iperf3 = lconv 未声明

c++ - C++11 和 C++14 中的 constexpr(与 const 关键字没有区别)

android - Drawable 优于 android 中内存位图的优势

c++ - 我应该使用跨平台 GUI 工具包还是依赖 native 工具包?

c++ - c linux 和 windows 中的键盘扫描代码

delphi - 我可以使用 TVirtualMethodInterceptor 实现实例计数监控吗?

c - 使用 strcpy 将字符串复制到使用 atoi 检索到的索引处的元素中

ios - 具有可选 IBOutlet 的属性多态性

c++ - 带有数组的 boost::ptr_vector:我可以确定它的析构函数调用 delete[] 而不是 delete 吗?

c++ - 访问对象层次结构的子集(不是子树)