版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bjbz_cxy/article/details/88804569
当我们在使用c语言创建字符串时一般是这样的:
char* str = (char*)malloc(20);
memset(str,0,20);
申请完内存需要使用memset来初始化字符串!
而使用Visual Studio编译器则完全不需要这么做
只需要这样:
char* str = new char[20]();
注意到后面的“()”了吗?
当你这样做分配内存工作时,Visual Studio编译器会自动给你字符初始化为0
而
char* str = new char[20];
是不可以的!
至少在我使用的Visual Studio2013上是可以的!