-
April 8th, 2007, 03:13 AM
#1
Difference between new () and []
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
-
April 8th, 2007, 03:46 AM
#2
Re: Difference between new () and []
Yes. The first one allocates 50 characters (50 times sizeof(char)) and the second statement allocates just one char initialized by a value 50.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 9th, 2007, 02:30 AM
#3
Re: Difference between new () and []
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[].
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|