【发布时间】:2016-08-23 21:05:45
【问题描述】:
我正在学习 C++,我知道“新”关键字用于将内存中的地址分配给指针。而且我认为在使用“nullptr”时会初始化一个指向任何内容的指针。那是对的吗?参考示例:
//using nullptr
int *x = nullptr; //this is just a pointer that points to nothing and
//will need to be initialized with an address before it
//it can be used. Correct?
//using new
int *x = new int; //this is basically giving x an address in memory. Will the
//address have some residual value stored in it or will
//it contain zero?
您什么时候会使用其中一种? new 仅用于动态内存分配还是有其他应用程序?如果您可以声明一个指向 nullptr 的指针,然后稍后再对其进行初始化,为什么还要初始化它呢?
感谢您的帮助!
【问题讨论】:
标签: c++ pointers new-operator nullptr