Storage Classes - C++ Tutorial For Beginners #8
Skills:
Backend Performance60%
Key Takeaways
Explains storage classes in C++ using the auto storage class as an example
Full Transcript
it's not a game it's [Music] a what is going on guys welcome back to C++ tutorial Series in today's video we're going to talk about storage classes so let us get right into it now storage classes are probably the first topic that we're talking about that you're probably not going to encounter in Python in Java and C uh because it's a little bit more low level we're talking about memory management here uh manual memory management and uh this is done to optimize performance and since this is not a very Advanced topic uh yes it's new to everyone who's watching who's not coding in C C++ rust or something like that uh it's a new topic but it's not very complicated and we're going to talk about the storage classes uh since they are some somewhat related to working with variables and uh data types and so on um and we have six storage classes we're going to talk about four of those in a little bit more detail and we're going to just mention two of them for the sake of completeness here the first one that we're going to talk about is the auto storage class and uh actually it's the default storage class so when you use the keyword Auto you're actually doing the same thing that you're doing when you're not using a keyword at all so automatic storage duration means that uh the compiler figures out itself uh how long a variable should be stored um and this is the default thing so if you just say int a equals 10 this storage class is already Auto so automat atic storage um duration however what you can do with auto that you cannot do with the normal way of declaring variables is you can make the data type automatic so you can say Auto a equals 10 and the compiler is going to figure out that this has to be an integer now don't confuse this with Dynamic typing uh C++ is statically typed it's not dynamically typed like python um the compiler will figure out that this is an integer so it's going to be a static uh statically typed integer um you cannot just go ahead and and make this dynamically typed here it's just that you don't have to specify it um this of course slightly increases the um the compiler time but it's NE negligible it's not something that you need to worry about too much but if you want to choose the auto keyword in order to not specify the data type yourself the compiler is going to do it for you uh you don't need to worry about that but except for that you don't have any differences than just saying int a int come on int AAL 10 it's the same thing because the auto storage class is actually the same as not not specifying a storage class at all all right so the second storage class that we're going to talk about is called extern like that extern and E and what this does is it tells the compiler that there is this integer e which is global it's accessible from multiple files it's uh public Global whatever uh and how this works is that you have one file we're talking about multiple files here otherwise this storage class doesn't make a lot of sense and I know we haven't talked about header files yet uh but the idea is that you have uh somewhere you define this integer e let's say in this file you define it not not with the extern keyword but just integer e and then you do something with it like eal 10 or whatever and then in another file let's say this down here is another file you would say extern int e and by doing that every every other file that includes that file where this is written has now AIS to e which is defined here so this essentially tells the compiler that uh or or it says there exists some integer e that's globally accessible so it specifies the existence it declares the existence the compiler doesn't care about where this uh integer e is defined it just cares about that it is defined and we know that uh it's globally accessible so we can work with it throughout different files and this extern keyword specifies exactly that so when we say extern in E we cannot Define it so you cannot do something like extern in E equals 10 because extern in E just specifies there is somewhere this integer e and it's globally accessible and you can use it in this file and in every file that that includes this file this is just specifying that there is this integer e out there somewhere and we can use it here so we can say C out e for example of course we need to use namespace SD and so on uh but this is how it works you say there is this integer e somewhere and I can use it in this file here so for the next storage class we're going to get a little bit more technical the storage class is called Static like static in aals 10 it has to do with memory allocation and with scope and if you've never programmed before you probably don't know what scope is if you're programmed in Python Java C you probably know what scope is already um it's essentially just an area of uh you could say accessibility let's say for example I Define int aals 10 here this is the scope where a is defined this is the scope here the main function is the scope I have this Cur these curly brackets here and if I say int a equals 10 this is the scope of that variable now I know we haven't talked about functions yet but I'm going to just uh give you a quick example here let's say we have another function here in my function uh that does something we're not going to care about the function signature or something like that in detail uh but here I can also Define a variable let's say I have int b equals 20 now because of the scope because of the scope of a a is defined for the main function I cannot access uh a in here I cannot say C out a now even if I say of course using Nam space STD not best practice remember uh I cannot go ahead and say C out a because a is not defined in that particular scope here in the same uh in the same way I can cannot use B in the main function because B is defined in that scope here whereas if I say something like int C equals 30 here I can access C in here and I can access C in here because the scope of C is the whole uh the whole file here now again we're not going to talk too much about functions but the idea of the static storage class is that um or actually let's talk about what happens if we don't use the static storage class whenever you define a variable like int Bal 10 what the program does during runtime is it allocates the memory uh for B and then it does something so this function does something uh and in the end we're done with the function we return a value here like return zero for example and this function is done and what we do is we get back to main we get out of the function scope here and what we usually do is we free the memory that was allocated for B so B loates memory does something and once it's out of scope it's deleted again we free the memory we don't need the memory anymore the block is freed and next time we get into my function we need to allocate it again so let's say I call my function multiple times what happens is I allocate uh memory for B I do something with it and then once I get out of the function I free this memory I delete B and then next time again I allocate I free I allocate I free and so on now if we do that uh if we don't want to do that if we we want to have a static allocation if we want to have the allocation being valid until the whole program is terminated uh what we can do is we can say static so static int a equals 10 for example now in this case this would be useless because the main function is all there is so essentially the scope doesn't end until the whole program terminates but uh if we define static and a in another function let's say in my function that we wrote down below uh a couple of seconds AO go if we do it there this means that once we get to that point once once we get to the point that a is allocated it's not going to be freed until the program terminates this is what the static keyword does it allocates the memory for a and it doesn't free that memory even if we get out of scope which means that next time when we get in scope or inside of scope next time we call that function the memory for a is already allocated and doesn't need to be allocated again it's already there and because of that this can increased performance of course it doesn't free the memory so it's you could say uh uselessly allocated it's allocated even though you don't need it so you're taking up more space in a sense but because of that you don't need to reallocate it once you need it again so if you know that you're going to use a function frequently and you're going to use uh a certain variable frequently and you don't want to uh reallocate it every time it gets out of scope or or actually you don't want to free it when it gets out of scope and you don't want to reallocate it once you get inside of scope again if you want to have this allocated all the time in order to save that uh that runtime that you're using to allocate the memory you can use a static storage class in order to do that and this can U sometimes increase performance so for the next storage class we're going to look at how variables are actually stored when we Define them let's say we say int a equals 10 what is happening here now first of all we look at the data type and we know we need four bytes for an integer so what we do is we go into the RAM and we allocate a block a memory block that has four bytes and we save the value or we store the value 10 inside of that block inside of those four bytes now whenever I say something like A+ equals 10 what happens is the CPO goes to the ram gets the value from that memory block applies the arithmetic operation and then stores that value back into the RAM and the ram is a very fast memory it's very efficient so it's very fast um but still sometimes you might want to have the actual variable closer to the CPU it's not always possible but sometimes a variable is going or an object is going to be accessed very very often and you want to have it close to the CPU and not save it in a ram but you want to save it in one of the registers of the CPU and if you want to do that you can use the register keyword register um in I equals 10 for example in this case if it's possible only if it's possible we're going to store that in I into a register rather than the ram now what do I mean by if if it's possible this means that sometimes of course we don't have free registers the ram is quite big nowadays so we have a lot of space there uh we're not likely to run out of ram unless you know we're programming a giant application uh but usually you'll always have some space in the ram however you're not always going to find a free register and if you don't find a a free register what's going to happen is you're going to allocate normal Ram uh memory so you're just going to access the ram again you're going to allocate a block of memory inside of the ram instead of a register but if a register is free so this is not a guarantee that you're going to save this variable in um or this object inside of a register it only says if there is a free register yes I'm going to use it I'm going to allocate uh I'm I'm going to store the object or the variable inside of that register instead of the RAM and because of that the access to that um to that uh variable is going to be way faster because it's actually in the CPU you don't have to go the way or the CPU doesn't have to go the way to the ram to access it it to process it and then to store it back into the ram it's actually in the CPU already in the registers of the CPU so this is again something that you can use to increase performance of course however don't abuse this quote unquote I mean abuse is probably not the right word here but don't overuse this don't store everything in a register just because you think it's going to make your program faster you want to select very few um objects or variables that you're going to store in the register because they're going to be accessed faster um than the ordinary uh variables or objects that are stored in the ram so if you know that something is going is going to be used very very often very frequently you might want to store it in register instead of the Ram uh but of course don't overuse this now one thing that you need to know here uh if you are going to use a register uh storage class is that you cannot use pointers and I know we haven't talked about pointers yet and you probably don't know what pointers are if you've never coded in C C+ plus uh go rust or something like that uh but essentially a pointer is just something an object let's say that points to a memory address and of course if the integer if the object is not stored in the ram but in the register it doesn't have a memory address of the ram so I'm going to show you real quickly what it's going to do uh we're not going to talk about pointers yet we're going to have a special video on pointers but essentially I'm going to show you that like that you can access the memory address of uh of an object here and probably we're not going to uh see the register here because the register is not always free actually it's pretty unlikely to have a free register uh and because of that we can see a memory address here this is the RAM memory address so in this case we didn't store it in a register we stored in we stored it in a uh in a normal memory block in the ram because there was no free register uh if there was a free register we would not be able to do this we would not be able to see a memory address here we would see something like register I'm I'm not actually sure what we get here I'm also not sure if we can use the size off operator but I think we should be able to do it um but the fact is if you store if you successfully store an object or a variable inside of a register you're not able to access it with pointers you cannot point to its memory address because it doesn't have an address it has a register it's a register for this variable so this is something that you can do to increase the performance of your program even more so last but not least for the sake of completeness we're also going to mention the other two storage classes as well well uh we're not going to talk too much about them just mention that they exist and the first one is called mutable mutable and it has to do with constants and classes and it's not important yet because we've not reached object-oriented programming yet and the second one is called threat local and it's something that was introduced with C++ 11 I think and this is also something that we're not going to talk about yet because it has to do with threats um again just wanted to mention them for the sake of completeness we also have mutable and threat local so that's it for today's video hope you enjoyed I hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you very much for watching see you in the next video and bye [Music]
Original Description
Today we talk about storage classes in C++.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Backend Performance
View skill →Related Reads
📰
📰
📰
📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Dev.to · IAMUU
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
🎓
Tutor Explanation
DeepCamp AI