1
00:00:00,400 --> 00:00:06,730
You can also create arrays of objects in dynamic memory using the new operator.

2
00:00:06,730 --> 00:00:13,250
So you do this by providing the number of items you want to create in a pair of square braces here.

3
00:00:13,270 --> 00:00:17,680
So this code here that I will write here.

4
00:00:17,710 --> 00:00:22,240
P new integer two and P zero.

5
00:00:23,530 --> 00:00:28,570
Zero here one and this pointer here.

6
00:00:29,230 --> 00:00:30,850
P plus one.

7
00:00:31,270 --> 00:00:33,460
And here to.

8
00:00:34,390 --> 00:00:36,970
And then we're going to create a for loop here.

9
00:00:36,970 --> 00:00:41,110
Integer E zero while e less than.

10
00:00:42,220 --> 00:00:46,330
To increment E by one.

11
00:00:46,330 --> 00:00:51,460
And then we're going to print the print, the p e variable here.

12
00:00:52,600 --> 00:00:54,610
P here.

13
00:00:56,300 --> 00:00:56,630
Line.

14
00:00:58,000 --> 00:01:01,810
And after this for loop, we're going to delete the P.

15
00:01:03,670 --> 00:01:03,940
Here.

16
00:01:06,500 --> 00:01:12,000
So the operator here returns a pointer to the type allocated.

17
00:01:12,020 --> 00:01:17,510
So and you use the pointer arithmetic or array indexing to access the memory.

18
00:01:17,540 --> 00:01:22,010
You cannot initialize the memory in the new statement.

19
00:01:22,010 --> 00:01:25,540
So you have to do that after creating the buffer here.

20
00:01:25,550 --> 00:01:32,360
So when you use a new to create a buffer for more than one object, you must use the appropriate version

21
00:01:32,390 --> 00:01:34,760
of the delete operator.

22
00:01:34,760 --> 00:01:43,670
And the curly braces is used to indicate that the more than one item is deleted and the destructor for

23
00:01:43,670 --> 00:01:45,860
each object will be called.

24
00:01:45,860 --> 00:01:52,490
So it's it's important that you always use the right version of delete appropriate to the version of

25
00:01:52,490 --> 00:01:53,300
new here.

26
00:01:53,300 --> 00:02:00,170
Used to create the pointers here so custom types can define their own operator.

27
00:02:00,920 --> 00:02:04,340
The new operator and the delete operator for individual objects.

28
00:02:04,340 --> 00:02:06,240
So as well as the operator.

29
00:02:06,240 --> 00:02:14,550
New curly braces here and operator that delete curly braces here, uh, for array of objects here.

30
00:02:14,550 --> 00:02:24,060
So the custom type order can use these to use custom memory allocation schemes for their objects.

31
00:02:26,050 --> 00:02:29,170
And here if we get a failed.

32
00:02:30,170 --> 00:02:31,430
When you're doing this.

33
00:02:31,460 --> 00:02:40,520
If the new operator cannot allocate the memory and for an object, it will throw the STD.

34
00:02:41,390 --> 00:02:51,590
STD but outlook bad exception and the pointer returned will have the nullptr here.

35
00:02:51,590 --> 00:02:54,440
So exceptions are covered in next lecture.

36
00:02:54,710 --> 00:02:55,790
Next lectures.

37
00:02:55,790 --> 00:02:55,940
So.
