首页 > 以下代码为什么报错?

以下代码为什么报错?

cout是有值的,但是delete得时候就会报错。

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

稍微看了一下strtod的代码:

00088 double
00089 strtod (const char * nptr, char ** endptr)
00105     if (endptr)
00106        *endptr = (char *)nptr;
00132        if (endptr)
00133            *endptr = (char *)nptr + 2;

然后对比一下下面两段代码:

分割线
char *ch = new char[10];
ch = "3.14";
cout << ch << endl;
delete [] ch;
char *ch = new char[10];
strncpy(ch,"3.14",10);
cout << ch << endl;
delete [] ch;

别忘了cstring!

【热门文章】
【热门文章】