YouTube Video Transcript

2,650 words

Full Transcript

Hello friends welcome to another C++ interview question and this question is what is structural pading and packing in CN C++ actually this is going to be a really very interesting topic okay because I have created lots of document here just to make you a little more comfortable okay so first of all what is padding and packing okay so padding is like you will be adding some extra bit to some size and packing is like you don't want to do the padding so you will be doing packing okay instead of adding extra bit you won't add any extra bit to the size of your class or structure and all that okay so first of all just answer me what would be the size of this base let's suppose you have created this base just forget about all the functions and all just this is your base class and let's suppose you have these four data members like character character integer character so one bite one bite 4 bite and one bite so total seven bytes are there so if you will print this size of Base it should print seven right so let's go and check that so if you will compile this code and run this code oh my God what it is printing it is printing 12 okay whether it should print seven but it is printing 12 and let's suppose I'm just copying and pasting this from here to here and you will just recompile this again and you will run this so this time it is giving you eight so why it is changing like this like why your base size is changing this time also it didn't give you the correct output but it is taking a little less size so what is this all about why your base is not seven as you expected so just to assure you that it is actually seven we have to write something like this and we'll compile it and we'll run it we'll see seven okay so for this moment I'll just com comment this so that we can just understand why this is happening okay so what you did you just did the packing here okay you tell to the compiler don't do any padding here so you just did packing okay so why it is working like this just take an example here let's suppose this is your base okay it is having one character another character and integer so it will be setting bytes like this the memory layout will look like this this is one bite okay a is one bite so it occupied one bite so the zeroth position is occupied B is also one bite so it occupied another place after that two empty spaces left and after that it is assigning this P oh my God I should have return return this I here so okay so this I is nothing but integer so it will take obviously four bytes so that's why it is occupying four bytes here but why it is having this two extra bits sorry bites empty here why it is working like that and if you will see second case here let's suppose you having one character here and integer I here and then character B here then what you will do what I mean what this compiler will do compiler will assign first bite here after that it will keep four empty bytes and then after that it will assign this I oh my God so instead of changing this I will just simply change this okay this P so as you can see this p is occupying four bytes as it is integer and after that b is coming okay and again this three by is empty so if you will print the size of this base it will tell you 0 to 11 12 bytes okay so this is how it works but why it is working like this actually the point is when you run your program it gets divided into pages and uh it is stored in your hard dis and when you are double clicking something like uh you're running your VLC player you're running your any browser or something it will load into the Ram so when it is getting loaded into the ram it is called segments okay so when so just okay I will show you one process diagram here I have kept it ready for you so let's suppose you double click so you created a process so that process will first come into the waiting queue after that it will go into the running queue so it is running here okay and when it is requiring any IU operation it will get blocked here after that when that IO operation is triggered or that device is ready you will get one interrupt and you will again start waiting and you will again go to the running state so this is how this operating system actually works okay there are lots of things but this is the actual thing okay and let's suppose your processor is of 32bit so have you ever imagined that why it is called 32bit and 64bit it's just because 32-bit processor means it can process 32 bits all together at once at one clock cycle like if you are giving one clock to the system it can process 32 bits and and that another 64bit means you can it can process 64bit Al together if you will give one clock to that okay so that is a reason so in that case when it is going to process four bytes the computer researchers found that if you will keep your data like this like seven if you will keep your data like this it might be looking something like I will just copy and paste this situation here and uh I'll just simply remove this two okay so in our thinking it should look like this but what your processor read it read four bytes all together so processor will take first four bytes okay and it will read so when this four byte has gone to the processor it can read A and B without any problem but let's suppose you want to read I okay which is this I this these four so in that situation processor has to read this one this portion to get these two bite of I and after that processor has to read this two okay so that's why there are two read okay to the memory so that's why it is a big penalty so just to remove that penalty we pad the extra bytes okay so now let's see if this is the situation here and you want to access a or b or something this first four es will go to the processor processor will read A and B without any problem just need a shifting here okay and if suppose processor want to read i processor will read another segment or page okay so we divide whole memory into segments okay so if it is stored at hard disk it is called Pages if it is brought to the memory it is called segments okay so that is the only difference and we do paging concept and that paging help you to map pages to the segment okay because your processor always generate logical address not the physical address okay it doesn't know about your RAM it asks The Logical address okay so this is how works I mean if you are not good in operating system you might find it little difficult to understand but in just simple way you need to understand one thing processor will read 4 by okay if it is 32bit just remember that if it is 32bit it will read 4 4 by okay and you cannot tell like why it is not reading from here okay no this when the pages were created it was created like this first these four by went to one page okay or one word size that is also uh proper terminology and another four bytes let's suppose here it will do the padding actually okay then another will go to another page okay or another word I think I should use the word here here so this is one word and this one is another word so you cannot say why it is not reading from here no processor doesn't have that Advantage but nowadays processor does have that Advantage nowadays scientist and processor creators are telling that there is no penalty even if you don't pad any extra bite but I'm not that sure about that but I have read that article somewhere so I'm just telling you that okay just keep that in your mind and there is a rule let's understand the rule okay so how data gets memory slot this is the rule for that one byte can go and can be stored at multiple of one memory slot so what do I mean by that so let's Suppose there is a multi there is a memory slot it start from zero and 1 and 2 and 3 4 5 6 7 8 9 and 10 okay so this one can go anywhere because one is the multiple of all so one can go anywhere but two can go at multiple of two only so it can go at zero location at loation at four location at sixth location two cannot go at first location okay so that is the memory mapping work okay if the size of your data type is 2 byte it will never go to First byte position that is how the memory optimization and all these algorithm and all those stuff work internally so if it is 4 byte it will go multiple of four like it can go to zeroth location so if it is going at zeroth location that means it is occupying from 0 to 3 so that's why it is requiring multiple of four okay from 0 to three and then if it is already occupied like uh from 0 to three suppose uh this one was already occupied then it will never take 1 2 3 no it will search for four so four but still it should get consecutive 4 empty space to get into this slot okay so this is how things work and in case of 8 byte it will always check consecutive eight empty bytes but that should be multiple of eight like it should be starting from zero position or from the eighth position it will never go to either of 1 to 7 okay so this is how things work so am I left with something one word is 4 by and 32 bit processor and one word is 8 by in 64bit processor so that I already explained you word is nothing but pages and uh I can explain you or or just leave it uh no need to go into that deep because otherwise I will become operating system teacher here for now you just understand this much only that padding gives boost to your program like it will get readed by 4 byte 4 byte 4 byte if it is 32bit processor okay so first for 4 by will go like this and yeah one more thing I wanted to tell you if it is like this then zero bite will get a here first bite will get B here third bite will sorry second bite will get C here and another time that third bite will get padding okay so here you will get one padding and after that this integer is going to get four bite so it will be like total 8 by so that we can just simply check here by compiling okay we will compile this oh what am I missing in here undefine constant what is this am I doing something okay I will just remove this we will go up I will compile check yeah eight why is eight I already told this is first bite second bite third bite fourth bite is padded and now this is starting from the fifth I'm mean the fourth bite because the indexing start from the zero okay so it will look something like this a is at zero position B is at first position is at second position and there is one padding and then third is coming third is padded okay and then I is at 4 5 6 and 7 okay so I I I so this is like this but let's suppose if you will remove these two like B and C then how it will be it is not going to look like this okay it is not going to look like this it is going to look like this why because it pads multiple of the maximum available data type here okay so I just give you the example here I will just simply remove this and what do you think what it should be like before it was having B and C here like B and C so if you'll remove this B and C from this place then it won't be looking like this why because I just simply compile oh my God I always mess up things and then we'll just simply compile check 8 again even if you removed B and C it is not changing it is again it was eight and it is again eight okay so what is this why it is not changing the reason is that only what I told you there like uh this is a 4 by data type this is a 1 byte data type so this this 1 by data type already occupied the zero place now this 4 byte data type will look for four consecutive bite so it will start looking from here but it will not get so as it is 4 by data type it will get stored multiple at four okay so that's why it is starting from four here that's why it is having this size eight so let's suppose if you are using some short or something like s h o r t s in that cas what should happen like s is of two bite okay so in that situation it would be like this only four because s is two so what did I said if it is two here then it will look for the multiple of two so what what do you mean by multiple of two here multiple of two is zero position Z is already occupied by a one is not the multiple of two so this two is the multiple of two and two and three was empty so it will occupy that size so the size would be these four bytes so that we can see here I'll just simply compile no before that I always forget this one I'll just simply compile fast four okay now I think I made my point guys oh is there anything left I don't remember yeah and even if you are interested to remove this one okay remove this extra ping you are just going to use this pragma pack here uh okay compile and check see now you are having three so one and three you are saving so much of memory but it might give you a big penalty okay but researchers claim that now there is no need of doing that extra pairing but still you should know this concept to answer to your interviewer that's why I'm explaining this okay so I think I'm done here thanks for watching bye-bye

Need a transcript for another video?

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

YouTube Video QSuBwGmFQqA Transcript | YouTubeTranscriptFree