Properties of a pointer to a zero length array

强颜欢笑 提交于 2020-02-29 12:50:24

问题


Consider

int main()
{
    auto a = new int[0];
    delete[] a; // So there's no memory leak
}

Between the copy initialisation and deletion, are you allowed to read the pointer at a + 1?

Furthermore, does the language permit the compiler to set a to nullptr?


回答1:


Per recent CWG reflector discussion as a result of editorial issue 3178, new int[0] produces what is currently called a "past-the-end" pointer value.

It follows that a cannot be null, and a + 1 is undefined by [expr.add]/4.



来源:https://stackoverflow.com/questions/60442205/properties-of-a-pointer-to-a-zero-length-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!