Hi,
Is there a difference between those 2 statements:
str1 = new char[50];
and
str2 = new char(50);
both str1 and str2 were defined like this:
char* str1=null;
char* str2= null;
Thanks
Avi
Printable View
Hi,
Is there a difference between those 2 statements:
str1 = new char[50];
and
str2 = new char(50);
both str1 and str2 were defined like this:
char* str1=null;
char* str2= null;
Thanks
Avi
Yes. The first one allocates 50 characters (50 times sizeof(char)) and the second statement allocates just one char initialized by a value 50.
The () notation calls a 'constructor' - which in the case of a primitive such as a char will just initialize it to that value. The [n] notation creates a contiguous block of memory with space for n objects of that type.
Remember when you delete something you allocated with new[n] use delete[].