动态分配的内存地址
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
&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.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.