Structure Padding in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ส.ค. 2024
  • C Programming: Structure Padding in C Programming.
    Topics discussed:
    1) Memory allocation to Structures.
    2) Need for Structure Padding.
    3) The concept of Structure Padding.
    C Programming Lectures: goo.gl/7Eh2SS
    Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
    Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
    Contribute: www.nesoacademy...
    Memberships: bit.ly/2U7YSPI
    Books: www.nesoacademy...
    Website ► www.nesoacademy...
    Forum ► forum.nesoacade...
    Facebook ► goo.gl/Nt0PmB
    Twitter ► / nesoacademy
    Music:
    Axol x Alex Skrindo - You [NCS Release]
    #CProgrammingByNeso #CProgramming #StructuresInC #StructurePadding #Structure&Union

ความคิดเห็น • 255

  • @ahamd19
    @ahamd19 4 ปีที่แล้ว +96

    speachless!! you are soooo goood in explainging. thanks a lot

  • @John-sl5zz
    @John-sl5zz ปีที่แล้ว +10

    your style of pedagogy has helped me in overcoming phobia of coding.
    the sort of knowledge you are imparting is enriching our lives.
    thanx a ton & we look forward to having more such amazing content.
    kudos to you.

  • @devugoel8698
    @devugoel8698 ปีที่แล้ว +13

    This was the best. Saw many tutorials, but understood the concept here in one go. I rarely comment on videos, but really had to appreciate you here. Thanks man!

    • @user-vy8py5ux9f
      @user-vy8py5ux9f ปีที่แล้ว +2

      I couldn't agree with you more , It's awesome how he explained it.

  • @WistrelChianti
    @WistrelChianti 3 ปีที่แล้ว +20

    Brilliant! I particularly liked the repetition when driving home a particularly important point. Superb work (and exactly what I was looking for - was searching for a good detailed explanation on structure storage in memory).

  • @nguyenxuannghia8089
    @nguyenxuannghia8089 4 ปีที่แล้ว +18

    Thanks God ! Your video saves my embedded programming career

  • @andresvallelisboa5511
    @andresvallelisboa5511 4 ปีที่แล้ว +9

    Thank you very much! I understood it. I Love the format you chose for the video, a black background, a representation of the memory bytes and everything was explained in a very direct and simple way.

  • @anshhmehta
    @anshhmehta 2 ปีที่แล้ว +5

    Perfect video to understand the importance of padding and its uses.

  • @sunilkumarreddy6537
    @sunilkumarreddy6537 4 ปีที่แล้ว +4

    Sir it is a good class for me. Thank you sir,upto now I didn't no the background allocation of memory now I got clarified. SIR ONE MORE THING, I WANT TO sll,dll,cll,sporting techniques tree's,graphs,bss. Pls... upload sir, one of ur student, thank you sir.

  • @michaelhernandez2075
    @michaelhernandez2075 ปีที่แล้ว +2

    trying to read how this works is way harder than having pictures and someone explaining. Thank you.

  • @bendono
    @bendono 11 หลายเดือนก่อน +2

    Padding is not defined in the C (or C++) standard. This behavior depends on the compiler. It can also be configured too. For example, VC++ has the /Zp and #pragma pack options while GCC has -fpack-struct option.

  • @ArjanvanVught
    @ArjanvanVught 5 ปีที่แล้ว +13

    Missing to explain __attribute__((packed)) with unaligned memory access which is possible (with ARM).

  • @manuelconte2127
    @manuelconte2127 ปีที่แล้ว +3

    It is a good habit to put first the biggest type in ascending order to help compilator !

  • @sujitsatpute3741
    @sujitsatpute3741 3 ปีที่แล้ว +5

    You are explaining 💯 times better than my college professor......

    • @SJ_46
      @SJ_46 ปีที่แล้ว +1

      1000 bro

  • @manoharbompella418
    @manoharbompella418 3 ปีที่แล้ว +3

    Explained very detailed, Cleared every doubt.
    Thank you

  • @andistheinforitbutso7513
    @andistheinforitbutso7513 2 ปีที่แล้ว +3

    Awesome work 👏
    Lesson: we should be careful while arranging the srruct variables for better memory utilisation

    • @pnuema1.618
      @pnuema1.618 2 ปีที่แล้ว +1

      Padding also becomes a portability issue when code is written on a system with different architecture then the architecture it is being run on. If you already knew this then my apologies. I just feel like understanding padding has more to do with writing portable code.

  • @aashishmalhotra
    @aashishmalhotra ปีที่แล้ว +12

    I'm wondering why bloody YT algo doesn't recommend your channel in first year.

  • @beeyou6220
    @beeyou6220 2 ปีที่แล้ว +1

    I m having 8 yrs of experience. Noone Clarified the doubt lk u..thank you..expecting more content from you

  • @19n05k83
    @19n05k83 2 ปีที่แล้ว +1

    For example, with ARM architecture (it's 8bit addressable and 32 bit accessible) engineers and programmers are encouraged to use 32 bit values all the time, so software will run faster and more secure. If memory space is critical then optimization is needed.

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz 2 ปีที่แล้ว +4

    Why is the compiler not allowed to rearrange member variables? If there's a consistent rule for this, then even external libraries will not face any ambiguity. That way, int variable could've been placed in front, and no wastage will be required and all 3 can be accessed in one CPU cycle.

    • @excite236
      @excite236 ปีที่แล้ว

      Have you tried enabling optimizations with your compiler?

  • @mikestock1848
    @mikestock1848 หลายเดือนก่อน

    Awesome explanation, thanks for sharing

  • @TechnoVisionTV
    @TechnoVisionTV ปีที่แล้ว +1

    Such an amazing explanation! You made it so simple to understand, thank you :)

  • @mcbotface
    @mcbotface 5 ปีที่แล้ว +3

    Thank you sir. Keep posting more on stacks, queues, linked lists

  • @adeshgaikwad2192
    @adeshgaikwad2192 4 ปีที่แล้ว +2

    Thank you sir for clearing the concept in such an easy way...

  • @absorpheus
    @absorpheus 25 วันที่ผ่านมา

    Very well explained. Thanks!

  • @RahulRazj
    @RahulRazj 3 ปีที่แล้ว +2

    when declaring a char and then a double variable it should give 4+8=12 as output. But it shows 16. Can someone explain why?

    • @kalppariya381
      @kalppariya381 3 ปีที่แล้ว

      thats called padding.

    • @_sk_beatz_6051
      @_sk_beatz_6051 ปีที่แล้ว +1

      Your processor is 64 bit architecture may be

  • @Aabara_ka_dabara
    @Aabara_ka_dabara ปีที่แล้ว

    something unique and new things I have learned💕💕

  • @ahmedsiraj2758
    @ahmedsiraj2758 4 ปีที่แล้ว +12

    when declaring 3 char, it should give 4 as output, why giving 3

    • @jayaprakashthokala640
      @jayaprakashthokala640 4 ปีที่แล้ว +6

      Word size is nothing but highest size of a data type used...

    • @anandg4018
      @anandg4018 4 ปีที่แล้ว +1

      because 3 char are taking 3 bytes of size within the word, and word is actually 4 bytes long thats why it is showing 4 bytes as output

    • @ImtiazAhmedRatul
      @ImtiazAhmedRatul 4 ปีที่แล้ว +14

      The reason behind is is that all 3 chars can be accessed using 1 CPU cycle. In other words, for CPU to access char 1 will only take 1 cycle, char 2 will take 1 cycle and char 3 will take 1 cycle. Padding is introduced to save CPU cycles. Since there is no way to get fewer cycles than 1, the compiler doesn't introduce any padding.
      I understand your confusion. If the CPU is accessing 1 word (4 bytes) at a time, the struct should be char (1 byte + 3 padding) x 3 = 12 bytes in size.
      What word size the machine will use depends on the type of data it is trying to access. So if it's trying to access a char, it is using word size == 1byte. For int, it will use 4 bytes. When it sees a structure that has both, it will use the highest size (int == 4 in the video's case). In your case, it's using 1 byte for word size.

    • @0mer870
      @0mer870 3 ปีที่แล้ว +1

      @@ImtiazAhmedRatul It seems you misunderstood his logic. He didn't say it should allocate 12 bytes but 4 which is the word size for a 32 bit processor. And since the instructor said that the processor reads one word at a time and not 1 byte, you'd except it to be always so even if it was just a char it should get allocated 4 bytes as in "1 word at a time". Therefore I think that padding applies only when the size of the variable exceeds the size of a word.

    • @lawalayodeji4044
      @lawalayodeji4044 3 ปีที่แล้ว

      The null character \0 is 1 byte. It is always at the end of all char or int.

  • @sohitgore2924
    @sohitgore2924 2 ปีที่แล้ว

    To handle the case where the memory words are in different memory pages the processor must either verify that both pages are present before executing the instruction or be able to handle a TLB miss or a page fault on any memory access during the instruction execution.
    Source - Wikipedia

  • @ronnieahabwomugisha5283
    @ronnieahabwomugisha5283 4 ปีที่แล้ว +3

    its has been clear. thanks Sir

  • @vipulchawla5466
    @vipulchawla5466 4 ปีที่แล้ว +2

    If we use one int,one double and again one int variable then the size of variable should be 16 but it is 24 why???

    • @kumargaurav8234
      @kumargaurav8234 4 ปีที่แล้ว +1

      Because double is 8 bytes long, so the word size would be 8 bytes long. Hence 8*3 = 24 bytes

    • @HarshitSharma-kq5yi
      @HarshitSharma-kq5yi 4 ปีที่แล้ว +1

      @@kumargaurav8234 your reply cleared every doubt i had thanks

  • @priyaj9908
    @priyaj9908 4 ปีที่แล้ว +1

    Very useful video...Thank you sir.please try to put more videos in advanced c topics..

  • @nishantraj5020
    @nishantraj5020 4 ปีที่แล้ว +1

    you make every concept so easyyyyyyyyy.

  • @khoahuynh6884
    @khoahuynh6884 ปีที่แล้ว +1

    It is always the Indian sensei that makes me understand.

  • @dhruvsakariya3129
    @dhruvsakariya3129 ปีที่แล้ว +1

    perfect example of 32byte and 64 byte difference thank you very much sir

  • @umairalvi7382
    @umairalvi7382 4 ปีที่แล้ว +3

    Considering the processor is of 32-bit
    Void main ()
    { char a;
    }
    then in the memory for the variable a, only 1 byte will be allocated, then how the processor will request this memory using word...without padding?

    • @bharathiapps8230
      @bharathiapps8230 3 ปีที่แล้ว

      The processor would read 4 bytes(from the address of variable char a, it will use only the first byte ), a 32 bit processor always reads 4 bytes(memory is 4 bytes aligned).

  • @pnuema1.618
    @pnuema1.618 2 ปีที่แล้ว

    You are one of my favorite sources for c programming concepts. I know that if you have a video on the subject then I will be able to understand that concept. Thank you!

  • @paschikshehu7988
    @paschikshehu7988 3 ปีที่แล้ว +1

    1:15 not necessarily wrong, just that 1-packing is not efficient

  • @rakurame6990
    @rakurame6990 2 ปีที่แล้ว

    Good explanation on structure padding

  • @jishankhan6686
    @jishankhan6686 2 ปีที่แล้ว

    You are really great. Plzz. Give a practical example nd make us clear how a software execute. Suppose i have one software on my mobile then how this software will be executed. Plz give us practical example from first to last.

  • @ashwanirajput9141
    @ashwanirajput9141 2 ปีที่แล้ว

    If we declear one more "char" var outside of struct. Then memory allocation for that var is where.
    1. Just after var C " 10th place ".
    2. 13th place.

  • @user-yc2ry2uz4h
    @user-yc2ry2uz4h 4 ปีที่แล้ว +2

    really easy to understand for me. thanks a lot!!

  • @QuratulainAzhar
    @QuratulainAzhar 4 หลายเดือนก่อน +1

    Amazing 🤩

  • @psicopatico00
    @psicopatico00 8 หลายเดือนก่อน

    Well explained, thank you so much

  • @santanacasas1
    @santanacasas1 2 ปีที่แล้ว

    Excellent explanation, thanks for your dedicated time in creating these videos. Regards from Mexico.

  • @pranavkshirsagar146
    @pranavkshirsagar146 4 ปีที่แล้ว +1

    Barney Stinson was their... I got so happy after seeing him😆😆

  • @multivalfran
    @multivalfran 2 ปีที่แล้ว

    Excellent video my friend

  • @revan7600
    @revan7600 2 ปีที่แล้ว

    Very nice explanation thank you

  • @RozgarBharatIT
    @RozgarBharatIT 8 หลายเดือนก่อน

    Great Explanation!!

  • @avronilbanerjee5302
    @avronilbanerjee5302 2 ปีที่แล้ว

    excellent explanation

  • @vishnu5466
    @vishnu5466 3 ปีที่แล้ว +1

    Very Interesting!...

  • @Rahul_Sharma123
    @Rahul_Sharma123 ปีที่แล้ว

    Please answer,
    #include
    Struct abc{
    Int a;
    Char b;
    Int c;
    }var;
    Void main() {
    Printf("%d bytes", sizeof(var));
    }
    Output : 12 bytes
    Whereas i am expecting the output to be "16 bytes" [4bytes(for a)+1bytes(for b)+3bytes(padding)+4bytes(for c)+4bytes(padding)] coz my system is of 64bit and word size has to be of 8bytes.

  • @yashikagoyal366
    @yashikagoyal366 2 หลายเดือนก่อน

    It means if processor is of 64 bit
    Then size of the discussed structure will be 8 irrespective of the order.

  • @05_abhisheksharma13
    @05_abhisheksharma13 3 ปีที่แล้ว +1

    Amazing initiative by neso academy

  • @vijaykounsalye
    @vijaykounsalye 5 ปีที่แล้ว +1

    just awesome bro you have explained very well

  • @rajmishra6769
    @rajmishra6769 4 ปีที่แล้ว +2

    Thanks for the wonderful explanation.

  • @leepakshiyadav1643
    @leepakshiyadav1643 2 ปีที่แล้ว +1

    crystal clear explanation, thanks a lot :)

  • @botelhocpp
    @botelhocpp 2 ปีที่แล้ว

    I was thinking... Sure, the processor can only access a word at a time. But wouldn't it make sense to, like, displace the address of the struct a bit to access the integer? Like, in the first example... Rather than padd the first word, wouldn't be better to just add 2 to the address and access the integer right away?? Or this operation would cost too much clock-wise everytime?

  • @y_red_gold_a4280
    @y_red_gold_a4280 2 ปีที่แล้ว

    Best explanation of the concept. Thank you so much.

  • @yemreekinci
    @yemreekinci 4 หลายเดือนก่อน

    Adamsın kral çok güzel anlattın.

  • @amandarash135
    @amandarash135 5 ปีที่แล้ว +1

    Best explanation

  • @RAVITEJAP-ss9rl
    @RAVITEJAP-ss9rl 3 ปีที่แล้ว +1

    So if we have a variable of char type then it's size would be 4 bytes because that processor can access 4 bytes at a time, but it's not so why? Does this padding concept applicable for only stuctures? Can somebody explain....

  • @premithagoni8634
    @premithagoni8634 ปีที่แล้ว

    Awesome Explanation sir,Thank you

  • @mohamedmaged2977
    @mohamedmaged2977 4 ปีที่แล้ว +1

    If the size of the variable is double , this means it will take 8 byte how padding will threat with it . it can not access it at the same time because word is equal to 4 bytes !

    • @sudeepgour9758
      @sudeepgour9758 4 ปีที่แล้ว

      It will take double as a word size and make room for 8 Byte every time.
      if we take char ,int ,int *,than int , guesses the size of struct
      now char,int, int, int *,and int now guess again .

  • @VishalMishra-jx8gs
    @VishalMishra-jx8gs 5 ปีที่แล้ว +2

    thanks sir, but why you are not prepared for gate computer science

  • @adithyay328
    @adithyay328 4 ปีที่แล้ว +1

    Good, clear explanation!

  • @Cat_Sterling
    @Cat_Sterling 3 ปีที่แล้ว +1

    That was just great! I was wondering about that for so long! But what is a "word" in computer architecture?

    • @zidanegimiga8180
      @zidanegimiga8180 3 ปีที่แล้ว +2

      A word must be 4 or 8 bytes long, depending on the computer architecture I guess.

  • @gurlinkanaveenkumar7209
    @gurlinkanaveenkumar7209 2 ปีที่แล้ว +1

    Tqu sir.

  • @shauryakumar2229
    @shauryakumar2229 ปีที่แล้ว

    Very helpful thanku

  • @chintanchatterjee9477
    @chintanchatterjee9477 8 หลายเดือนก่อน +1

    Respected Sir,
    I have a doubt in one program of this topic.
    #include
    struct abc{
    float a;
    double b;
    } var;
    int main()
    {
    printf(“%d”, sizeof(var));
    return 0;
    }
    In 32-bit architecture, the output of the above code should be 12 if float size is 4 B and double size is 8 B.
    But in online compilers like onlinegdb, programiz, etc., the output of this particular code is 16.
    What is the reason? I am not getting it.
    Thanks and Regards.
    Chintan

    • @sushaanthsureshkumar
      @sushaanthsureshkumar 7 หลายเดือนก่อน

      Ah you kind of got it wrong, It should be 16 bytes.
      In Sir's example int was the largest size taking value i.e. 4 bytes, (Alignment value)
      so have 2 fields char and char then having int will have a total size of 8 bytes.
      In your case, the largest is double which is 8 bytes. (Alignment value)
      You have float at first and then double meaning 8*2 = 16 bytes are reserved for your struct and its not 12 bytes.
      This C way of writing is also used Go. I'm not sure with C++ probably it too has this behavior. Happy Coding @chintanchatterjee9477

  • @haripriyaraja1890
    @haripriyaraja1890 2 ปีที่แล้ว

    Nice clarification 😍

  • @mudassar2012
    @mudassar2012 2 ปีที่แล้ว

    very helpful video.....so good explanation

  • @SaidasSR
    @SaidasSR 4 ปีที่แล้ว +2

    struct mystrct
    {
    char a;
    double b;
    char a;
    }; what will be sizeof struct in 32 bit system

    • @sameeksharl7655
      @sameeksharl7655 4 ปีที่แล้ว +2

      it shows 10 in m y compiler i donno whats wrong ...it should be 16 though

    • @nihalpandey5675
      @nihalpandey5675 4 ปีที่แล้ว

      @@sameeksharl7655 yaa same here

    • @xrafter
      @xrafter 4 ปีที่แล้ว

      @@sameeksharl7655
      Optimization you need optimization is off let it on

  • @rosako
    @rosako 4 ปีที่แล้ว +1

    Instead of padding and wasting memory, why not just change the order to : struct abc{int b; char a; char c}?

  • @thienphucnguyenpham6944
    @thienphucnguyenpham6944 3 ปีที่แล้ว +1

    i think it would be better with CC ^^

  • @okavango5937
    @okavango5937 3 ปีที่แล้ว +1

    If the CPU accesses the addresses only in "word" sizes, then why is 'sizeof(char)' 1? It sould also be 4 (in 32bit) right?

  • @KrupaPrarthana
    @KrupaPrarthana 6 หลายเดือนก่อน

    Wonderful ❤

  • @anjani_tiwari
    @anjani_tiwari 11 หลายเดือนก่อน

    Dhanyavaad🙏

  • @MirajAli-oj3cj
    @MirajAli-oj3cj 9 หลายเดือนก่อน

    thanks

  • @FraPiz
    @FraPiz 2 ปีที่แล้ว

    wow, my mind is blown

  • @shreyasingh222
    @shreyasingh222 3 ปีที่แล้ว

    Thank you so much for detailed explanation.....

  • @mdkamrankhan4032
    @mdkamrankhan4032 5 ปีที่แล้ว +1

    Great explanation Sir..

  • @prakhartyagi6444
    @prakhartyagi6444 4 ปีที่แล้ว +1

    Sir in 2nd case also 2 cycles are used for 8 bytes.then what is the use of padding.

    • @xrafter
      @xrafter 4 ปีที่แล้ว

      Second case is 3 cycles and not 2.
      using padding is a compiler thing.
      You will care about aligning you variables if you program in assembly but in c the compiler will do that for you.
      Aligning is great for higher performance and speed but take more memory

  • @nikhilwagh1728
    @nikhilwagh1728 ปีที่แล้ว

    Great video

  • @mowafkmha4505
    @mowafkmha4505 2 ปีที่แล้ว

    wow bro just thank you

  • @yahyaaouledamer1466
    @yahyaaouledamer1466 2 ปีที่แล้ว

    amazing thanks so much

  • @somuseth
    @somuseth 4 ปีที่แล้ว +2

    4:40
    Sir, does it mean that total of 3 cyles are required to access the 3 variables???

  • @tanmayghosh8311
    @tanmayghosh8311 2 ปีที่แล้ว

    never thought we can optimize our code using this methods

  • @47-6F-64
    @47-6F-64 4 ปีที่แล้ว +2

    Amazing

  • @ሰውመሆን
    @ሰውመሆን ปีที่แล้ว

    thank you so much

  • @winxkin6056
    @winxkin6056 2 ปีที่แล้ว

    it is very helpful

  • @gandhijainamgunvantkumar6783
    @gandhijainamgunvantkumar6783 2 ปีที่แล้ว

    Amazing explanation. Thank you 😃

  • @user-bu8mg7uq3s
    @user-bu8mg7uq3s 3 ปีที่แล้ว

    thank you

  • @user-si9qz3kx9h
    @user-si9qz3kx9h ปีที่แล้ว

    excellent lecture i have ever seen

  • @yashikagoyal366
    @yashikagoyal366 2 หลายเดือนก่อน

    Also size of struct is not fixed. It varies with the processor.

  • @andyprem
    @andyprem 3 ปีที่แล้ว

    excellent excellent explanation ..I was having an issue wstructure memebers being read incorrectly and I was suspecting this padding issue. and I was wondering how to explain this to the team .may be I should link to this vedio .. lol ;)

  • @krishnakumar-kh5vo
    @krishnakumar-kh5vo 2 ปีที่แล้ว

    well explained

  • @Jose_T131
    @Jose_T131 2 ปีที่แล้ว

    Very well explained, thank you sir

  • @nileshpokale862
    @nileshpokale862 3 ปีที่แล้ว

    Excellent Explanation. All cleared

  • @assassinhitman
    @assassinhitman ปีที่แล้ว

    Amazing explanation

  • @karthicknandhakumar264
    @karthicknandhakumar264 4 ปีที่แล้ว +2

    Sir, I understand the padding concept..but in 1st case , overall 2 cpu cycles are used with padding and without padding...then what is the use of having padding?

    • @abhishekrawat5096
      @abhishekrawat5096 4 ปีที่แล้ว +2

      Please watch video properly , the Instructor have already answered this , in 1st example , suppose you want to access (integer c) , then you will get it only in 1 cycle using "padding" and without padding concept , you have to get the same (integer c) in 2 cycles

    • @arshadshaik6554
      @arshadshaik6554 4 ปีที่แล้ว

      @@abhishekrawat5096 yes, correct but in 1 st cycle, along with 2 bytes of int c, already two bytes (char a and char b) are accessed and in second cycle the remaining two bytes of int c are accessed.
      In this case, Anyway only two cycles are used with padding and also without padding, then what's the use of padding here??

    • @debabratamohapatra6638
      @debabratamohapatra6638 3 ปีที่แล้ว +3

      @@arshadshaik6554 bro, sir then explained it, suppose u want to access only int c and not char a and b
      to do so, unnecessary 2 cycles will be wasted which can be done in one cycle using structure paddling.
      Hp u understood..

    • @rishabhjha7565
      @rishabhjha7565 2 ปีที่แล้ว

      @@debabratamohapatra6638 thanks for clearing my doubt