Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,300 --> 00:00:06,660
Hello, and welcome back.
2
00:00:06,660 --> 00:00:10,660
In this lecture, we're going to talk about the register storage class specifier.
3
00:00:10,660 --> 00:00:14,460
It has to do specifically with speed and efficiency.
4
00:00:14,820 --> 00:00:18,180
So we all know what a register is in terms of the CPU.
5
00:00:18,180 --> 00:00:21,580
A process register is a small set of data holding
6
00:00:21,580 --> 00:00:24,080
places that are part of the computer processor.
7
00:00:24,380 --> 00:00:27,680
So register can hold an instruction, a storage address
8
00:00:27,680 --> 00:00:29,180
or any kind of data.
9
00:00:29,780 --> 00:00:33,380
The register storage class is used to find local variables that should be stored
10
00:00:33,380 --> 00:00:35,740
in a register instead of ram.
11
00:00:35,740 --> 00:00:38,240
So normally, when you create variables right they're in memory,
12
00:00:38,740 --> 00:00:40,070
random access memory,
13
00:00:40,070 --> 00:00:43,670
when you specify the keyword register to a variable,
14
00:00:43,670 --> 00:00:46,170
it's going to store the variable in a register.
15
00:00:46,170 --> 00:00:48,370
So this will make the use of the register variables
16
00:00:48,370 --> 00:00:51,770
much quicker and much faster more efficient than normal
17
00:00:51,770 --> 00:00:53,670
variables that are stored in memory.
18
00:00:54,170 --> 00:00:56,670
So during the run time the program is going to be more efficient.
19
00:00:57,270 --> 00:01:00,270
The registered storage class should only be used for variables
20
00:01:00,270 --> 00:01:01,770
that require this quick access,
21
00:01:02,670 --> 00:01:05,870
the variables which are most frequently used in a c program.
22
00:01:05,870 --> 00:01:08,870
So if a function uses a particular variable a lot,
23
00:01:08,870 --> 00:01:11,870
then you may want to consider adding a register keyword to it.
24
00:01:12,860 --> 00:01:15,060
It's basically a hint to the compiler
25
00:01:15,060 --> 00:01:17,560
that a given variable can be put in a register.
26
00:01:18,060 --> 00:01:19,960
It is the compiler's choice
27
00:01:19,960 --> 00:01:23,060
to put in a register or not and you're going to see this as a common theme
28
00:01:23,060 --> 00:01:25,560
when it comes to compiler and efficiencies.
29
00:01:25,560 --> 00:01:27,220
You can suggest things to the compiler.
30
00:01:27,220 --> 00:01:30,820
The compiler doesn't necessarily have to follow that, sometimes it can't.
31
00:01:31,320 --> 00:01:35,820
So this is the same thing when you say register variable. It may or may not be stored in a register.
32
00:01:35,820 --> 00:01:38,320
It might be stored depending on the hardware
33
00:01:38,320 --> 00:01:40,920
and implementation restrictions in a register or not.
34
00:01:40,920 --> 00:01:43,720
Generally, compilers themselves do optimizations
35
00:01:43,720 --> 00:01:46,380
and put the variables in registers sometimes when you don't even know it.
36
00:01:46,380 --> 00:01:49,680
The keyword register is used to define the register storage class,
37
00:01:50,670 --> 00:01:54,270
both local variables and formal parameters can be declared as
38
00:01:54,270 --> 00:01:55,870
register variables.
39
00:01:56,470 --> 00:02:00,470
This storage class declares registered variables which have the same functionality
40
00:02:00,470 --> 00:02:03,070
as that as automatic local variables.
41
00:02:03,070 --> 00:02:06,060
So the lifetime of the register only remains
42
00:02:06,060 --> 00:02:08,960
when control is within the block.
43
00:02:08,960 --> 00:02:11,320
The actual variable that is stored in register
44
00:02:11,320 --> 00:02:14,320
has a maximum size equal to the register size.
45
00:02:14,680 --> 00:02:17,680
You cannot obtain the address of a registered variable using pointers.
46
00:02:18,280 --> 00:02:22,940
So you cannot have the unary address of operator applied to it.
47
00:02:22,940 --> 00:02:26,740
It doesn't have an address because it's not a memory location it's in a register itself.
48
00:02:27,240 --> 00:02:29,240
So let's go ahead and look at an example.
49
00:02:30,040 --> 00:02:32,940
So it's very basic to use a register it's pretty straightforward.
50
00:02:32,940 --> 00:02:36,300
If we have a main function here and we want to clear our variable
51
00:02:36,300 --> 00:02:40,760
with the storage class specify register, we just say register int x.
52
00:02:41,560 --> 00:02:43,560
And this will be a register variable.
53
00:02:44,060 --> 00:02:46,560
It's essentially a counter in this program.
54
00:02:47,460 --> 00:02:51,360
And so now if you have a for loop, you can say x equals one.
55
00:02:52,460 --> 00:02:54,860
X is less than equal to 15,
56
00:02:55,660 --> 00:02:57,260
and x plus plus.
57
00:02:57,260 --> 00:03:02,140
It doesn't really seem like a good example to use it because it's not necessarily being accessed to ton.
58
00:03:02,140 --> 00:03:06,540
So I know how much efficiency you can improve, but it really is just showing you the syntax.
59
00:03:06,540 --> 00:03:09,840
You apply that register keyword before the data type.
60
00:03:09,840 --> 00:03:13,200
So maybe inside this function, we would print out the variable.
61
00:03:14,700 --> 00:03:18,800
And it's as basic as that now.
62
00:03:18,800 --> 00:03:20,400
As I previously mentioned,
63
00:03:20,400 --> 00:03:23,600
if you try to use the ampersand operator with this variable,
64
00:03:23,600 --> 00:03:25,800
the compiler is going to complain and give me an error,
65
00:03:25,800 --> 00:03:29,700
sometimes it'll be a warning. It depends on the compiler you're using.
66
00:03:29,700 --> 00:03:31,900
So when you say the variable's a register,
67
00:03:31,900 --> 00:03:34,100
it may be stored in a register instead of memory
68
00:03:34,100 --> 00:03:38,100
and axis in the address of the register is invalid. We can't do this. We can't
69
00:03:38,100 --> 00:03:39,600
say register int x
70
00:03:40,600 --> 00:03:45,200
and then try to create a pointer like int pointer a
71
00:03:45,800 --> 00:03:48,700
equals ampersand x.
72
00:03:48,700 --> 00:03:52,500
That's not going to work. And if you try to compile that,
73
00:03:52,500 --> 00:03:54,500
again, it depends on the compiler.
74
00:03:54,500 --> 00:03:57,900
In this case, it just gives me a warning, saying
75
00:03:58,800 --> 00:04:02,800
unused variable a, but this is actually not okay to do.
76
00:04:03,790 --> 00:04:07,390
So let's say we try to initialize that to 15
77
00:04:07,390 --> 00:04:09,990
and then we just try to print out the variable,
78
00:04:09,990 --> 00:04:11,490
the value of the pointer.
79
00:04:11,490 --> 00:04:15,050
So we say de-reference it,
80
00:04:15,050 --> 00:04:18,050
say something like this. If we build this,
81
00:04:18,850 --> 00:04:23,750
we get an actual address of variable register is required.
82
00:04:24,550 --> 00:04:28,210
So error address of register variable x requested, you can't do that
83
00:04:28,210 --> 00:04:29,810
because x is registered.
84
00:04:29,810 --> 00:04:33,710
Now you can still actually use the register key or with a pointer variable
85
00:04:33,710 --> 00:04:35,910
or you just can't access the address.
86
00:04:35,910 --> 00:04:39,510
So the register can store any data and that includes a memory location.
87
00:04:40,110 --> 00:04:43,770
And so here, you could say, let's say, we
88
00:04:43,770 --> 00:04:47,130
change this int x and we say it's equal to 15
89
00:04:47,130 --> 00:04:49,630
and then we declare the pointer as a register.
90
00:04:50,130 --> 00:04:54,330
So we say register int star a equals
91
00:04:54,930 --> 00:04:58,830
the address of x. Now this is perfectly fine to do
92
00:04:58,830 --> 00:05:01,730
because all we're doing here is we're saying
93
00:05:01,730 --> 00:05:05,030
the pointer is actually going to store in a register and it' going to point to
94
00:05:05,030 --> 00:05:08,330
store an actual address So if we tried to build this,
95
00:05:08,930 --> 00:05:11,930
we're good to go. Because again, see the difference here.
96
00:05:11,930 --> 00:05:13,930
The pointer is going to be stored in a register.
97
00:05:13,930 --> 00:05:15,930
It can store an actual address.
98
00:05:15,930 --> 00:05:19,590
You just can't get the address of a register variable itself.
99
00:05:19,590 --> 00:05:21,190
So understand that different.
100
00:05:22,090 --> 00:05:24,690
Now remember register is a storage class
101
00:05:24,690 --> 00:05:28,690
and c does not allow multiple storage class specifiers for a variable.
102
00:05:28,690 --> 00:05:34,590
So register cannot be used with static. We can't say register static
103
00:05:34,620 --> 00:05:35,980
int a.
104
00:05:35,980 --> 00:05:38,980
That's going to give you a compiler error. So again,
105
00:05:38,980 --> 00:05:40,580
you can test this yourself.
106
00:05:40,580 --> 00:05:45,180
But when you try to build this, it says multiple storage classes and declaration specifiers
107
00:05:45,180 --> 00:05:46,780
You cannot do that now.
108
00:05:46,780 --> 00:05:49,780
What if we try to use register with global scope.
109
00:05:49,780 --> 00:05:54,440
So let's try to move this out, put it outside in here.
110
00:05:54,440 --> 00:05:57,740
And we're going to remove the static because we can't do that.
111
00:05:57,740 --> 00:06:01,540
We're not going to store -- we're not going to have it be a pointer, we're just going to say
112
00:06:01,540 --> 00:06:05,040
register int x equals 10.
113
00:06:06,030 --> 00:06:10,130
And then we're going to try to access this,
114
00:06:13,730 --> 00:06:14,730
something like that.
115
00:06:14,730 --> 00:06:17,330
If we try to build this, you're going to get an error.
116
00:06:17,330 --> 00:06:19,930
It says register name not specified for x.
117
00:06:19,930 --> 00:06:22,130
So register can only be used within a local block,
118
00:06:22,130 --> 00:06:24,730
it can't be used in global scope outside of main.
119
00:06:25,330 --> 00:06:29,320
It's perfectly fine like we did before to move this now
120
00:06:29,620 --> 00:06:33,220
down inside the main function and then print it out.
121
00:06:34,220 --> 00:06:38,420
This is fine. You just can't actually use it inside global scope.
122
00:06:39,420 --> 00:06:40,920
So keep that in mind.
123
00:06:40,920 --> 00:06:44,910
So now that we're done with storage class specifiers, let me just give you a little bit of review here.
124
00:06:46,410 --> 00:06:50,710
So here's a nice summary of all our different storage class specifiers.
125
00:06:51,010 --> 00:06:53,410
And you'll see also with static because you can
126
00:06:53,410 --> 00:06:57,410
use it in many different ways I also summarize the static I found this online
127
00:06:57,410 --> 00:07:00,010
this isn't my entire image.
128
00:07:00,010 --> 00:07:02,210
But you'll notice, if you have storage class auto
129
00:07:02,710 --> 00:07:07,210
and you declare that inside of a function or a block, the scope, the visibility
130
00:07:07,210 --> 00:07:10,570
is within the function or the block. You can't access it in other files.
131
00:07:10,570 --> 00:07:13,170
You can't access it outside of that function.
132
00:07:13,170 --> 00:07:14,370
The lifetime
133
00:07:14,370 --> 00:07:19,270
until the function block completes. So it gets created in the function destroyed in the function or the block.
134
00:07:19,270 --> 00:07:20,870
When you use storage class register,
135
00:07:20,870 --> 00:07:26,270
you can only use that inside of a function or a block, so inside the main, for example.
136
00:07:26,270 --> 00:07:28,930
The scope is in within that function or block.
137
00:07:28,930 --> 00:07:32,730
And again, it gets created and destroyed when the function creates
138
00:07:32,730 --> 00:07:36,230
and -- when the function executes and returns.
139
00:07:36,830 --> 00:07:40,150
For extern, extern is available outside all the functions.
140
00:07:40,150 --> 00:07:43,150
It has basically global scope between files.
141
00:07:43,150 --> 00:07:45,410
So the entire file plus other files
142
00:07:45,410 --> 00:07:49,090
where the variable is disclaimed as external is the scope and the visibility.
143
00:07:49,990 --> 00:07:53,290
The lifetime is the entire program until it terminates.
144
00:07:53,290 --> 00:07:57,590
If you static locally inside a function or block,
145
00:07:57,590 --> 00:07:59,590
it's only accessible within that function or block
146
00:07:59,590 --> 00:08:03,090
and but it's it's actually allocated in memory,
147
00:08:03,090 --> 00:08:05,590
all the way until the program exits.
148
00:08:06,190 --> 00:08:07,990
Now if you static on a global
149
00:08:08,490 --> 00:08:11,490
and it's declared outside of all functions which is global variable,
150
00:08:11,490 --> 00:08:14,690
the entire file in which it is declared you can access,
151
00:08:14,690 --> 00:08:18,690
you can't access it in other files. And again, it's alive in memory
152
00:08:18,690 --> 00:08:20,190
until the program exits.
153
00:08:20,390 --> 00:08:22,990
So understand these storage class specifiers,
154
00:08:22,990 --> 00:08:26,650
understand how to use them with variables functions and so forth
155
00:08:26,650 --> 00:08:28,650
and let me know if you have any questions. Thank you.
14046
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.