1
00:00:01,470 --> 00:00:04,740
Memory and the C++ standard library here.

2
00:00:04,830 --> 00:00:11,910
This C++ Standard library provides various classes to allow you manipulate collection of objects.

3
00:00:12,150 --> 00:00:18,600
These classes called the s t L Standard Template Library.

4
00:00:18,900 --> 00:00:27,090
So this provides a standard way to insert items into collection objects and ways to access the items

5
00:00:27,090 --> 00:00:29,790
and iterate wrote entire collections.

6
00:00:29,820 --> 00:00:31,470
This is called the Iterator.

7
00:00:31,610 --> 00:00:40,400
Here, the SDL defines collections classes that are implemented as Quest stacks or as a vectors with

8
00:00:40,410 --> 00:00:42,000
a random axis here.

9
00:00:42,030 --> 00:00:45,590
These classes will be covered in depth in the next lecture.

10
00:00:45,600 --> 00:00:54,780
So in this section we will limit the discussion to just two classes that behave like C++ built in arrays.

11
00:00:54,990 --> 00:00:58,990
The first let's get began with the standard library arrays here.

12
00:00:59,010 --> 00:01:09,710
The standard library arrays provides two containers that given random access via an indexer to the data.

13
00:01:09,720 --> 00:01:17,040
So these two containers also allows you to access the underlying memory size they guarantee to store

14
00:01:17,070 --> 00:01:20,520
the items sequentially and contiguous in memory.

15
00:01:20,520 --> 00:01:24,780
And they can be used when you are required to provide a pointer to a buffer.

16
00:01:24,990 --> 00:01:32,400
So these two types are both templates, which means that you can use them to hold built in in the custom

17
00:01:32,400 --> 00:01:32,880
types.

18
00:01:32,880 --> 00:01:39,600
So these two collection classes are array and vector here.

19
00:01:41,990 --> 00:01:42,370
Yeah.

20
00:01:42,680 --> 00:01:43,970
So these are.

21
00:01:45,190 --> 00:01:48,430
These are the builtin standard.

22
00:01:49,400 --> 00:01:50,390
Library.

23
00:01:51,300 --> 00:01:52,140
All right.

24
00:01:52,560 --> 00:01:57,540
So we can also use the stack based array class in C++ here.

25
00:01:57,570 --> 00:02:05,400
The array class is defined in the array here, like that array header file here.

26
00:02:06,030 --> 00:02:10,940
So the class allows you to create fixed size arrays on the stack.

27
00:02:10,950 --> 00:02:16,990
And with the built in arrays, they cannot shrink or expand at runtime.

28
00:02:17,010 --> 00:02:23,850
So since they are all located on the stack, they do not require a call to a memory allocator at runtime,

29
00:02:23,850 --> 00:02:27,310
but clearly they should be smaller than the stack frame size.

30
00:02:27,390 --> 00:02:35,580
So this means that an array is a good choice for small array of items, so the size of an array must

31
00:02:35,580 --> 00:02:38,250
be known at compile time.

32
00:02:38,820 --> 00:02:44,310
And this is passed as a template parameter like that here, for example.

33
00:02:44,850 --> 00:02:47,640
Let's include a include array here.

34
00:02:50,020 --> 00:02:53,530
Array and array.

35
00:02:54,720 --> 00:02:56,790
Here, Integer four.

36
00:02:57,390 --> 00:02:59,100
And here, 1 to 3.

37
00:02:59,100 --> 00:02:59,820
Four.

38
00:03:00,890 --> 00:03:08,590
So in this code, the first template parameter in the angled braces is a type of each item in the array,

39
00:03:08,600 --> 00:03:12,620
and the second parameter is the number of items.

40
00:03:12,620 --> 00:03:21,730
So here number of items and the type of the each item in the array here.

41
00:03:21,740 --> 00:03:29,180
So this code initialize the array with an initialize list, but not that you still have to provide the

42
00:03:29,180 --> 00:03:31,370
size of an array in the template here.

43
00:03:31,550 --> 00:03:38,660
And this object will work like a built in array or indeed any of the standard library containers with

44
00:03:38,660 --> 00:03:40,480
the range loop here.

45
00:03:40,490 --> 00:03:49,160
So let's create a range load here integer E and here we can reset the array here, make it here.

46
00:03:52,780 --> 00:03:57,890
Right here, and we're going to create the array in the jar.

47
00:04:08,250 --> 00:04:08,660
There.

48
00:04:10,380 --> 00:04:13,440
Firstly, we're going to create the array like that.

49
00:04:19,070 --> 00:04:19,400
Yeah.

50
00:04:19,640 --> 00:04:23,150
And we're going to pass iterate fraudulence error here at.

51
00:04:26,500 --> 00:04:31,450
And each time we're going to print this on the screen.

52
00:04:34,430 --> 00:04:38,420
We have to import and use the namespace here.

53
00:04:40,530 --> 00:04:42,870
And rent it out.

54
00:04:43,880 --> 00:04:47,240
As you can see, we printed our variables here.

55
00:04:47,270 --> 00:04:53,870
The reason is that all implements the begin and end function that are required for this syntax.

56
00:04:53,870 --> 00:04:58,190
So you can also use this like that here.

57
00:04:58,190 --> 00:05:14,990
For example, for integer E zero opps integer E equals zero, while E is less than R that size here

