Structure Padding in C

  Рет қаралды 257,399

Neso Academy

Neso Academy

Күн бұрын

Пікірлер: 263
@John-sl5zz
@John-sl5zz 2 жыл бұрын
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 2 жыл бұрын
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!
@عليكيلاني-ي8د
@عليكيلاني-ي8د Жыл бұрын
I couldn't agree with you more , It's awesome how he explained it.
@WistrelChianti
@WistrelChianti 4 жыл бұрын
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).
@manazirahsan1351
@manazirahsan1351 2 жыл бұрын
Tnx
@ahamd19
@ahamd19 4 жыл бұрын
speachless!! you are soooo goood in explainging. thanks a lot
@koperaljali3253
@koperaljali3253 4 жыл бұрын
Yes agree.
@dono42
@dono42 Жыл бұрын
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.
@andresvallelisboa5511
@andresvallelisboa5511 4 жыл бұрын
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.
@manazirahsan1351
@manazirahsan1351 2 жыл бұрын
Ok
@michaelhernandez2075
@michaelhernandez2075 Жыл бұрын
trying to read how this works is way harder than having pictures and someone explaining. Thank you.
@anshhmehta
@anshhmehta 2 жыл бұрын
Perfect video to understand the importance of padding and its uses.
@beeyou6220
@beeyou6220 2 жыл бұрын
I m having 8 yrs of experience. Noone Clarified the doubt lk u..thank you..expecting more content from you
@rakesh231rakesh
@rakesh231rakesh 2 жыл бұрын
😱
@sunilkumarreddy6537
@sunilkumarreddy6537 5 жыл бұрын
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.
@manuelconte2127
@manuelconte2127 2 жыл бұрын
It is a good habit to put first the biggest type in ascending order to help compilator !
@khoahuynh6884
@khoahuynh6884 2 жыл бұрын
It is always the Indian sensei that makes me understand.
@sujitsatpute3741
@sujitsatpute3741 3 жыл бұрын
You are explaining 💯 times better than my college professor......
@SJ_46
@SJ_46 2 жыл бұрын
1000 bro
@manoharbompella418
@manoharbompella418 4 жыл бұрын
Explained very detailed, Cleared every doubt. Thank you
@andistheinforitbutso7513
@andistheinforitbutso7513 3 жыл бұрын
Awesome work 👏 Lesson: we should be careful while arranging the srruct variables for better memory utilisation
@pnuema1.618
@pnuema1.618 2 жыл бұрын
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.
@TechnoVisionTV
@TechnoVisionTV 2 жыл бұрын
Such an amazing explanation! You made it so simple to understand, thank you :)
@ArjanvanVught
@ArjanvanVught 5 жыл бұрын
Missing to explain __attribute__((packed)) with unaligned memory access which is possible (with ARM).
@ivanleon6164
@ivanleon6164 3 жыл бұрын
#pragma pack(1)
@adeshgaikwad2192
@adeshgaikwad2192 5 жыл бұрын
Thank you sir for clearing the concept in such an easy way...
@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!
@manazirahsan1351
@manazirahsan1351 2 жыл бұрын
Ok
@paschikshehu7988
@paschikshehu7988 3 жыл бұрын
1:15 not necessarily wrong, just that 1-packing is not efficient
@nishantraj5020
@nishantraj5020 4 жыл бұрын
you make every concept so easyyyyyyyyy.
@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
@kitanowitsch
@kitanowitsch 2 жыл бұрын
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.
@rakesh231rakesh
@rakesh231rakesh 2 жыл бұрын
It is all about time and space.
@gamerplayer94830
@gamerplayer94830 5 ай бұрын
What does addressable and accessable mean? and what is the difference could you please tell? couldnt find or understand from anywhere
@kitanowitsch
@kitanowitsch 5 ай бұрын
@@gamerplayer94830 When you write and when you read from the memory, respectively. Data is written by 8 bits into the memory and read by 32 bits. So if your 8 bit data is in 4th memory part of 32bit location, it will take 4 tries to access that particular byte. That's why it is important when you declare structs and any other type of data. More speed and more secure.
@gamerplayer94830
@gamerplayer94830 5 ай бұрын
@@kitanowitsch thank you very much, i understood! have a nice day
@santanacasas1
@santanacasas1 3 жыл бұрын
Excellent explanation, thanks for your dedicated time in creating these videos. Regards from Mexico.
@mcbotface
@mcbotface 5 жыл бұрын
Thank you sir. Keep posting more on stacks, queues, linked lists
@QuratulainAzhar
@QuratulainAzhar 9 ай бұрын
Amazing 🤩
@05_abhisheksharma13
@05_abhisheksharma13 4 жыл бұрын
Amazing initiative by neso academy
@ronnieahabwomugisha5283
@ronnieahabwomugisha5283 5 жыл бұрын
its has been clear. thanks Sir
@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.
@dhruvsakariya3129
@dhruvsakariya3129 2 жыл бұрын
perfect example of 32byte and 64 byte difference thank you very much sir
@mikestock1848
@mikestock1848 6 ай бұрын
Awesome explanation, thanks for sharing
@priyaj9908
@priyaj9908 4 жыл бұрын
Very useful video...Thank you sir.please try to put more videos in advanced c topics..
@rajmishra6769
@rajmishra6769 5 жыл бұрын
Thanks for the wonderful explanation.
@comradepeter87
@comradepeter87 3 жыл бұрын
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 2 жыл бұрын
Have you tried enabling optimizations with your compiler?
@あまね-y6t
@あまね-y6t 4 жыл бұрын
really easy to understand for me. thanks a lot!!
@pranavkshirsagar146
@pranavkshirsagar146 4 жыл бұрын
Barney Stinson was their... I got so happy after seeing him😆😆
@rakurame6990
@rakurame6990 3 жыл бұрын
Good explanation on structure padding
@aashishmalhotra
@aashishmalhotra Жыл бұрын
I'm wondering why bloody YT algo doesn't recommend your channel in first year.
@Siva-fl1sq
@Siva-fl1sq 11 ай бұрын
Same feeling..
@Vishall00018
@Vishall00018 11 ай бұрын
Also*
@kryptoniangaming6082
@kryptoniangaming6082 7 ай бұрын
Maybe they haven't done the right SEO
@elchurisrivarna
@elchurisrivarna 2 ай бұрын
Same
@Learnwithme12e
@Learnwithme12e Ай бұрын
Becuz first you get all indian yt channel . And becuz this channel is international so it take time to search or scroll more and u I'll get .. but when you enter on this or other channel . KZbin algorithm catch your data and next time when you search any topic it gives you the channel you visit more 👍 Or it also depending on views system
@y_red_gold_a4280
@y_red_gold_a4280 3 жыл бұрын
Best explanation of the concept. Thank you so much.
@Aabara_ka_dabara
@Aabara_ka_dabara Жыл бұрын
something unique and new things I have learned💕💕
@vijaykounsalye
@vijaykounsalye 5 жыл бұрын
just awesome bro you have explained very well
@jishankhan6686
@jishankhan6686 3 жыл бұрын
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.
@premithagoni8634
@premithagoni8634 Жыл бұрын
Awesome Explanation sir,Thank you
@leepakshiyadav1643
@leepakshiyadav1643 2 жыл бұрын
crystal clear explanation, thanks a lot :)
@RozgarBharatIT
@RozgarBharatIT Жыл бұрын
Great Explanation!!
@multivalfran
@multivalfran 2 жыл бұрын
Excellent video my friend
@adithyay328
@adithyay328 4 жыл бұрын
Good, clear explanation!
@mudassar2012
@mudassar2012 2 жыл бұрын
very helpful video.....so good explanation
@donythomas7
@donythomas7 2 жыл бұрын
Excellent explanation 👍
@vishnu5466
@vishnu5466 4 жыл бұрын
Very Interesting!...
@mdkamrankhan4032
@mdkamrankhan4032 5 жыл бұрын
Great explanation Sir..
@tanmayghosh8311
@tanmayghosh8311 2 жыл бұрын
never thought we can optimize our code using this methods
@avronilbanerjee5302
@avronilbanerjee5302 2 жыл бұрын
excellent explanation
@amandarash135
@amandarash135 5 жыл бұрын
Best explanation
@rohiths-k2d
@rohiths-k2d Жыл бұрын
excellent lecture i have ever seen
@revan7600
@revan7600 2 жыл бұрын
Very nice explanation thank you
@nileshpokale862
@nileshpokale862 3 жыл бұрын
Excellent Explanation. All cleared
@haripriyaraja1890
@haripriyaraja1890 3 жыл бұрын
Nice clarification 😍
@gurlinkanaveenkumar7209
@gurlinkanaveenkumar7209 2 жыл бұрын
Tqu sir.
@ahmedsiraj2758
@ahmedsiraj2758 4 жыл бұрын
when declaring 3 char, it should give 4 as output, why giving 3
@jayaprakashthokala640
@jayaprakashthokala640 4 жыл бұрын
Word size is nothing but highest size of a data type used...
@anandg4018
@anandg4018 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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.
@harshalgarg1149
@harshalgarg1149 3 жыл бұрын
Great explanation.
@chandumadhumanthi9983
@chandumadhumanthi9983 4 жыл бұрын
Excellent👍
@shreyasingh222
@shreyasingh222 3 жыл бұрын
Thank you so much for detailed explanation.....
@yashikagoyal366
@yashikagoyal366 8 ай бұрын
It means if processor is of 64 bit Then size of the discussed structure will be 8 irrespective of the order.
@mohamedmaged2977
@mohamedmaged2977 5 жыл бұрын
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 .
@mamtaCoder
@mamtaCoder 5 жыл бұрын
Thank you sir...😃😇
@anjani_tiwari
@anjani_tiwari Жыл бұрын
Dhanyavaad🙏
@Jose_T131
@Jose_T131 2 жыл бұрын
Very well explained, thank you sir
@somasekhardakala2630
@somasekhardakala2630 5 жыл бұрын
very nice explained....thank you sir 🤝.....
@KrupaPrarthana
@KrupaPrarthana 11 ай бұрын
Wonderful ❤
@learnprogam
@learnprogam 4 жыл бұрын
You forget one point. In C programming structure have not any object. struct ABC A; here A is a variable of type struct ABC.
@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?
@mykun8737
@mykun8737 2 жыл бұрын
thanks
@aps_269
@aps_269 3 жыл бұрын
Amazing, 😍😍
@Cat_Sterling
@Cat_Sterling 4 жыл бұрын
That was just great! I was wondering about that for so long! But what is a "word" in computer architecture?
@zidanegimiga8180
@zidanegimiga8180 3 жыл бұрын
A word must be 4 or 8 bytes long, depending on the computer architecture I guess.
@47-6F-64
@47-6F-64 4 жыл бұрын
Amazing
@garapatianushadevi3427
@garapatianushadevi3427 3 жыл бұрын
Very good, just loved it
@kunalsoni7681
@kunalsoni7681 5 жыл бұрын
wow great one presentation sir..
@JohnDoe-kj2cs
@JohnDoe-kj2cs 4 жыл бұрын
Silky smooth!
@AlokSingh-jw8fr
@AlokSingh-jw8fr 3 жыл бұрын
struct abc { char b; int c; char a; double e; }x; Why it's output is 24? It should be 20 as per the concept sir has taught
@riyajoseph9261
@riyajoseph9261 3 жыл бұрын
Hii, as the size of double is 8, word size will be taken as 8. So, for this, it'll be 1(char) + 4(int) + 3(padding) + 1(char) + 7(padding) + 8(double). That'll give the output as 24.
@AlokSingh-jw8fr
@AlokSingh-jw8fr 3 жыл бұрын
@@riyajoseph9261 Thanks ma'am
@luhem_
@luhem_ 3 жыл бұрын
@@riyajoseph9261 I don't think word size will be taken as 8 ...
@luhem_
@luhem_ 3 жыл бұрын
@@AlokSingh-jw8fr it should be like this : For 32 bit system -> char b(1byte) + padding (3bytes)+int c(4bytes)+char a(1byte)+padding(3bytes)+double e(4 + 4 bytes) = 20 bytes .
@fidalgoverde
@fidalgoverde 4 жыл бұрын
Excellent job
@nikhilwagh1728
@nikhilwagh1728 Жыл бұрын
Great video
@shauryakumar2229
@shauryakumar2229 Жыл бұрын
Very helpful thanku
@somuseth
@somuseth 4 жыл бұрын
4:40 Sir, does it mean that total of 3 cyles are required to access the 3 variables???
@kumard-n9m
@kumard-n9m 4 жыл бұрын
Yes
@bharatbs7141
@bharatbs7141 4 жыл бұрын
Very Helpfull...Thank You :)
@yemreekinci
@yemreekinci 10 ай бұрын
Adamsın kral çok güzel anlattın.
@093raselrahman4
@093raselrahman4 3 жыл бұрын
Nothing to say ! Very easy explanation.
@liangzhang1722
@liangzhang1722 4 жыл бұрын
Life saving! Ty
@yashikagoyal366
@yashikagoyal366 8 ай бұрын
Also size of struct is not fixed. It varies with the processor.
@winxkin6056
@winxkin6056 3 жыл бұрын
it is very helpful
@kirankumarkakileti2811
@kirankumarkakileti2811 4 жыл бұрын
Awsome xplanation😍
@thienphucnguyenpham6944
@thienphucnguyenpham6944 3 жыл бұрын
i think it would be better with CC ^^
@rosako
@rosako 4 жыл бұрын
Instead of padding and wasting memory, why not just change the order to : struct abc{int b; char a; char c}?
@yahyaaouledamer1466
@yahyaaouledamer1466 2 жыл бұрын
amazing thanks so much
@EmpowerKnowledge00
@EmpowerKnowledge00 Жыл бұрын
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.
@ayushgemini
@ayushgemini 23 күн бұрын
Although you are using 64-bit system but check the compiler specs it must be compiling for 32-bit. So (4)+(1+3)+(4) = 12 gcc -v command in cmd will tell you compiler specs
@FraPiz
@FraPiz 2 жыл бұрын
wow, my mind is blown
@preciouslife1212
@preciouslife1212 2 жыл бұрын
thank you so much
@ElifArslan-l9g
@ElifArslan-l9g 3 жыл бұрын
thank you
@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 ;)
@vamshimedishetty5624
@vamshimedishetty5624 4 жыл бұрын
Gud explanation. Sir I have a doubt. If we declare 1000 character type data type(for eg: char a1,a2,...... a1000;) the 32 bit processor makes 1000 cycles each cycle taking of 4 bytes. As per you, memory wastage is 3000 bytes. Here, allocated memory for 1000 characters is 1000 and wasted memory is 3000 bytes. Is my answer right?
@pradhanan9413
@pradhanan9413 3 жыл бұрын
No, you're wrong
@patricktomchacko1909
@patricktomchacko1909 3 жыл бұрын
The CPU makes 250 cycles in 32 bit processor, because every cycle has 4 bytes, 250*4=1000 bytes. Why will there be any wastage, characters are declared contiguously and they take 1 byte each, so they will take 1000 bytes consecutively without leaving 3 empty cells after each character.
@vamshimedishetty5624
@vamshimedishetty5624 3 жыл бұрын
@@patricktomchacko1909 tq
Structure Packing in C
2:27
Neso Academy
Рет қаралды 127 М.
C_116 Structure Padding in C | C Language Tutorials
18:25
Jenny's Lectures CS IT
Рет қаралды 108 М.
КОНЦЕРТЫ:  2 сезон | 1 выпуск | Камызяки
46:36
ТНТ Смотри еще!
Рет қаралды 3,7 МЛН
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН
$1 vs $500,000 Plane Ticket!
12:20
MrBeast
Рет қаралды 122 МЛН
the cleanest feature in C that you've probably never heard of
8:13
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 184 М.
you will never ask about pointers again after watching this video
8:03
Introduction to Unions in C
6:58
Neso Academy
Рет қаралды 269 М.
How a Russian student invented a faster multiplication method
18:48
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Hash Tables and Hash Functions
13:56
Computer Science Lessons
Рет қаралды 1,6 МЛН
КОНЦЕРТЫ:  2 сезон | 1 выпуск | Камызяки
46:36
ТНТ Смотри еще!
Рет қаралды 3,7 МЛН