Static Memory vs Dynamic Memory | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to nourish technologies this is universe today we are going to discuss a common topic and very important topic in programming languages of course this topic is useful for C language also so what is the concept is a static memory static memory and dynamic memory static memory and dynamic memory the people saying that a static is nothing but compile time dynamic is nothing but runtime memory runtime memory so generally people saying that write a static memory is nothing but memory will be allocated at the time of compilation a runtime memory means the memory will be allocated while application is running that is completely a wrong statement and most of the people saying that C and C++ languages are static memory languages and Java is a dynamic memory language that is impossible case whether it is a static memory or it is a dynamic memory so that is completely in the hands of programmer in the hands of a developer exactly what is a static memory means it is not the compile time memory a static memory means this is a fixed memory fixed memory once you allocate the memory you cannot change the memory nothing but you cannot increase and decrease the size of the memory a memory block size we cannot increase and decrease by using any operations impossible a fixed memory we can call it as a static memory answer what is the dynamic memory what is the dynamic memory means initially initially some specified amount of memory will be presented initial memory initial memory will be present and with our operations for example we are inserting the data or we are deleting the data depends on insertions and deletions of data elements the size may grow or the size measuring thing but the memory size is not constant is called a dynamic memory a very very important thing static memory means the memory will not be allocated at compile time that is completely wrong a static memory means a fixed memory the size is fixed no one can change the dynamic memory means the memory varies from time to time the block size may increase or decrease from time to time and from state to state that is called dynamic memory sir give me one best example that easily we can understand what is a static memory and what is a dynamic memory and the memory will not be allocated by the compiler right so we will see an example that proves very easily that compiler will not allocate the memory at the time of compilation see here for example I am taking one computer inside this computer this is a computer one this is computer one and here it is we are taking another computer this is computer to computer one and computer to any programming language no matter C C++ Java dotnet right see suppose Here I am writing one program a simple program I will write right inside the main function I am declaring one primitive variable a I am declaring one primitive variable a and next simply I am printing the a value in the next statement generally I have written you can consider as any language syntax C language C++ Java dotnet this is the program this is a program dot C consider once program is ready this is what we call the source code what we call the source code source code need to be compiled so we are passing all these instructions to the compiler then C compiler generates the file so generally if it is a Windows operating system it will generate the dot exe file program dot exe file program dot exe file so what is a compilation process means converting converting all the instructions high-level instructions into machine level is called a compilers task conversion only so once a program dot exe file is ready I just want to copy copying that file copying that file into pen drive into pen drive now in my pen drive that program dot exe file is there this is a pen drive flash drive secondary storage device a permanent storage device we can call and here it is I connected this pendrive to this computer and I copied this program dot exe into my pendrive now I just connect this pendrive to the next system and I copied into a second machine that is program dot exe file I copied there is no connection between these two computers understand there is no connection between a computer one and computer two just I compile the application in a computer one and I copied into the computer two we are passing all these instructions to operating system windows operating system operating system understand the executable file operating system will run the output I mean it will give the output it will run and it will produce the output what is the output it will print a for example here it is a value I am assigning 10 so in the second computer it will print that a value is a 10 now tell me in which computer a memory will be allocated for variable a is the question where a variable gets memory allocation in computer 1 or in a computer to write the answer is in a computer too in computer to a gets memory allocation and here it is initializes with the value 10 so now tell me memory will be allocated for this primitive datatype variable at compile time or runtime at run time if the memory is allocated at compile time right a gets memory allocation in computer one imagine it happened how can you access the a value from the second computer if there is no connection even internet connection is also not there then how can you access impossible so easily we can understand that there is no question of compile time memory in this world memory allocation in this world right but the only thing is compiler can recognize how much memory is occupied by the program while application is running in any system one more best example for example I downloaded one a dot exe file like VLC player I downloaded VLC player whenever I am installing into my computer if you look at the specifications if you want to run this VLC player some amount of memory is required some 30 MB memory is required or 40 MB memory is required in your computer like that it will give one message very clearly that means compiler only just estimate how much memory is required for the application Asian to run but compiler will not allocate any memory that is completely wrong statement memory allocation always at runtime only so then why they called a static memory and dynamic memory means a static memory is a fixed memory that you will fix dynamic memory is not fixed it will keep on increasing and it will keep on decreasing sir example for example if you take array if you take array in C language in two ways we can allocate the memory to array here first one is a int a are are the size is a 5 here it is a fixer size this is what we called a static memory for example int pointer ar-ar-ar-ar equals to using Tarlac using can lock 5 comma sizeof integer integer and we are typecasting into integer pointer so what it will do we are allocating the memory at runtime while application is running but still it is also called a static memory only memory allocation at runtime only but here you will fix the size at runtime this is the size you fix at runtime ok so here whenever you give the size 5 using catalog we are allocating the memory so can we store the sixth element impossible case in this case of 5 here also you can store only five elements sixth element storage is impossible sir then how can you say that catalog function comes under dynamic memory allocation concept in C language not a catalog function Carol function will take the help of another function is called reallocation function that is real log function is used to increase or decrease the size of the memory block array size if you want to increase our array size if you want to decrease that is possible with the help of real log function for that code programmer should write you should write the code okay so a programmer will decide a memory is a static memory or a dynamic memory if you fix the size that is called a static memory if you write the logic to increase and decrease the memory is called a dynamic memory but for example come to that Java come to the Java and here also here also if you want to declare an array how we are declaring int a are R equals to new int of 5 the people saying that whenever we are using a new keyword memory will be allocated at runtime of course memory always allocates at runtime only because the previous diagram already we discussed the best example here also memory allocates at runtime but you cannot say that memory is a very we cannot increase and decrease the size of the memory here size is a fixed size into this array can you store the sixth element impossible then how can you say that it is a dynamic memory impossible case this is what we called a static memory we called a static memory where is a dynamic memory concept in Java simple collections concept collections instead of arrays suppose if you take array list our vector whenever you are increasing the elements initial capacity of array list is a 10 but can we store only 10 elements impossible as soon as you store the 11 element the size will grow in vector easily we can see that using a capacity method right how much the size is increasing nothing but the capacity is increasing whenever you store the eleventh element right the size will be doubled nothing but ten become twenty twenty become forty so that is right so the size is increasing and decreasing sir who will write the logic already logic written that is what we called algorithms data structure algorithms implementation of a data structure algorithms is nothing but a collections framework concept so how they have written maybe they write a catalog function and real log function in ArrayList what logic they have written no matter the size is increasing and decreasing automatically means it is comes under die in a maker the size is a fixer you can't increase and decrease the size of the memory that is called a static memory okay so this is a completely about a static memory allocation and dynamic memory allocation so you people please don't think that a static memory means a compile-time memory right a dynamic memory means runtime memory at compile time no memory allocation compiler is intended to design to translate the instructions and to check the syntactical rules that is not developed for right allocating the memory for the variables or for the program memory always allocates at runtime at the time of execution only in any programming language but static memory means fixed in dynamic memory means it's varies right it will change from time to time from state to state this is all about a compile time memory allocation and runtime memory allocation thank you all for more videos please subscribe to nourish heidi channel
Info
Channel: Naresh i Technologies
Views: 222,608
Rating: 4.908864 out of 5
Keywords: C Language, Srinivas, Naresh IT, Hands on C Language, C Language Demo, C Language Tutorial Videos, Online C Language Training, C Language Overview, C Language Interview Questions
Id: 7icNeEZ8PDo
Channel Id: undefined
Length: 16min 30sec (990 seconds)
Published: Mon Sep 19 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.