YouTube Video Transcript

1,681 words

Full Transcript

[Music] hello friend this is rotation watching cpp nerds video series on C++ and today's topic is structural pairing part two so yeah there is a part one of this particular topic and if you don't know what is that I mean what is the structure repairing please go ahead and watch that video first because we are going to discuss little advanced point with respect to structural pairing so if you don't know what is the structural pairing or you you have only heard about it and have few knowledge I would recommend please watch that video you will be getting that link somewhere here I guess so please watch that video first then you come and watch this one so if you have watched that video let us start this video so for this I need some struct and a here here will give integer X and double D and we need another struct let's make it B and we'll make it integer Y and E a here so this is very important part we're taking this E as a data member inside this B and yeah you might have guessed it what I'm going to ask you now is the size of B here can you guess it what is going to be size of B think about it pause the video and think about it so I assume you have pause the video and come up with some answer so let's compile this and check the output for this 24 why is it so let's check that but before that let's see what you might have thought so it is very easy to guess the size of this a here this is going to be 4 and this is going to be 8 byte but structural padding does not work like this so structural padding we'll see that okay what is the biggest size among these two double means 8 so it will create let's suppose this is the memory slot and it will create let's suppose 8 byte and try to place this integer X inside that so this integer X we occupy only for bites and remaining four bites will be wasted and after that this double D will come so another eight bite so these two I mean these two data types will result into 8 plus 8 16 bytes so the result of this one should be 16 okay it will search for the bigger one which is double here and then it will create block multiple of that so it created block of 8 and it entered this X inside that and then it will try to fix this 8 inside this remaining 4 but it cannot fix that that's why it will allocate another block and then this D will come here okay actually this 8 can start at memory location which is multiple of 8 so if you will divide this full memory from 0 to 7 here so which is total 8 and then from 8 to 15 so as you can see that this D will start from the memory address which is multiple of 8 so this 8 is of multiple 8 of 8 ok so it will start from here this is another way to remember this and these things I have explained in my previous video I mean first part and I have given so much of extra points related to this ok so now let's come to this structure B so as you can see that we have this integer which will take 4 and this a which is of size 16 so it is 16 and as I said here it's on the biggest data type which was 8 so it started creating multiple of 8 okay but if you will see here according to that statement it should start creating multiple of 16 bytes I mean the first block should be of 0 to 15 but it is not like that don't miss this part ok I'm discussing the false case here it won't create 0 to 15 I'm in the first block of size 16 and place this 4 here and I mean this Y here and some space will go wasted and then this a will come here no no no this is not going to be like this so let's discuss the actual thing here the actual thing is this structure B will try to open this structure a here itself what do I mean by opening it it will check which is the biggest data member inside a which is happened to be double here which is of 8 bytes so again it will create it by block not 16 by block I guess you are following me so let's suppose this is 0 to 7 8 bytes and it will try to place this Y so Y will come here and this much of memory will go waste you got this point right when there is a situation like this one structure is inside another structure then how to get the size it will try to go to structure a and it will find that which is the biggest data member here it is 8 byte I mean which is double so it will allocate 8 byte block first and place this one inside that and remaining place will go waste and then it will place this 16 so from 8 it should be 2003 okay so this a will come inside this so 0 to 23 it is 24 that's why we got 24 there and this is how it work and you might think what if here I am having another integer let's suppose K then as I said if suppose there is some integer K after this integer Y then what should be the size of this B here it should be 24 only because as I said it will try to open a and it will find that eight I mean the biggest element and that is eight so here it will first allocate eight byte and try to fix why inside that so why will take only four byte remaining four byte will go for K so k will come here and then a will come here so now there is no wastage if you want to see that let's see that I will place one integer k here and let's compile and check see it is still 24 but wait a minute what if you place this one here instead of like that what if you place it sorry place it like this then what would be the size of this one I will tell you the size is going to be 32 let me compile that see it is 32 now I will explain why it is 32 be ready for that so let us assume that we have integer X and then this a and then integer Y then Y it is 32 as I said there is this a here so it will try to open this and we will get 8 so it will create multiple of eight blocks so first it will go and locate the memory it will create eight block so 0 to 8 and it will try to accommodate this X inside that which will come in half because this integer is of 4 bytes so X will come here now as I said this e is of 16 byte it cannot come inside this remaining 4 byte so we have to create another block of 16 bytes so it should not be 0 to 8 it should be 0 to 7 you might be shouting there but I was not listening sorry so it is 32 sorry 23 so from 8 to 23 will have a here okay now we will have this integer Y so we have another eight byte for this Y here and half will go waste while we are creating multiple of eight because we have this a here and this is the biggest data member inside a so you might have got the answer for your question why having the similar data members suddenly increased your structure size so if you will have the same data structure integer X and integer Y and a then this will result in 224 and the same data members but if you have changed the places it will result in 232 so place is really very important when you are creating any class or structure and how you're managing your data members inside that I mean how you are placing them it should be in sorted order okay so you might be thinking why I am uploading this video after 2 months so actually I have a reason for that and I wanted to show you that reason so let's see that actually my brother has also come into this YouTube stuff and where is that oh I wanted to show you his channel and I am doing a free advertising bought game actually he is a gamer he likes to play games and currently he is playing game only and he used to play pub G too much so there he is maybe he died in this game and he's watching his friend playing dad so his channel name is bought King if you like to watch pub G game in your free time please try to support this channel he plays really well actually he plays very quick so I like that part and I was also gamer when I was in B deck so again guys if you watch pop G videos in your free time please consider watching this channels video the channel name is again barking ok this is the channel name so thanks for watching this video and if you any doubt please comment I will be very happy to answer your questions thanks for watching bye bye see you in the next videos [Music]

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 tUAGceoY0R4 Transcript | YouTubeTranscriptFree