1
00:00:00,750 --> 00:00:01,170
OK.

2
00:00:01,200 --> 00:00:09,720
Welcome back to yet another lecture on dynamic memory and pointers in this lecture, we are going to

3
00:00:09,720 --> 00:00:15,600
go over something that I actually kind of foreshadowed before that we were going to go over and that

4
00:00:15,600 --> 00:00:20,940
was in the final video slash lecture that was on while loops.

5
00:00:21,120 --> 00:00:27,540
It was the one where we did an example where we're using an array at the beginning of the video.

6
00:00:28,170 --> 00:00:35,340
I mentioned something that was not good programming practice, and that was to make something like this

7
00:00:35,340 --> 00:00:37,800
where we had a variable called size.

8
00:00:38,490 --> 00:00:42,570
We see in to the size with user input.

9
00:00:42,990 --> 00:00:52,170
And then we were making an array like this where the size was defined by this variable that we ran into.

10
00:00:53,330 --> 00:00:55,280
And the reason that this is a problem.

11
00:00:56,670 --> 00:01:03,810
Is because although I said it works on some compilers, it doesn't really work for the C++ language

12
00:01:03,810 --> 00:01:05,460
in general, it's not supported.

13
00:01:05,730 --> 00:01:10,800
And the reason it's not supported is because this actually needs to be known at compile time.

14
00:01:11,040 --> 00:01:15,870
The array size, and this is not something that is going to happen at compile time, right?

15
00:01:16,050 --> 00:01:16,980
It's going to see this.

16
00:01:16,980 --> 00:01:21,600
But size is not known until at runtime.

17
00:01:21,600 --> 00:01:23,580
The user enters the size, right?

18
00:01:24,480 --> 00:01:25,410
That's problematic.

19
00:01:26,070 --> 00:01:29,520
So we cannot actually do something like this and by cannot.

20
00:01:29,570 --> 00:01:35,280
I mean, you still can do it because of + + compiler extensions.

21
00:01:35,610 --> 00:01:38,220
So many GW compiler that we installed.

22
00:01:39,130 --> 00:01:42,390
Seems like it actually works for something like this, but you should not do this.

23
00:01:42,390 --> 00:01:43,860
It is not good programming practice.

24
00:01:44,890 --> 00:01:55,260
However, there is a way that we can safely do something like this and like you might be able to guess

25
00:01:55,270 --> 00:02:01,870
it is going to be using dynamic memory on the heap, which is something called a dynamic array when

26
00:02:01,870 --> 00:02:03,490
we're doing it in form of an array.

27
00:02:04,180 --> 00:02:07,540
So let's go ahead and see how we can do that.

28
00:02:07,930 --> 00:02:11,440
So we don't want to make this on the busy stack, right?

29
00:02:11,740 --> 00:02:13,390
Because that becomes an issue.

30
00:02:13,720 --> 00:02:18,790
C++ doesn't like, you know, it wants to know the size of it because the stack is a busy place and

31
00:02:18,790 --> 00:02:25,420
we can't have these like arrays that can be really big, that aren't known on the stack.

32
00:02:25,420 --> 00:02:29,520
We need to put them in a more free area, which is over there in the heap.

33
00:02:30,580 --> 00:02:36,790
So I'm going to leave this part right here, but I'm going to change this to be an array on the heap,

34
00:02:36,910 --> 00:02:40,090
since it's on the heap, we know that we're going to need to make a point, right?

35
00:02:40,750 --> 00:02:42,520
I'm just actually going to call this.

36
00:02:42,520 --> 00:02:45,520
I'm going to call this like my RR for like array.

37
00:02:46,090 --> 00:02:52,690
So it's going to be int my R for my array and it's going to have to be a pointer.

38
00:02:52,690 --> 00:02:56,040
Like I said, when I say new equals new int.

39
00:02:56,650 --> 00:03:00,250
And then I'm putting in square brackets and I'm putting size like this.

40
00:03:01,060 --> 00:03:05,890
And that's all we have to do to make a dynamic array that it's going to be on the heap.

41
00:03:07,530 --> 00:03:12,990
So now we can totally read into this, so I'm going to go ahead and do that, I'll do a for loop, I

42
00:03:12,990 --> 00:03:14,610
say into a zero.

43
00:03:17,470 --> 00:03:26,680
I is less than size I was plus so it would go as many times as the user enters, and then I'm going

44
00:03:26,680 --> 00:03:37,630
to just sit out, I'll say into a number and I'll see an end to my air.

45
00:03:37,870 --> 00:03:47,230
And I do not need to reference it with the asterisk because this operator right here, the subscript

46
00:03:47,230 --> 00:03:52,790
operator, actually references memory as it is.

47
00:03:52,810 --> 00:03:58,600
So this highlighted thing right here, which is that subscript indexing operator.

48
00:03:59,020 --> 00:04:00,880
That's the thing that's referencing.

49
00:04:00,880 --> 00:04:07,300
So we do not need to put in an additional star in front of this because it's going to be handled by

50
00:04:07,300 --> 00:04:08,260
this Ferrara.

51
00:04:08,290 --> 00:04:10,720
OK, so pretty cool.