58
00:05:15,230 --> 00:05:29,510
and plus class E here and C out here are right here are error E and and line here.

59
00:05:29,510 --> 00:05:35,510
We can write it also like that it's the same output here, the size function here.

60
00:05:35,510 --> 00:05:41,840
As you can see here we return the size of the array and the square braces.

61
00:05:41,840 --> 00:05:49,520
Indexer gives the random access to the members of the array so you can access the memory outside of

62
00:05:49,520 --> 00:05:51,180
the bounds of the array.

63
00:05:51,200 --> 00:06:00,170
So for the previously defined array that has four members you can access like this array or that ten.

64
00:06:00,170 --> 00:06:05,330
So this may cause the unexpected behavior at runtime.

65
00:06:05,330 --> 00:06:06,320
Let's try it out.

66
00:06:07,370 --> 00:06:09,770
As you can see here, there's a just reprint.

67
00:06:09,850 --> 00:06:11,330
There's a bunch of zeros.

68
00:06:12,250 --> 00:06:12,840
Here.

69
00:06:12,910 --> 00:06:19,820
So or this may cause unexpected behavior or even some kind of memory faults.

70
00:06:19,840 --> 00:06:28,450
So to guard against this, the class provides a function at which will perform perform a range take.

71
00:06:28,450 --> 00:06:37,840
And if the index is out of range, the class will throw the C++ exception which here.

72
00:06:39,290 --> 00:06:39,690
Here.

73
00:06:41,020 --> 00:06:45,310
C++ exception named out of range here.

74
00:06:46,060 --> 00:06:53,830
So the main advantage of using an array array object is that you get compile time checks to see if you

75
00:06:53,830 --> 00:06:59,530
are inadvertently passing the object to a function as a dump pointer here.

76
00:07:00,100 --> 00:07:02,230
Let's make another example here.

77
00:07:02,260 --> 00:07:03,070
Void.

78
00:07:04,150 --> 00:07:04,630
Here.

79
00:07:06,000 --> 00:07:09,480
Void use pen in its.

80
00:07:10,280 --> 00:07:12,140
An integer pointer here.

81
00:07:12,650 --> 00:07:17,720
So at runtime the function does not know the size of the buffer pass to it.

82
00:07:17,840 --> 00:07:25,160
And in this case, the documentation says that you must pass a buffer with ten integer types.

83
00:07:25,520 --> 00:07:26,180
Variables.

84
00:07:26,180 --> 00:07:34,360
But as we see in C++ allows, you allows a built in array to be used as a pointer.

85
00:07:34,370 --> 00:07:36,740
So we can do it like that.

86
00:07:37,280 --> 00:07:39,350
In practice use.

87
00:07:41,170 --> 00:07:41,800
Ten.

88
00:07:43,000 --> 00:07:43,900
Ah one.

89
00:07:44,020 --> 00:07:46,000
So, Ops.

90
00:07:49,540 --> 00:07:49,990
Here.

91
00:07:50,200 --> 00:07:55,450
So let's create another array for this example and make comment out.

92
00:07:56,720 --> 00:07:58,770
Common theme here.

93
00:07:59,570 --> 00:07:59,990
Okay.

94
00:08:00,060 --> 00:08:01,370
Integer array.

95
00:08:02,560 --> 00:08:02,900
Yes.

96
00:08:02,980 --> 00:08:04,630
And one, two, three, four.

97
00:08:05,050 --> 00:08:09,010
This contains the same variables as previous arrays here.

98
00:08:10,200 --> 00:08:17,070
And then this will read the past the end of the buffer here.

99
00:08:17,100 --> 00:08:20,610
As you can see here, we got an exception.

100
00:08:20,730 --> 00:08:28,620
So this is no compiler check or nor any non runtime check to catch this error.

101
00:08:28,920 --> 00:08:36,750
But there is the already class will not allow such an error to happen because there's no automatic conversion

102
00:08:36,750 --> 00:08:39,570
into a the pointer here.

103
00:08:39,570 --> 00:08:47,490
So if you're interested in obtaining a dumb pointer, you can do this and be guaranteed to have access

104
00:08:47,490 --> 00:08:53,430
to data as a contiguous block of memory where the items stored sequentially.

105
00:08:53,790 --> 00:08:55,830
So you can do it like that.

106
00:08:59,530 --> 00:08:59,980
Here.

107
00:09:00,960 --> 00:09:01,950
Let's compile it.

108
00:09:04,380 --> 00:09:06,750
And, uh, here.

109
00:09:10,440 --> 00:09:14,760
We got a letter turned from some comment filed.

110
00:09:15,540 --> 00:09:20,760
That's why you don't have to do this like that with a thumb pointer here.

111
00:09:21,210 --> 00:09:23,790
And we can.

112
00:09:26,070 --> 00:09:27,660
Testify like that here.

113
00:09:29,470 --> 00:09:30,570
I was one.

114
00:09:31,990 --> 00:09:34,000
And data here.

115
00:09:38,520 --> 00:09:43,340
And as you can see here, this won't compile either.

116
00:09:43,350 --> 00:09:48,960
So this is not the correct, correct coding style here.

117
00:09:48,960 --> 00:09:56,100
And you will learn more about and how to correct this syntax in next lecture.
