c++ 的初始化堆对象

发布于 2025-01-02 01:28:58 字数 374 浏览 1 评论 0原文

我想知道用 new 在堆上创建的对象中的内置类型是否会初始化为零?它是标准强制的还是编译器特定的?

给出以下代码:

#include <iostream>

using namespace std;

struct test
{
    int _tab[1024];
};

int main()
{
    test *p(new test);

    for (int i = 0; i < 1024; i++)
    {
        cout << p->_tab[i] << endl;
    }

    delete p;
    return 0;
}

运行时,它会打印全零。

I'am wondering if built-in types in objects created on heap with new will be initialized to zero? Is it mandated by the standard or is it compiler specific?

Given the following code:

#include <iostream>

using namespace std;

struct test
{
    int _tab[1024];
};

int main()
{
    test *p(new test);

    for (int i = 0; i < 1024; i++)
    {
        cout << p->_tab[i] << endl;
    }

    delete p;
    return 0;
}

When run, it prints all zeros.

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

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

发布评论

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

评论(4

仄言 2025-01-09 01:28:58

您可以选择是否要默认初始化,这使基本类型(以及一般的POD类型)未初始化,或者值初始化,这将基本类型(以及POD类型)零初始化) 类型。

int * garbage = new int[10];    // No initialisation
int * zero    = new int[10]();  // Initialised to zero.

这是由标准定义的。

You can choose whether you want default-initialisation, which leaves fundamental types (and POD types in general) uninitialised, or value-initialisation, which zero-initialises fundamental (and POD) types.

int * garbage = new int[10];    // No initialisation
int * zero    = new int[10]();  // Initialised to zero.

This is defined by the standard.

人心善变 2025-01-09 01:28:58

不,如果您执行以下操作:

int *p = new int;

char *p = new char[20];  // array of 20 bytes

struct Point { int x; int y; };
Point *p = new Point;

p 指向的内存将具有不确定/未初始化的值。

但是,如果您执行以下操作:

std::string *pstring = new std::string();

那么您可以放心,该字符串将被初始化为空字符串,但这是因为类构造函数的工作方式,而不是因为有关堆分配的任何保证。

No, if you do something like this:

int *p = new int;

or

char *p = new char[20];  // array of 20 bytes

or

struct Point { int x; int y; };
Point *p = new Point;

then the memory pointed to by p will have indeterminate/uninitialized values.

However, if you do something like this:

std::string *pstring = new std::string();

Then you can be assured that the string will have been initialized as an empty string, but that is because of how class constructors work, not because of any guarantees about heap allocation.

那伤。 2025-01-09 01:28:58

标准没有强制要求。基本类型成员的内存可以包含最后留在内存中的任何值。

我猜有些编译器可能会选择初始化字节。许多都是在代码的调试版本中进行的。它们分配一些已知的字节序列,以便在调试时提示您内存未被程序代码初始化。

It's not mandated by the standard. The memory for the primitive type members may contain any value that was last left in memory.

Some compilers I guess may choose to initialize the bytes. Many do in debug builds of code. They assign some known byte sequence to give you a hint when debugging that the memory wasn't initialized by your program code.

一指流沙 2025-01-09 01:28:58

使用 calloc 将返回初始化为 0 的字节,但这不是特定于标准的。 calloc 自 C 以来就与 malloc 一起出现。但是,您将为使用 calloc 付出运行时开销。

前面给出的关于使用 std::string 的建议非常合理,因为毕竟,您正在使用 std,并获得类构造/析构行为的好处。换句话说,您需要担心的事情(例如数据初始化)越少,出错的可能性就越小。

Using calloc will return bytes initialized to 0, but that's not standard-specific. calloc as been around since C along with malloc. However, you will pay a run-time overhead for using calloc.

The advice given previously about using the std::string is quite sound, because after all, you're using the std, and getting the benefits of class construction/destruction behaviour. In other words, the less you have to worry about, like initialization of data, the less that can go wrong.

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