Kai*_*aan 3 c++ memory io iostream
我有一个简单的问题.我有一个我写数据的流.完成并打电话后close()
,我是否需要拨打delete
句柄或close()
进行清理?
例如:
mFileStream = new std::ofstream(LogPath.c_str(), std::ios::trunc);
...
mFileStream->Close();
//delete mFileStream?
Run Code Online (Sandbox Code Playgroud)
我的直觉是肯定的,因为我已经分配了它,但我不确定我在哪里读它.任何人都可以澄清吗?
是的,你必须.在C++中,你必须配对new
和delete
.
虽然,在这样一个简单的情况下你不需要,你可以在堆栈上分配你的对象,它会被破坏,强烈建议(更快更安全):
{ // enclosing scope (function, or control block)
ofstream mFileStream(LogPath.c_str(), std::ios::trunc);
...
mFileStream.close(); // mFileStream is not a pointer any more, use the "." operator
// mFileStream destroyed for you here. "close" may even be called for you.
}
Run Code Online (Sandbox Code Playgroud)
小记:它close
带有一个小"c".
归档时间: |
|
查看次数: |
5602 次 |
最近记录: |