Structure Padding | C Programming Tutorial
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!
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.
Cool, thank you for sharing this Aaron! :-)
Currently dealing with a network interop nightmare because the vendor developers didn’t understand this and used memcpy to deserialize. Thanks for the refresher
That sounds painful! And you’re welcome. :-)
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.
Others may be able to read unaligned addresses but at a performance penalty.
Thank you for this information. Searching this topic influenced me to learn about assembly
Awesome explanation!
Very clear explanation!❤
I'm happy that you found the explanation clear! :-)
Thanks for guide! Really helpful !
You're welcome, I'm glad you found it helpful! :-)
Learnt something new. Thank you🙏🙏
You're welcome Tuhin, that's great you learned something new! :-)
Wowwww It's Amazing. Thank you for such a beautiful tutorial.
You're welcome, I'm glad you enjoyed it! :-)
I didn't know about this before. Thanks!
You're welcome! :-)
MindBlowing Lecture Sir Respect
Thank you for the positive feedback! :-)
@@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
Tanks, sir.
Another good explanation, in deep on back work the machine.
I need to learn more about #pragma.
Good day sir.
You're welcome, I'm glad you enjoyed the explanation! :-)
Very nice! Thank you!
You're welcome Eden! :-)
Good explanation
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)?
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.
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.
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
I'm glad you enjoyed it! :-)
@@PortfolioCourses can you please tell more about this attribute
How about a video on how to keep a relatively short global array in cache?
Thanks, I was wondering why a struct I made was 8 bytes instead of 6
You're welcome! :-)
that was interesting !
I'm glad you found it interesting Abdelhak! :-)
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
A CPU could read 8 bytes at a time, e.g. a 64-bit CPU.
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
Great, video as always,, what IDE are you using?
Hes using xcode on macOS
Dan is correct, I'm using Xcode on a Mac. :-)
Thank you for answering Kennedy's question Dan! :-)
@@PortfolioCourses No worries? If I may ask, could you do a little video about the basics of linked lists using structs in C?
@@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. 🙂
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.
You're welcome Firas! :-)
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.
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. :-)
@@PortfolioCourses thanks, it was really helpful.
You're welcome! :-)
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
"ch-are" not "car".
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
Confidently incorrect lol
man you deserve more followers 🫡
Thanks for the support! :-D