Тёмный
No video :(

Structure Padding | C Programming Tutorial 

Portfolio Courses
Подписаться 213 тыс.
Просмотров 11 тыс.
50% 1

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/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

Опубликовано:

 

4 янв 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 58   
@Gigaclank
@Gigaclank Год назад
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.
@PortfolioCourses
@PortfolioCourses Год назад
Cool, thank you for sharing this Aaron! :-)
@MarcoAurelio-sv2tk
@MarcoAurelio-sv2tk Месяц назад
Thank you for this information. Searching this topic influenced me to learn about assembly
@mattmurphy7030
@mattmurphy7030 Месяц назад
Currently dealing with a network interop nightmare because the vendor developers didn’t understand this and used memcpy to deserialize. Thanks for the refresher
@PortfolioCourses
@PortfolioCourses Месяц назад
That sounds painful! And you’re welcome. :-)
@shaunmorgan2202
@shaunmorgan2202 4 месяца назад
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 4 месяца назад
Others may be able to read unaligned addresses but at a performance penalty.
@thesmartone6601
@thesmartone6601 Год назад
MindBlowing Lecture Sir Respect
@PortfolioCourses
@PortfolioCourses Год назад
Thank you for the positive feedback! :-)
@thesmartone6601
@thesmartone6601 Год назад
@@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
@shadow3961
@shadow3961 Год назад
Learnt something new. Thank you🙏🙏
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome Tuhin, that's great you learned something new! :-)
@rabbitoose1381
@rabbitoose1381 Год назад
Very clear explanation!❤
@PortfolioCourses
@PortfolioCourses Год назад
I'm happy that you found the explanation clear! :-)
@hanaksi
@hanaksi Год назад
Thanks for guide! Really helpful !
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome, I'm glad you found it helpful! :-)
@Mnogojazyk
@Mnogojazyk Год назад
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 Год назад
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.
@BattlewarPenguin
@BattlewarPenguin 2 месяца назад
Awesome explanation!
@hasdrubal9799
@hasdrubal9799 Год назад
Tanks, sir. Another good explanation, in deep on back work the machine. I need to learn more about #pragma. Good day sir.
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome, I'm glad you enjoyed the explanation! :-)
@nameless5724
@nameless5724 Год назад
Wowwww It's Amazing. Thank you for such a beautiful tutorial.
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome, I'm glad you enjoyed it! :-)
@undeadpresident
@undeadpresident Год назад
I didn't know about this before. Thanks!
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome! :-)
@themannyzaur
@themannyzaur Год назад
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
@PortfolioCourses
@PortfolioCourses Год назад
I'm glad you enjoyed it! :-)
@Aabara_ka_dabara
@Aabara_ka_dabara Год назад
@@PortfolioCourses can you please tell more about this attribute
@person0425
@person0425 Год назад
Thanks, I was wondering why a struct I made was 8 bytes instead of 6
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome! :-)
@erbenton07
@erbenton07 5 месяцев назад
How about a video on how to keep a relatively short global array in cache?
@edenng8431
@edenng8431 Год назад
Very nice! Thank you!
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome Eden! :-)
@sanfreful
@sanfreful 3 месяца назад
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
@blazerchicken1619
@blazerchicken1619 8 месяцев назад
Good explanation
@dukereg
@dukereg Год назад
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)?
@minandychoi8597
@minandychoi8597 Год назад
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
@PortfolioCourses
@PortfolioCourses Год назад
A CPU could read 8 bytes at a time, e.g. a 64-bit CPU.
@abdelhakmezenner2855
@abdelhakmezenner2855 Год назад
that was interesting !
@PortfolioCourses
@PortfolioCourses Год назад
I'm glad you found it interesting Abdelhak! :-)
@fifaham
@fifaham Год назад
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.
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome Firas! :-)
@kennedymunyao6136
@kennedymunyao6136 Год назад
Great, video as always,, what IDE are you using?
@frenkie_music
@frenkie_music Год назад
Hes using xcode on macOS
@PortfolioCourses
@PortfolioCourses Год назад
Dan is correct, I'm using Xcode on a Mac. :-)
@PortfolioCourses
@PortfolioCourses Год назад
Thank you for answering Kennedy's question Dan! :-)
@frenkie_music
@frenkie_music Год назад
@@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 Год назад
@@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. 🙂
@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.
@PortfolioCourses
@PortfolioCourses Год назад
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 Год назад
@@PortfolioCourses thanks, it was really helpful.
@PortfolioCourses
@PortfolioCourses Год назад
You're welcome! :-)
@monarchofaether
@monarchofaether Год назад
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 6 месяцев назад
"ch-are" not "car".
@PortfolioCourses
@PortfolioCourses 6 месяцев назад
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 Месяц назад
Confidently incorrect lol
@HARIHARESHWARDP
@HARIHARESHWARDP 6 месяцев назад
man you deserve more followers 🫡
@PortfolioCourses
@PortfolioCourses 6 месяцев назад
Thanks for the support! :-D
Далее
7 Days Stranded In A Cave
17:59
Просмотров 56 млн
Premature Optimization
12:39
Просмотров 787 тыс.
NULL Pointer | C Programming Tutorial
18:59
Просмотров 10 тыс.
why do void* pointers even exist?
8:17
Просмотров 352 тыс.
Cursed C++ Casts
17:41
Просмотров 72 тыс.
Union Data Types | C Programming Tutorial
11:42
Просмотров 14 тыс.