46

In C++, what's the difference between

char *a = new char[10];

and

char *a = new char(10);

Thanks!

geschema
  • 2,464
  • 4
  • 30
  • 41

6 Answers6

66

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

Or:

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

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 1
    @Green: Eh, [this](http://stackoverflow.com/questions/94227/smart-pointers-or-who-owns-you-baby) is a good start. – GManNickG Oct 10 '10 at 20:36
  • +1 from me. I always chalk it up to a "passing ignoramus" when I see there was an anonymous downvote to a perfectly correct answer. – Amardeep AC9MF Oct 13 '10 at 19:25
16
new char[10];

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

new char(10);

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

eq-
  • 9,986
  • 36
  • 38
12
char *a = new char[10];
...
delete [] a;

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

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'.


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.

Squirrelsama
  • 5,480
  • 4
  • 28
  • 38
  • 3
    What does this add over the existing answers? And your second half is purely opinion, and actually quite disagreeable in the modern C++ community. It's better to hand people black boxes, *then* show them how it works and why we use them. Manual memory management is bad C++ anyway, why start off by teaching people bad C++? – GManNickG Oct 13 '10 at 19:12
  • 1
    10 memory slots are contiguous, comment on std::vector, (char)10 == '\n' ~~ Don't down-vote me just because I hurt your feelings. – Squirrelsama Oct 13 '10 at 19:13
  • You edited in the second half, I edited my comment to match. Also, all three existing answers say it's an "array". Arrays are always contiguous. Also, `'\n'` doesn't necessarily have to be 10, it depends on your character set. – GManNickG Oct 13 '10 at 19:16
  • 2
    You opinion on how to teach people is also an opinion. We'll let him decide. Additionally, mentioning delete is important. – Squirrelsama Oct 13 '10 at 19:21
  • Indeed, but I feel it's a more reasonable belief to hold. It's not important because one should use `std::vector`. ;) I'm not down-voting you because you hurt my feelings, I'm down-voting because I don't think the answer is helpful; it doesn't add anything and includes bad advice. – GManNickG Oct 13 '10 at 19:24
  • 1
    The one thing it adds is explicitly mentioning the deletion, esp. the `delete[] a` gotcha – andybuckley Jul 10 '17 at 23:18
2

I would rather use:

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

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

&buff[0]

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

buff.c_str()

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

radato
  • 840
  • 9
  • 27
0

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:

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

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

Amir Zadeh
  • 3,481
  • 2
  • 26
  • 47
  • 4
    Just because an object has a size of one doesn't mean it's a char. Also, how can you possibly delete that variable? (Don't dynamically allocate when you don't have to, either.) "Don't forget to delete" -> Bad C++ code. – GManNickG Oct 10 '10 at 20:34
  • You are right, but i just wanted to show our friend that the people who made C++, made this for some purpose. I shouldn't have used it with cin and no references to it. – Amir Zadeh Oct 10 '10 at 20:53
0

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

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 deleting a.

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

nik
  • 9
  • 4