52
00:04:10,990 --> 00:04:17,380
We can actually put AI in here and we will.

53
00:04:19,890 --> 00:04:23,700
Then read in to that location in our.

54
00:04:26,770 --> 00:04:33,250
So once we read in everything into the array, I can then print it out, so I'll just do another loop

55
00:04:33,250 --> 00:04:46,500
here and I zero Eilis size eight plus and I'll just say see out and it will be like my array I.

56
00:04:46,540 --> 00:04:50,110
And then I'll go ahead and put a little camera in a space like that.

57
00:04:50,590 --> 00:04:53,020
And we should be good to go.

58
00:04:53,770 --> 00:05:01,030
So one question that you might have been thinking about now is, well, we've allocated memory, so

59
00:05:01,030 --> 00:05:02,320
we need to delete it, right?

60
00:05:02,330 --> 00:05:03,910
We've allocated heap memory.

61
00:05:04,510 --> 00:05:07,240
I'm sure that even though it's an array, we must need to delete it.

62
00:05:07,240 --> 00:05:07,510
Right?

63
00:05:07,510 --> 00:05:08,830
And you are correct.

64
00:05:08,830 --> 00:05:09,880
We do need to delete it.

65
00:05:10,480 --> 00:05:14,710
But you might be thinking, Well, how do we delete an entire array?

66
00:05:15,370 --> 00:05:22,750
Because if you know, really, this pointer here is just pointing to the starting byte of the array.

67
00:05:24,040 --> 00:05:26,830
This array of integers and memory and continuous memory.

68
00:05:28,060 --> 00:05:36,520
Normally we would just delete, you know, like my aunt or something, if it was a dynamically allocated

69
00:05:36,520 --> 00:05:44,590
single integer, but to delete an array of values to make sure we delete everything, we actually need

70
00:05:44,590 --> 00:05:52,840
to use a slightly different syntax instead of just saying delete my ah, we are going to say delete

71
00:05:52,840 --> 00:05:55,780
with some square brackets and then say my error.

72
00:05:55,810 --> 00:06:00,760
So this right here is the syntax that will delete everything in the array.

73
00:06:00,760 --> 00:06:06,730
So we don't have to worry about only deleting like the first item that's being pointed to in that contiguous

74
00:06:06,730 --> 00:06:07,510
memory section.

75
00:06:09,260 --> 00:06:10,890
So this should be good to go.

76
00:06:10,940 --> 00:06:17,870
Just going to run it for kind of example purposes, so we can kind of see that this works out.

77
00:06:18,140 --> 00:06:19,520
I go ahead and I can pilot.

78
00:06:19,520 --> 00:06:22,190
Of course, I'm using the same program.

79
00:06:22,190 --> 00:06:28,100
You know, you probably should make a new program for all of this and save it and store it in an organized

80
00:06:28,100 --> 00:06:28,670
fashion.

81
00:06:28,680 --> 00:06:32,870
Unlike I have been doing with everything just stepped in here in the first program, but nevertheless,

82
00:06:33,680 --> 00:06:35,180
I'm going to run this.

83
00:06:36,610 --> 00:06:39,080
So I go ahead and I run this.

84
00:06:39,100 --> 00:06:44,080
It doesn't really say anything because it's just asking me for a size without saying anything, so I'm

85
00:06:44,080 --> 00:06:48,090
going to go ahead and say for then enter a number.

86
00:06:48,100 --> 00:06:54,970
I'm going to go ahead and just put one two three four, right?

87
00:06:55,720 --> 00:06:58,750
Then of course, that all prints out right here.

88
00:06:58,780 --> 00:07:00,070
One two three four.

89
00:07:00,880 --> 00:07:02,180
And we're good to go.

90
00:07:02,200 --> 00:07:04,240
This thing got deleted because of this.

91
00:07:04,600 --> 00:07:06,430
So everything is all good now.

92
00:07:06,640 --> 00:07:07,000
So.

93
00:07:08,490 --> 00:07:12,540
What you've kind of learned now is how to make a dynamic array.

94
00:07:13,500 --> 00:07:19,620
You've learned that it is necessary to make a dynamic array if you want to do something like this where

95
00:07:19,620 --> 00:07:25,800
you have an unknown size, at least the size is not known immediately at compile time.

96
00:07:26,100 --> 00:07:32,160
If you want to make an array on the stack, if you're going to make a variable, it has to be a constant

97
00:07:32,430 --> 00:07:39,030
variable or you have to put a like hardcoded value in there so the size can be known at the compile

98
00:07:39,030 --> 00:07:39,330
time.

99
00:07:41,250 --> 00:07:41,730
All right.

100
00:07:41,940 --> 00:07:46,650
So I think that this was probably also enough for a full lecture.

101
00:07:47,550 --> 00:07:55,230
I'm not going to go into too much more because I think this is enough already with just a how to make

102
00:07:55,230 --> 00:07:59,820
a dynamic array scene that it's the same to read into it.

103
00:08:00,570 --> 00:08:02,640
You don't need to reference it to do that.

104
00:08:02,970 --> 00:08:08,720
This D references it here, and this is how you would delete that dynamic array.

105
00:08:09,780 --> 00:08:13,260
All right, so with that, I will see you in the next lecture.
