Curiosity Corner: Allocating Empty Memory in C
In the realm of C programming, an intriguing question arises: Can we allocate memory for an empty block, represented by new int[0].
Observations from a Test Application
A simple test application that prints the result of new int[0] reveals an output of 0x876c0b8, suggesting that memory is indeed being allocated.
Diving into the Standard
The C Standard provides guidance on this matter:
Additional Considerations
Foot-Note Insights
An interesting foot-note from 3.7.3.1/2 reveals that the intention is to enable operator new() implementation using either malloc() or calloc(), which behave similarly with regards to zero-size requests. However, C differs from C by requiring a zero-size request to return a non-null pointer.
The above is the detailed content of Can C `new int[0]` Allocate Empty Memory, and If So, What Are the Implications?. For more information, please follow other related articles on the PHP Chinese website!