以下代码为什么会报错,以及如何正确删除ch,释放内存。
char *ch = new char(100); char tmp[10] = "e100"; strtod(tmp, &ch); cout << *ch << endl; delete ch; system("pause");
欢迎选择我的课程,让我们一起见证您的进步~~
You wrote it wrong, it should be new char[100], and the delete statement should be delete[] ch. New char(100) means to construct a char and initialize the value to 100.
The usage of strtod function is wrong here - it’s okay to say that ch is not initialized
Please check strtod usage: http://baike.baidu.com/view/1876981.h...
ch has not been initialized, and its content is uncertain. It is very likely that strtod will not encounter the end of string symbol ("NULL")
Also, the last sentence delete[] ch is better.
You wrote it wrong, it should be new char[100], and the delete statement should be delete[] ch. New char(100) means to construct a char and initialize the value to 100.
The usage of strtod function is wrong here - it’s okay to say that ch is not initialized
Please check strtod usage:
http://baike.baidu.com/view/1876981.h...
ch has not been initialized, and its content is uncertain. It is very likely that strtod will not encounter the end of string symbol ("NULL")
Also, the last sentence delete[] ch is better.