Does "new int[0]" allocate memory?

Asked 2 years ago, Updated 2 years ago, 77 views

On my computer, cout << new int[0] << endl; When I ran , I printed 0x876c0b8.

I think I'm allocating memory Assigning [0] means assigning empty. Personally, I thought there would be an error, but unexpectedly, the memory is allocated. Does this apply to all compilers? Is it C++ standard?

c++ memory-management new-operator standard-compilance

2022-09-21 19:51

1 Answers

C++ Standard

According to 5.3.4/7,

When assigned as new int[0], assign an array without elements, and

According to 3.7.3.1/2,

For these pointers, the size of the object the pointer points to is not determined Sometimes requests such as new int[0] can fail

accordingly new int[0]The result of writing together is It depends on the platform.

I looked it up to see if there's something similar in C C++'s new operation is similar to C's malloc() or calloc() The results are different when 0 is assigned, so there is no fixed standard in C.


2022-09-21 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.