删除类对象数组?
几乎众所周知,下面的代码正确地释放了 100 个整数的内存。
int* ip = new int[100];
delete [] ip;
我认为即使对于用户定义的类它也有效:
Node* ip = new Node[100];
delete [] ip;
在第一种情况下,要释放的内存大小(400 字节)是在编译时确定的吗?基本上,内部发生了什么?
在第二种情况下,
Node
的析构函数会在 100 个对象中的每一个上调用吗?
本质上,我一直在使用这种语法,但从未理解内部发生了什么,现在我很好奇。
It's almost common knowledge that the code below correctly frees the memory of 100 integers.
int* ip = new int[100];
delete [] ip;
And I think even for user defined classes it works:
Node* ip = new Node[100];
delete [] ip;
In the first case, is the size of memory to be freed (400 bytes), determined at compile time? Basically, what goes on internally?
In the second case, will the destructor of
Node
be called on each of the 100 objects?
Essentially, I have been using this syntax, but never understood what goes on internally and now I am curious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)