Golang Tutorial 3 - Golang pointers explained, once and for all

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Been programming for years, but have always enjoyed watching videos either for stuff I don’t know so I can learn or for things I already know to refresh my memory. This video was excellent, showing everything that happens in memory with nice animations and code examples! It took me months when I first started learning programming and C++ in my case to understand everything happening in memory with pointers and references. Might even subscribe to her channel!

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/VacuumSpace8 πŸ“…οΈŽ︎ Feb 27 2021 πŸ—«︎ replies

What program did you use to create this tutorial? Super well done

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/abstart πŸ“…οΈŽ︎ Feb 27 2021 πŸ—«︎ replies

Great Content, Thanks

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/algorithmAcademy πŸ“…οΈŽ︎ Feb 26 2021 πŸ—«︎ replies
Captions
hi um today let's talk about pointers along with some very basic memory allocation concepts um i want to know if you can explain what actually happens in the memory when a function is called like explaining the difference between stack and heap allocations and also explain how pointers relate to any of that if not then i think this video will be very helpful to you and if you're like a total beginner that's also fine because i'll be explaining everything from scratch starting from the basic syntax using very simple examples and after that i'll be moving on to intermediate concepts okay so yeah let's get started if you think of a variable in programming it would look something like this it's going to have a name type and value and it's going to be stored somewhere in the memory it's like a box that's stored in a warehouse and inside that box there's the value and we give the box a name and a type but we also give the box an address the address is going to represent the location of the box inside the warehouse so if we need to go and get that box we can immediately locate it and get it if we have the address as we declare an int with the name foo and value 23 this is how it would look like can you see how the new variable is given an address this address is the value of the pointer that points to foo and if we change the value of a pointer we can also make it point to different things i'll explain in more detail with the tour of go example code first we declare two variables with the shorthand declaration and assign these integer values 42 and 2701 i and j is going to get some space in the memory and they'll also get an address if you print out inj you'll get the value like what's in the box so this is just very normal stuff nothing special here but if you want to check the address for inj you can do that by adding an ampersand in front of the variable name like this you will be able to see that it's printing out the address when you're reading code in your head it's easy to just read the ampersand as address of so we are printing the address of i and the address of j this time instead of printing out ampersand i let's store that in a variable p as we assign the address of i to p it is now a pointer if we print p out it will just give the same result as ampersand i the address but what if we print this out star p the star can be a little confusing because they're used in two ways if it's in front of a type name like this then the whole thing including the star it's going to represent a type the whole thing it is a pointer type and we say that int is the base if the star is in front of a variable which is a pointer type the star acts as an operator and returns the value that the pointer is pointing to so if we print out star p that's going to give us 42 because it's the value of the variable that p is pointing to we also call this dereferencing so the value of p is the address of i and star p is the value at that address which is the value of i so what happens if you try to change star p changing star p to 21 would be changing the value of i which you can confirm by printing out i next we assign the address of j to p we can do that because the type of p is a pointer with the base type integer and j is an integer so that's why this is possible and now if we do something to start p then we are modifying the value of j here we are assigning star p to star p divided by 37 which is the equivalent of just changing j to j divided by 37 so we can also just confirm that by printing out j and you can see that the value has changed so why do we need pointers can't we just modify j if we just want to just modify j so why do we use them well it's efficient to store the variable in one place and access it from here and there you can share the variable and update it in multiple places in the program that can be more efficient than copying the variables every time you need to make changes to them or use them and if you want to manipulate a value across function calls you need to use pointers so what does that mean let's talk a little bit about memory allocation to explain that when we try to execute code a go routine is created and each go routine gets a stack of memory a go routine is an independent path of execution and you can just think of it as a very lightweight thread whenever the go routine makes a function call a part of the stack is going to be allocated and we call that a frame let me show you how this works with an example let's write a function that can square the input and this function is also going to print out the address and the value of the output and then in the main function we declare a variable a with the value 4. the main function is also a function so we get a frame in the stack for that so this is what it's going to look like and currently this frame is the active frame so we're following through the main function but then we call square val the function that we just wrote when we call square val with a as soon as we call this function the stack will allocate a new frame and the go team can work only inside that new frame it cannot go get data from other frames or other stacks or anywhere else this is good because if we isolate each frame we are guaranteed immutability which means that it's safe there is less danger of the variables getting modified throughout the program then how can we access a because like the active frame needs to know what a is to to square it and print it out actually strictly speaking we can't we can't access it instead we have to copy the value of a into the new active frame and inside the active frame that value is going to be called v and we can modify v square it print it and do whatever we want with it but because we're making the changes inside the active frame it will not change anything else in the program outside of this frame the mutation will only happen inside this isolated frame but this is not going to be so efficient because we need to copy the arguments each time we make a function call and right now when the square valve function call ends and the active frame goes back to the main function a will still be four but what if we really want to change a itself in the main function we want to get our hands on a and not just the copy of it well this is where we start talking about pointers this time we are going to write a function that can modify this variable in the main function by saying go and change the value at that specific address let's write that function we're going to call it square add i just called it add because we're inputting an address instead of normal integer so we're going to put this address as the input parameter and we'll call it p so the type of the input is starint the star here is not a dereferencing operator star int itself is just one whole token we want to square the value of what's at that address so we need to put a star in front of p if we want to say the value at p which in this example is going to be a and then let's print out p which is an address and the value of what p is pointing to by saying star p and when you call this function you need to pass in an address not a value what you need to pass is ampersand a because ampersand means that you're passing in the address of a let's see what happens in the stack when we call square add instead of copying a we are copying the address of a and assigning it as a pointer p in the frame and that pointer is pointing here across the boundary of the frame and this is how we can modify the value of a in the current active frame by using star p after we finish calling the function we move to the main function again and everything under the active frame then becomes invalid meaning that if we make another function call this space will be overwritten and go will set all the variables to a zero value for the new frame so that we won't accidentally be using any random garbage values when we're using value semantics like the example we saw before with square val it was fine there's there's no way a can get mutated but when we start using pointer semantics we need to be careful because there is more possibility for the variable to be mutated in a way we didn't intend today in this video you just need to understand that when you use pointer semantics you're giving up the safety of immutability for for more efficiency now that we understand how pointers work in functions and we also learned about how it can affect variables in the stack let's start talking about heaps okay this time instead of comparing value and pointers as an input parameter we're going to see the difference between returning a value and returning a pointer let's first define a structure called person with a name and an age then we will make a function that declares a person as m and initializes it with no name for name and 50. so those are going to be like initial values and init person is going to be a function that initializes the the struct with these values and then we going to return m and then we call initperson in the main function and because we need to check the results we'll print it out and we can check that the person m was successfully initialized to no name and 50. when we call this function in main the stack is going to look like this so currently the active frame is main function then when we call init person a new frame is created and allocates m and then we change the values in m because of the isolation of init person frame we cannot simply send m to main instead we will be making a copy of it in the active frame so that's what happens when you return a value but instead of returning a value let's return the address of m so we're going to make a little bit of changes to this function and it works the same way but instead of copying the value this function is going to make a copy of the address of m to the main function frame but do you notice something anything wrong here oh so we have an address pointing to m but after init person call is finished that frame is going to become invalid so the address we copied into the active frame it's going to be useless because we don't know what what it's going to point to so that can be a problem and this is where heaps come in so heaps is going to solve this problem for us and just to avoid confusion the name heaps has nothing to do with the heaps that we learned in data structures it's a different thing they just have the same name so the compiler will analyze what's going on and figures out that this might cause a problem and it's going to copy m to the heap then the init person function will return the address of m in the heap and after the return when the address of m is copied to the frame of the main function we would be able to access m with that address just to confirm this let's print out the return value of init person and also let's print out the address of m in the init person function you can check that they they share the same address so our problem is solved but we are doing this in the cost of heap allocation which can be a burden for the garbage collector and it can cost us performance the stack doesn't need garbage collector because it's self-cleaning just like what we saw when when a function is cold and finished it just discards the frame and everything inside it when another function is called this space will be used by another frame and it's going to be totally wiped out because it will be initiated with zero values so it's easy for stacks but if we put something on the heap that's going to be another job for the garbage collector a garbage collector automatically sets the memory free for ones that we don't use and just keep the ones that we need and there's a specific algorithm for that but we won't get into the details of it but just remember if we start giving too much work for the garbage collector it can affect performance okay so i think we covered some topics behind memory allocations and pointers we also went through the basic syntax and talked about stack and heap applications um yes so that's that's that's for today thank you so much for watching until the end and i hope you like this video if you did please like and subscribe thank you bye [Music]
Info
Channel: Junmin Lee
Views: 27,079
Rating: 4.9927039 out of 5
Keywords: golang tutorial, golang, go, pointers, programming, golang pointers, golang beginner, programming tutorial, golang beginner tutorial, memory allocation, stack and heaps, garbage collector, heap allocations, golang tutorials, golang tutorial for beginners
Id: sTFJtxJXkaY
Channel Id: undefined
Length: 13min 49sec (829 seconds)
Published: Wed Dec 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.