tags:

views:

250

answers:

4

In C++, what's the difference between

char *a = new char[10];

and

char *a = new char(10);

Thanks!

+16  A: 

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.

GMan
@GMan: Could you tell us about smart pointers?
Green Code
@Green: Eh, [this](http://stackoverflow.com/questions/94227/smart-pointers-or-who-owns-you-baby) is a good start.
GMan
+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
+7  A: 
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-
A: 

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.

Green Code
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.
GMan
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.
Green Code
A: 
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.

Legatou
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++?
GMan
10 memory slots are contiguous, comment on std::vector, (char)10 == '\n' ~~ Don't down-vote me just because I hurt your feelings.
Legatou
@Legatou: 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.
GMan
You opinion on how to teach people is also an opinion. We'll let him decide. Additionally, mentioning delete is important.
Legatou
@Legatou: 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.
GMan