the difference between new[] & new?

Started by
1 comment, last by Zerosignull 22 years, 8 months ago
whats the difference between usig the new[] operator and the new operator??? ~prevail by daring to fail~
Advertisement
with the ''new'' operator, you allocate memory and get a pointer to it:

int* i = new int;

now, if you want to allocate an array of 100 int''s:

int* iv = new int [100];

(the size you allocate must be a constant/const variable)

..and make sure that you delete the second one like this:

delete [] iv; //delete the whole block
new is for single items. For example:
// Gives you a pointer to 1 intint* abc = new int; 


new[] is for arrays. For example:
// Gives you a pointer to 10 intsint* abc = new int[10]; 

This topic is closed to new replies.

Advertisement