Structure Padding | C Programming Tutorial

Sdílet
Vložit
  • čas přidán 4. 01. 2023
  • An explanation of what structure padding is, how it works, why it optimizes performance at the cost of memory, including how to create our structs to optimize memory and examples of techniques to turn off struct padding. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Komentáře • 58

  • @Gigaclank
    @Gigaclank Před rokem +18

    This becomes especially important when working with embedded systems. This can allow for easier data transfer between external ic’s into structures by replicating the data format of the external ic registers.

  • @mattmurphy7030
    @mattmurphy7030 Před 7 dny

    Currently dealing with a network interop nightmare because the vendor developers didn’t understand this and used memcpy to deserialize. Thanks for the refresher

  • @shaunmorgan2202
    @shaunmorgan2202 Před 3 měsíci +2

    Some processors are only able to read word aligned addresses, so the padding is there so the CPU doesn't fault. That being said, great explanation.

    • @andrewdunbar828
      @andrewdunbar828 Před 3 měsíci

      Others may be able to read unaligned addresses but at a performance penalty.

  • @MarcoAurelio-sv2tk
    @MarcoAurelio-sv2tk Před 3 dny

    Thank you for this information. Searching this topic influenced me to learn about assembly

  • @BattlewarPenguin
    @BattlewarPenguin Před měsícem

    Awesome explanation!

  • @rabbitoose1381
    @rabbitoose1381 Před rokem +3

    Very clear explanation!❤

  • @hanaksi
    @hanaksi Před rokem +2

    Thanks for guide! Really helpful !

  • @shadow3961
    @shadow3961 Před rokem +4

    Learnt something new. Thank you🙏🙏

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      You're welcome Tuhin, that's great you learned something new! :-)

  • @nameless5724
    @nameless5724 Před rokem

    Wowwww It's Amazing. Thank you for such a beautiful tutorial.

  • @undeadpresident
    @undeadpresident Před rokem

    I didn't know about this before. Thanks!

  • @thesmartone6601
    @thesmartone6601 Před rokem +4

    MindBlowing Lecture Sir Respect

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      Thank you for the positive feedback! :-)

    • @thesmartone6601
      @thesmartone6601 Před rokem

      @@PortfolioCourses Love your Courses Sir
      Such Topics are unknown to many but these Intricacies make one appreciate the detail in designing of something we use so often in our life

  • @hasdrubal9799
    @hasdrubal9799 Před rokem +1

    Tanks, sir.
    Another good explanation, in deep on back work the machine.
    I need to learn more about #pragma.
    Good day sir.

  • @edenng8431
    @edenng8431 Před rokem +1

    Very nice! Thank you!

  • @blazerchicken1619
    @blazerchicken1619 Před 7 měsíci

    Good explanation

  • @dukereg
    @dukereg Před rokem

    So if the word size is 8 bytes (which I assume is true of the common "64 bit" CPUs), how would that affect your example with {char x; int y; char z}?From what you said I'm not sure if all fields would be padded to 8 bytes due to word size (24 bytes total), or padded to 4 bytes from the int being the largest field (12 bytes total), or all packed into one word since 6 bytes can fit in one word (8 bytes total)?

  • @Mnogojazyk
    @Mnogojazyk Před rokem +5

    I knew about struct padding from the first general purpose language I learned, Pascal. Pascal does something analogous called record padding.
    I do have a question: when a struct is written to file, are the data elements analogously padded in the file? I can only guess indeed yes because of the block layout of memory on disc, SSD, tape (in the olden days of yore), etc.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +2

      Great question! :-) If we write the data to a binary file in the same way we do in this example... github.com/portfoliocourses/c-example-code/blob/main/struct_array_binary_file.c ...then yes, the data will be stored with the same padding in the file as it has in memory.

  • @themannyzaur
    @themannyzaur Před rokem

    Most of the other videos I watched just left it at #pragma pack(1) but you actually gave solutions that are useful aside the pragma

  • @erbenton07
    @erbenton07 Před 4 měsíci

    How about a video on how to keep a relatively short global array in cache?

  • @person0425
    @person0425 Před rokem +2

    Thanks, I was wondering why a struct I made was 8 bytes instead of 6

  • @abdelhakmezenner2855
    @abdelhakmezenner2855 Před rokem +1

    that was interesting !

  • @minandychoi8597
    @minandychoi8597 Před rokem

    3:34 why doesn’t it just read the bytes at addresses 1-4? that’s one read operation since the CPU reads 4 bytes at a time

  • @sanfreful
    @sanfreful Před 3 měsíci

    I accidentally discovered this after working on na personal project on raspberry pico. What I was trying to implement a CanOpen communication protocol on the pico

  • @kennedymunyao6136
    @kennedymunyao6136 Před rokem +1

    Great, video as always,, what IDE are you using?

    • @frenkie_music
      @frenkie_music Před rokem

      Hes using xcode on macOS

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      Dan is correct, I'm using Xcode on a Mac. :-)

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      Thank you for answering Kennedy's question Dan! :-)

    • @frenkie_music
      @frenkie_music Před rokem

      @@PortfolioCourses No worries? If I may ask, could you do a little video about the basics of linked lists using structs in C?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem +1

      @@frenkie_music I actually made a Udemy course on that topic, I've created a discount code for it right now which is the lowest price I can sell it for in case you are interested: www.udemy.com/course/linked-lists-with-c/?couponCode=SPECIALDEAL. 🙂

  • @fifaham
    @fifaham Před rokem +2

    With TSMC of Taiwan and their amazing 5 nm silicon technology with super high speed and super high memory density, programmers may be pardoned to set the CHAR then INT then CHAR in STRUCT, however I don't recommend that they should do that ! They should follow Kevin instructions.
    Thank you Kevin.

  • @Rahul_Sharma123
    @Rahul_Sharma123 Před rokem

    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.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      It's ultimately up to the compiler, so it's hard to say for sure to be honest. Often the structure will be aligned to the size of the largest member in the struct, so because the strict has int the max size of a member is 4 bytes and the struct is 4-bye aligned as a result. :-)

    • @Rahul_Sharma123
      @Rahul_Sharma123 Před rokem

      @@PortfolioCourses thanks, it was really helpful.

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      You're welcome! :-)

    • @monarchaether9079
      @monarchaether9079 Před 11 měsíci

      thanks for this response, many Online teachers ignore this question, in my case the the assumption you made was correct, coz i included a long datatype and the size became 24 meaning the compiler made the byte-alignment according to the largest member of the struct, if not I should have gotten 20 instead for a 4-byte alignment, you've made me a fan and you have my follow@@PortfolioCourses

  • @ddstar
    @ddstar Před 5 měsíci

    "ch-are" not "car".

    • @PortfolioCourses
      @PortfolioCourses  Před 5 měsíci +2

      There are several "phonetically valid" pronunciations of "char" in American Englishand "car" is one of them, this link goes through the pronunciations of "char" that people use: english.stackexchange.com/a/60175

    • @mattmurphy7030
      @mattmurphy7030 Před 7 dny

      Confidently incorrect lol

  • @HARIHARESHWARDP
    @HARIHARESHWARDP Před 5 měsíci +1

    man you deserve more followers 🫡