Arrays - C++ Tutorial For Beginners #16
Key Takeaways
This video tutorial covers arrays in C++ for beginners, providing a foundational understanding of the language.
Full Transcript
it's not a g it's [Music] a what's going on guys welcome back to the C++ tutorial Series in today's video we're going to talk about array so let's get right into it now arrays are essentially just collections of elements of the same data type for example we can go ahead and say I want an integer array so I specify int and then my array or any other name followed by square brackets and inside of the those square brackets we can say how many slots do we want to have in that array so let's say we allocate 20 slots for example uh which means that we have an array with 20 spaces for 20 integers so a space for each of those 20 integers uh and if an integer takes up four byte in this case we would have uh 20 * 4 bytes allocated which means that we have a section in the memory in the ram in the in the stack so to say where we have the uh the array placed inside there and we can write values and read values from there but compared to other languages like Java for example we can not only do that we can also go beyond the limits which is not recommended we're going to talk about what happens there um but this is something that you can do in C++ you can go beyond the limit so if you allocate 20 uh slots you can also go to 35 for example but the problem is of course if we don't know what happens then and we're going to talk about that in a second uh but for now let's look at how we can actually access access the individual values here um so let's say we want to print a certain slot what we do is we say see out my array and then the respective slot for example seven uh now what you need to know if you have never worked with arrays is that arrays start uh or the index of an array starts counting at zero and goes up until the size minus one so the the the first index here would be index zero and the last one would be index 19 if you want to stay in the in the boundaries um and now the problem here is or it's actually not a problem but the the interesting thing here is that we're printing something that we haven't assigned yet and we just have this array we've allocated the space but we haven't set what should be inside of that space so what we did here or what the compiler or the system does here is it says okay this range here of memory this this uh section here is for this array but we don't do anything so this section may have already some values in there it can have values from previous stuff it can have some random values it can have zeros it doesn't matter uh we just allocate we don't change it so the values that are in there are still in there so we can go ahead and see what we get here in this case uh it's totally random and undefined we cannot really know uh in this case we get a zero so let's go ahead with a for Loop and print all the values and I equal z i less than uh 20 I ++ and we're going to print all of them and in this case you can see we have a bunch of random values here um these are all the values that were already inside of that section that we allocated for the array if we don't want to have all these values still in there we can also overwrite overwrite them with zeros by just saying equals and then two empty curly brackets if we do that and run this again you will see that it's full of zeros down here as you can see so this is uh one thing that you can do if you care about what's in there in the first place uh we can also go ahead and overwrite the first couple of um uh first couple of slots manually so we can say for example I want 10 in there I want 80 in there and I want 20 in there and the rest shall be zero so we can do it like that and um you will see that the first three slots now have these values and then we have only zeros so this is how we can create array and access the individual Fields so now let's talk about what happens if we go beyond the limit now first of all let me show you that it actually works so we can go ahead and say see out my array uh 50 even though we have only allocated 20 slots this works in C++ it it would not work in Java for example not sure if it works in Python I think python has some I think actually it doesn't even work in Python uh but but in C++ it works and you can see that we get some value and this is completely undefined we're somewhere in the memory that does not belong to this array we can still access it but what happens when we do it is undefined it depends on the compiler and the system now most compilers what they do is when you for example um access 20 or 21 it just goes to the neighboring to the next neighboring uh spaces in memory but this does not have to be the case we can end up anywhere and read any values and of course the dangerous uh thing here is that you're not just reading the stuff you can also write the stuff so you can say my array um 50 for example equals 200 now this works but what happens we don't know now in this case it just terminates which is even more dangerous than if it crashes because uh you can think about this like that you have the random uh Random Access Memory you have the memory where you have all your uh variables and important values and also jump back addresses so when you call a function for example and this function has to jump back so let's say I have some function defined and I uh call this function here uh what I do is I jump to that function and then I execute the code in the function and when I reach the end of the function I need to know where to jump back I need to know that I need to come back here and continue with the rest of the code which is down here and the problem is that um we have all of this basically stored in the stack which means that if I overwrite the uh if I go beyond the limits of the buffer of the array that I have I can also overwrite the jumpack address and this can happen by accident which is uh dangerous because you don't know what you're overwriting right I might have a different variable here which is very important in a equals I don't know some value here 10 for example uh I can overwrite that value by writing on this slot which does not belong to the array anymore uh I can overwrite the jump back address in the best case my program crashes and I see that I have done some nonsense because that's really the best case scenario the program crashes and uh some error happens and this is it but the worst case scenario or something that's a little bit worse than that is if it continues to run and you don't know what you have done right now because you know why are you changing this value what does that even mean this is some value that's there and you're just changing it and this is not a good idea and you should never be doing that and this is also one thing that people do that hackers do uh it's called buffer overflow now it's a little bit more complex than that but the basic idea is that uh if you have some input for example you have I don't know some uh I don't know if it works with strings uh it it should work with C strings but you have some user input here where you say CN s and what you do then is you can string copy this into a string buffer which uh or into a character buffer actually so you have a certain amount of characters that you can store let's say you have a buffer and an an character array like that character array of size 50 for example and then you take some user input that has a different size it's a dynamic size and the user passes something that has 100 uh characters what happens is you fill up those 50 characters and then you go beyond that and write what the user has put into the program now this is dangerous because the user can put instructions in there that uh overwrite the jumpback address and then execute the code inside of that buffer here which uh can lead to all sorts of different hacker attacks it's called buffer overflow you can Google it uh it's a very dangerous thing so you should never go beyond the limits of what you have allocated now one last thing that I want to show you is that you can also Define the size of the array by using a variable so let's say we have this int a here and we're going to see in the value of a and then we're going to allocate a slots and we can see that uh if I go from zero to a + 10 just to see that after that it's no longer allocated we're going to print all the values here so we're going to say STD oh forgot the STD here STD see out my array I STD end line and you're going to see that we're now going to have those three values here then a bunch of allocated values of size a and then we have 10 values that we're going to print that are not allocated so just that you can see that we have a dynamic size here so let's choose 60 for example you can see we have 60 zeros or actually uh 57 zeros or yeah 57 zeros or actually why is it 60 in the first place here oh 60 is my input sorry and then we have a bunch of unallocated values the last 10 here so you can see that uh we can use this to create a dynamically sized uh array however I don't think that this is supported in all C++ versions or in C even so sometimes this is not really possible sometimes I'm not sure what it depends on compiler or version or standard uh but sometimes this does not work unless uh the integer is a constant integer and sometimes uh you just need to pass um static values and if you want want to have dynamically sized values you need you need to use a function called Malo that we're uh maybe going to talk about in a future video but in uh the version I'm using here and maybe in the version you're using at home uh we can do it like that we can just create an integer assign a value and then use this is the size of the array 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 don't forget to 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 arrays 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
Related Reads
📰
📰
📰
📰
The no-code AI business stack that earns $5k/month on autopilot
Dev.to AI
GitHub’s April Changelog Exposes the Private-Repo Cost of Instant Reviews
Medium · AI
AI Did Not Kill Freelancing. It Changed What Clients Actually Pay For
Dev.to · Alcora
25 Best AI Tools in 2026 to Boost Productivity, Create Content & Make Money Online
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI