Arrays - C++ Tutorial For Beginners #16

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·5y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches the basics of arrays in C++ and how to use them to solve problems. It provides a foundational understanding of the language and is suitable for beginners. By watching this video, viewers can learn how to declare, initialize, and manipulate arrays in C++.

Key Takeaways
  1. Declare an array in C++
  2. Initialize an array in C++
  3. Access array elements
  4. Modify array elements
  5. Use arrays to solve problems
💡 Arrays are a fundamental data structure in C++ that can be used to store and manipulate collections of data.

Related Reads

📰
The no-code AI business stack that earns $5k/month on autopilot
Learn how to build a no-code AI business stack that earns $5k/month on autopilot, leveraging interconnected AI tools for content creation, customer payment processing, and more
Dev.to AI
📰
GitHub’s April Changelog Exposes the Private-Repo Cost of Instant Reviews
GitHub's April changelog reveals that private-repo reviews now consume AI credits and Actions minutes, affecting automatic review rules
Medium · AI
📰
AI Did Not Kill Freelancing. It Changed What Clients Actually Pay For
Discover how AI has changed the freelancing landscape and what clients now pay for, to stay ahead in the industry
Dev.to · Alcora
📰
25 Best AI Tools in 2026 to Boost Productivity, Create Content & Make Money Online
Discover 25 AI tools to boost productivity, create content, and make money online in 2026
Medium · Startup
Up next
How I Use AI to Write Facebook Ad Scripts That Actually Scale
Nick Theriot
Watch →