C++ new char[10] 和 new char(10) 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3902011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:00:37  来源:igfitidea点击:

What's the difference between new char[10] and new char(10)

c++

提问by geschema

In C++, what's the difference between

在 C++ 中,有什么区别

char *a = new char[10];

and

char *a = new char(10);

Thanks!

谢谢!

回答by GManNickG

The first allocates an array of 10 char's. The second allocates one char initialized to 10.

第一个分配一个 10 个字符的数组。第二个分配一个初始化为 10 的字符。

Or:

或者:

The first should be replaced with std::vector<char>, the second should be placed into a smart pointer.

第一个应替换为std::vector<char>,第二个应放入智能指针中。

回答by eq-

new char[10];

dynamically allocates a char[10] (array of char, length 10), with indeterminate values, while

动态分配一个具有不确定值的 char[10](char 数组,长度为 10),而

new char(10);

again, dynamically allocates a single char, with an integer value of 10.

同样,动态分配单个字符,整数值为 10。

回答by Squirrelsama

char *a = new char[10];
...
delete [] a;

The above dynamically allocates and deallocates 10 contiguous memory slots that can be used to store chars.

以上动态分配和释放 10 个可用于存储字符的连续内存插槽。

char *a = new char(10);
...
delete a;

The above dynamically allocates and deallocates one memory slot that is initialized with the integer value 10, equivalent to the char value '\n'.

以上动态分配和释放一个内存槽,该内存槽用整数值初始化10,相当于字符值'\n'



Do NOT use the std::vector<T>if you do not first understand pointers. Knowing how memory allocation and pointers work will make you a better programmer.

std::vector<T>如果您不先了解指针,请不要使用。了解内存分配和指针的工作原理将使您成为更好的程序员

回答by radato

I would rather use:

我宁愿使用:

size_t size = 10; //or any other size
std::string buff(size, 0); //or: std::string buff(size, '
&buff[0]
');

Now if you must use the char* buff, then you can use:

现在,如果您必须使用 char* buff,那么您可以使用:

buff.c_str()

When you need to use const char* then you can use:

当您需要使用 const char* 时,您可以使用:

char * x ;
cin >> *(x=new char()) ;

The big advantage is that you don't need to deallocate the memory, stl take care of this for you. The next advantage is that you can use all of the stl string functions

最大的优点是您不需要释放内存,stl 会为您处理这个问题。下一个优点是您可以使用所有 stl 字符串函数

回答by nik

[10] defines an array where as (10) assigns a value to the newly created (single) character.

[10] 定义了一个数组,其中 as (10) 为新创建的(单个)字符分配一个值。

If you want to declare an array of size 10 in C and by mistake you define char a(10), compiler would throw a syntax error, so you get to fix it. But in C++, it will compile fine and your program may crash while accessing say a[1] or while deletinga.

如果你想在 C 中声明一个大小为 10 的数组并且错误地定义了char a(10),编译器会抛出一个语法错误,所以你可以修复它。但是在 C++ 中,它会编译得很好,并且您的程序可能会在访问 a[1] 或删除a 时崩溃。

So in C++, its always better to use vectorrather than dynamically allocated arrays. I hope you got the point.

所以在 C++ 中,使用向量总是比动态分配的数组更好。我希望你明白这一点。

回答by Amir Zadeh

Well the first one will make an array. But i guess your question is mostly on the second one. Your code can use it as a valid character, consider:

那么第一个将创建一个数组。但我想你的问题主要是在第二个问题上。您的代码可以将其用作有效字符,请考虑:

##代码##

Will make a character dynamically and then read it from stdin.

将动态创建一个字符,然后从标准输入中读取它。