开发者

Does "new char[]" need to be release resources manually?

For this: m_sFilename = new char [len+1];

Should I call delete[] m_sFilename; someti开发者_如何学JAVAme?

And:

should I use delete[] m_sFilename or delete m_sFilename;?


Does "new char[]" need to be release resouces manually?

Yes.

For this: m_sFilename = new char [len+1]; Should I call delete[] m_sFilename; sometime?

Yes.

Should I use delete[] m_sFilename or delete m_sFilename;?

delete[].


But you should actually use std::string, which does all this for you, and is free.


Yes, if you don't want the memory allocated by the new[] to leak, you should delete[] it when you're done using that memory.

To avoid having to keep track of that memory, I recommend you use std::string or std::vector instead.


Yes, but use std::string to store m_sFilename instead - it's better in just about every way.


For native data types, and user-defined data types not having destructors using delete or delete[] would mean the same. The underlying heap manager would free all of the memory. But calling scaler delete over a vector new (new[]) will not make all destructors to be called. Destructor will be called only once for the very first object.

The behavior given above is compiler/heap-manager (which is handling new and delete) dependent. For better portability use scaler delete for scaler new and vector delete (delete[]) for vector new.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