sarfaraznawaz wrote:
When we dynamically allocate the memory say 100 integers say
int *x = new int(1000);
When we want to allocate
100
integers, we should use the correct syntax.
Your code snippet just allocates memory for
1
(yes, one) integer and then
initializes it with the value
1000
. The correct statement (for allocating
100
integers) is:
int * x = new int[100];
on success, it allocates (at once) contiguous memory for
100
integers, on failure an exception is thrown as specified in
documentation of new operator[
^].