Archived

This topic is now archived and is closed to further replies.

Zerosignull

the difference between new[] & new?

Recommended Posts

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

Share this post


Link to post
Share on other sites
new is for single items. For example:
// Gives you a pointer to 1 int
int* abc = new int;


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

Share this post


Link to post
Share on other sites