C++内存管理问题,如何删除char *
天蓬老师
天蓬老师 2017-04-17 11:06:25
0
2
587

以下代码为什么会报错,以及如何正确删除ch,释放内存。

char *ch = new char(100);
char tmp[10] = "e100";
strtod(tmp, &ch);
cout << *ch << endl;
delete ch;
system("pause");
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
迷茫

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.

Ty80

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template