动态分配的内存地址

发布于 2024-12-08 02:32:51 字数 558 浏览 0 评论 0原文

#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不同?虽然是动态分配,但是不明白为什么内存地址不一样。我尝试了多个值;这是同一件事。为了简单起见,这里我对两者都使用一个值。

#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;
}

Why is the memory address in &something different from &something[0] and something? Although it is a dynamic allocation, I don't understand why the memory address is different. I tried it with more than one value; it's the same thing. Here I used one value for both for simplicity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝礼 2024-12-15 02:32:51

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

&something is the memory address of the pointer itself (hey, it needs to store that value somewhere!), while &something[0] is the address of the actual memory that is storing your stuff.

时间你老了 2024-12-15 02:32:51

something 是一个指针。 &something 是该指针的地址。 &something[0]是指针指向的第一个元素的地址,与指针的地址完全不同。 something 是指针的值,也是所指向元素的地址。

我确信这个话题之前已经被讨论过很多次了,我希望我做得公正。

something is a pointer. &something is the address of that pointer. &something[0] is the address of the first element pointed to by the pointer, which is completely different from the address of the pointer. something is the value of the pointer, which is also the address of the element that is pointed to.

I'm sure this topic has been covered many times before, I hope I did it justice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文