okay now let's move on to talking about volatile and const so let's start with the volatile keyword first uh let me go to a new page and okay so the general answer about the volatile keyword when asked or when inquired is that it helps you to uh skip optimization on a variable so if we have let's say in a this might get optimized but if we have volatile int a you know people say that a will not be optimized out well what I want to do now is provide a picture as to what happens at the system level and personally I also feel this is an end Wy sort of answer and it this particular one right this particular one right like what does optimization mean and why would you care if it's optimized or not and right so as essentially the the picture that we share with you now is the mechanics behind the scenes that's happening and uh that is like a good answer to provide people okay so let's go back to discussing about the system a little bit so we have the memory we have the CPU and as we saw in our you know the god Bol uh demonstration uh the CPU has register resistors internal resistors like x0 X1 X31 things of those nature what happens now is when you have an a variable let's say in a which is in the memory somewhere let's say value 10 and let's say we somewhere have A++ A++ A++ so think of what happens at the system level a will get fhed into for example X1 then all the CPU has to do is go on incrementing X1 in N number of times so if we had A++ for let's say three times the CPU can essentially retain the value of a in X1 and go on incrementing it now when we and this is the normal case now when we Mark a variable to be volatile what it really means and this is the explanation you should provide when ask is it avoids the variable from getting cached into the CPU and cached into the CPU is a very tricky thing the right thing to say the right statement to say is it avoids the variable from getting cached in the CPU registers right that is very important uh so what what what would it mean in our case in the current case so if a were volatile the value of a is feted into X1 it's incremented so X1 becomes 11 and then that value is returned back or pushed outside the CPU that is important it's pushed outside the CPU CP and the value of 11 is essentially sent outside the CPU and I'm saying outside the CPU for a very specific reason which we'll come to in a bit so next time around when it wants to modify the value of a again this 11 is fetched back into the CPU registers incremented and sent out so this load modify store operation is forced as a result of volel keyword and that is the correct answer and what I now want to briefly talk about for hint is the role of caches so let's say somewhere here we have an Elin cache so what will happen is the first time we fetch the value of a it will come through the cache and when we modify and write back a it will be written here notice that the value of a which is now incremented to 11 is sent outside the CPU it doesn't retain in the CPU register and next time we want to modify a again the value of 11 is feted from the caches not from the main limit and that is why I was saying that volatile essentially implies that the variable will not be cached in the CPU registers it can be cached in the caches that can happen but it won't be retained within the CPU load into the CPU register modify it do the computation and send it out right that is what the volatile keyword means do you want to add as to you know what the motivation and why not uh so I think the summary would be that the volatile keyword can control your code and change the behavior into hey instead of reusing the values from uh the register directly maybe it would be a better idea to go fetch it modify it and again store it back and do that every reference of that variable but because it's a code it has no control over your Hardware which is which is where the caches sit right so if your value is cached in the cache L1 L2 or any other cache then it will get the value from there what volatile keyword sort of guarantees you is that hey I'm not going to use any stale or any value in my register so to speak I'm always going to issue a fetch instruction get it and then store it back perfect and and you know where it might be helpful is if you have two CPUs each one of them you know trying to use the latest value then what happens is the latest value is available outside the CPU right and this CPU can go fetch it from here modify it and this CPU can go and modify it won't happen that you know both of the CPU are incrementing their own internal copies internal meaning the one in the registers and be out of syn now again you know there are more conditions to having two CPUs modify same memory location which is you need a mutex and things of or you know Atomic reads and wrs more on that later but the idea is don't keep it within the C send it out that's the first step right yes and one of the areas in embeded where it is used is for peripherals H so what happens is when you are let's say wanting to read and write to a u register for example you don't want that Ur register's value to be retained in the CPU register you want to fetch it do whatever with it and if you have modified it and you want to send out the modified data well you should be sending it outside the SE so that is one thing so when you're dealing with the peripherals ensure or keep in mind that your peripheral uh registers of the stru that is mapped on the peripheral the the elements within that should they be volatile or not just be mindful of those yeah and believe me you know there this is like the I I would say the biggest roadblock for many people like they have either you know incomplete understanding or some misguided understanding of what volatile keyword does and if you have nailed this down then congratulations I think that was our intent for this particular video so congratulations and then you know just to conclude then go ahead sorry complete your thoughts sorry on the peripheral side P I think you had asked me about the motivation and you know I was just thinking of some nice example I think peripheral peripheral one is really nice but one other uh sort of example which we can imagine is maybe a GPU and a CPU so CPU is preparing you know the raw data for GPU to render and uh the architecture is such that hey once GPU is done rendering it it will set some flag in a memory asking GPU for the next frame asking CPU for the next CPU for the next frame that hey provide me the data for the next frame I will also render that up corre this is the flag which CPU is also looping on so so CP is waiting hey if this flag becomes one I need to provide GPU with the new frame now think of a scenario where you are loading that flag once in your register and it and you're just looping it there that while this flag doesn't become one I'm just going to keep looping and you will never come out chances are that you will never come out yes and you the code is stuck nobody understands why that's why okay so then and very common thing that happens world right when any two agents are like spinning on volatile is the keyword that you should consider you should first you know doubt like hey yes and then the second one would be cashes but you know that's for some other yeah that's at the system level Okay cool so with this we can conclude the volatile part what I wanted to briefly also speak about is the const con um so what I want folks to remember is cons simply means that once assigned uh let's say once assigned the variable cannot be reassigned this is for so the compiler will just try to make sure that your variable that you have declared as a const um there there is no assignment happening to it it'll throw an error saying hey we are assigning value to a const variable now that said const doesn't mean doesn't necessarily mean that the value cannot be changed right again based on the based on the inputs of not inputs per based on the disassembly that we have seen if a for whatever reason happens to be in the RAM and you have the pointer to a then you can attempt changing the value of it using the pointer the compiler wouldn't complain about that right now how the code actually behaves well that's undefined sometimes it might go through sometimes on different systems it might block but the whole point is a const in C doesn't mean constant it simply means once assigned doing the right definition definition with initialization once assigned uh no more modification of that variable using the assignment op so direct assignment would be blocked by comp forb and you can play around and have you know a smart ways to still do and go and the maybe like pointers and such and you know if you have free time you know we encourage you to try it out as an experiment and see what happens and the reason why PE said it is unknown is because on uh uh rather on hosted environment recall hosted environment is where your code runs on top of an OS right we do not know for sure what OS does to this sections because the constant a equals 10 tells compiler that hey this might be a readon memory because I don't see anyone is going to change it so it's it's better to put it in some readon attribute with readon attribute and if you attempt to then write it your Hardware is going to generate fault if let's say OS behaves that way uh then you can't modify it but in some you know bare metal OS where this restrictions are not there you are free to go and modify could work yeah things could work out differently there so long story short it's the behavior is undefined those perfect so I think this is a good and the question you can sorry go ahead on the con part the question or you know the disc the probing part for you which can uh which you can encounter is hey is what is the behavior of const does that mean that this const variable can never be modified and the answer to that would be it depends like whatever right right and then you know you can reason about whether it's in the read only whether it's in the RAM do we have the address to it is it guarded by the OS on bare metal if it is not guarded someone answers me with this depth I would be flattered by that fair I would be flattered as well it it does signal as to someone knows what they're talking about they have like a deeper understanding and that's usually you know a very good again you know yeah Engineers should know that in all right perfect so this is like a good point to conclude this lecture act and then in the next one let's move on to you know tips on bit manipulation yeah perfect
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact