Structure Padding in C

  Рет қаралды 252,733

Neso Academy

Neso Academy

Күн бұрын

Пікірлер: 259
@ahamd19
@ahamd19 4 жыл бұрын
speachless!! you are soooo goood in explainging. thanks a lot
@koperaljali3253
@koperaljali3253 4 жыл бұрын
Yes agree.
@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
@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
@anshhmehta
@anshhmehta 2 жыл бұрын
Perfect video to understand the importance of padding and its uses.
@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.
@manoharbompella418
@manoharbompella418 3 жыл бұрын
Explained very detailed, Cleared every doubt. Thank you
@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 !
@michaelhernandez2075
@michaelhernandez2075 Жыл бұрын
trying to read how this works is way harder than having pictures and someone explaining. Thank you.
@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 жыл бұрын
😱
@adeshgaikwad2192
@adeshgaikwad2192 5 жыл бұрын
Thank you sir for clearing the concept in such an easy way...
@andistheinforitbutso7513
@andistheinforitbutso7513 2 жыл бұрын
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 :)
@sujitsatpute3741
@sujitsatpute3741 3 жыл бұрын
You are explaining 💯 times better than my college professor......
@SJ_46
@SJ_46 2 жыл бұрын
1000 bro
@ArjanvanVught
@ArjanvanVught 5 жыл бұрын
Missing to explain __attribute__((packed)) with unaligned memory access which is possible (with ARM).
@ivanleon6164
@ivanleon6164 3 жыл бұрын
#pragma pack(1)
@nishantraj5020
@nishantraj5020 4 жыл бұрын
you make every concept so easyyyyyyyyy.
@khoahuynh6884
@khoahuynh6884 2 жыл бұрын
It is always the Indian sensei that makes me understand.
@mcbotface
@mcbotface 5 жыл бұрын
Thank you sir. Keep posting more on stacks, queues, linked lists
@santanacasas1
@santanacasas1 2 жыл бұрын
Excellent explanation, thanks for your dedicated time in creating these videos. Regards from Mexico.
@paschikshehu7988
@paschikshehu7988 3 жыл бұрын
1:15 not necessarily wrong, just that 1-packing is not efficient
@05_abhisheksharma13
@05_abhisheksharma13 3 жыл бұрын
Amazing initiative by neso academy
@ronnieahabwomugisha5283
@ronnieahabwomugisha5283 5 жыл бұрын
its has been clear. thanks Sir
@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.
@あまね-y6t
@あまね-y6t 4 жыл бұрын
really easy to understand for me. thanks a lot!!
@vijaykounsalye
@vijaykounsalye 5 жыл бұрын
just awesome bro you have explained very well
@y_red_gold_a4280
@y_red_gold_a4280 2 жыл бұрын
Best explanation of the concept. Thank you so much.
@mikestock1848
@mikestock1848 5 ай бұрын
Awesome explanation, thanks for sharing
@dhruvsakariya3129
@dhruvsakariya3129 2 жыл бұрын
perfect example of 32byte and 64 byte difference thank you very much sir
@adithyay328
@adithyay328 4 жыл бұрын
Good, clear explanation!
@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
@mdkamrankhan4032
@mdkamrankhan4032 5 жыл бұрын
Great explanation Sir..
@leepakshiyadav1643
@leepakshiyadav1643 2 жыл бұрын
crystal clear explanation, thanks a lot :)
@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
@pranavkshirsagar146
@pranavkshirsagar146 4 жыл бұрын
Barney Stinson was their... I got so happy after seeing him😆😆
@VivekYadav-ds8oz
@VivekYadav-ds8oz 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?
@premithagoni8634
@premithagoni8634 Жыл бұрын
Awesome Explanation sir,Thank you
@Aabara_ka_dabara
@Aabara_ka_dabara Жыл бұрын
something unique and new things I have learned💕💕
@QuratulainAzhar
@QuratulainAzhar 7 ай бұрын
Amazing 🤩
@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.
@rakurame6990
@rakurame6990 2 жыл бұрын
Good explanation on structure padding
@multivalfran
@multivalfran 2 жыл бұрын
Excellent video my friend
@rohiths-k2d
@rohiths-k2d Жыл бұрын
excellent lecture i have ever seen
@vishnu5466
@vishnu5466 3 жыл бұрын
Very Interesting!...
@mudassar2012
@mudassar2012 2 жыл бұрын
very helpful video.....so good explanation
@RozgarBharatIT
@RozgarBharatIT Жыл бұрын
Great Explanation!!
@amandarash135
@amandarash135 5 жыл бұрын
Best explanation
@umairalvi7382
@umairalvi7382 5 жыл бұрын
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).
@nileshpokale862
@nileshpokale862 3 жыл бұрын
Excellent Explanation. All cleared
@donythomas7
@donythomas7 2 жыл бұрын
Excellent explanation 👍
@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
@avronilbanerjee5302
@avronilbanerjee5302 2 жыл бұрын
excellent explanation
@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 3 ай бұрын
What does addressable and accessable mean? and what is the difference could you please tell? couldnt find or understand from anywhere
@kitanowitsch
@kitanowitsch 3 ай бұрын
@@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 3 ай бұрын
@@kitanowitsch thank you very much, i understood! have a nice day
@kunalsoni7681
@kunalsoni7681 5 жыл бұрын
wow great one presentation sir..
@revan7600
@revan7600 2 жыл бұрын
Very nice explanation thank you
@shreyasingh222
@shreyasingh222 3 жыл бұрын
Thank you so much for detailed explanation.....
@chandumadhumanthi9983
@chandumadhumanthi9983 4 жыл бұрын
Excellent👍
@haripriyaraja1890
@haripriyaraja1890 3 жыл бұрын
Nice clarification 😍
@aashishmalhotra
@aashishmalhotra Жыл бұрын
I'm wondering why bloody YT algo doesn't recommend your channel in first year.
@Siva-fl1sq
@Siva-fl1sq 10 ай бұрын
Same feeling..
@Vishall00018
@Vishall00018 9 ай бұрын
Also*
@kryptoniangaming6082
@kryptoniangaming6082 5 ай бұрын
Maybe they haven't done the right SEO
@elchurisrivarna
@elchurisrivarna 29 күн бұрын
Same
@harshalgarg1149
@harshalgarg1149 3 жыл бұрын
Great explanation.
@JohnDoe-kj2cs
@JohnDoe-kj2cs 3 жыл бұрын
Silky smooth!
@garapatianushadevi3427
@garapatianushadevi3427 3 жыл бұрын
Very good, just loved it
@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.
@Jose_T131
@Jose_T131 2 жыл бұрын
Very well explained, thank you sir
@KrupaPrarthana
@KrupaPrarthana 9 ай бұрын
Wonderful ❤
@somasekhardakala2630
@somasekhardakala2630 5 жыл бұрын
very nice explained....thank you sir 🤝.....
@aps_269
@aps_269 3 жыл бұрын
Amazing, 😍😍
@093raselrahman4
@093raselrahman4 3 жыл бұрын
Nothing to say ! Very easy explanation.
@shauryakumar2229
@shauryakumar2229 Жыл бұрын
Very helpful thanku
@mamtaCoder
@mamtaCoder 5 жыл бұрын
Thank you sir...😃😇
@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.
@nikhilwagh1728
@nikhilwagh1728 Жыл бұрын
Great video
@liangzhang1722
@liangzhang1722 4 жыл бұрын
Life saving! Ty
@bharatbs7141
@bharatbs7141 4 жыл бұрын
Very Helpfull...Thank You :)
@fidalgoverde
@fidalgoverde 4 жыл бұрын
Excellent job
@kirankumarkakileti2811
@kirankumarkakileti2811 4 жыл бұрын
Awsome xplanation😍
@47-6F-64
@47-6F-64 4 жыл бұрын
Amazing
@yemreekinci
@yemreekinci 8 ай бұрын
Adamsın kral çok güzel anlattın.
@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.
@chintanchatterjee9477
@chintanchatterjee9477 Жыл бұрын
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 11 ай бұрын
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
@gurlinkanaveenkumar7209
@gurlinkanaveenkumar7209 2 жыл бұрын
Tqu sir.
@FraPiz
@FraPiz 2 жыл бұрын
wow, my mind is blown
@winxkin6056
@winxkin6056 3 жыл бұрын
it is very helpful
@tanmayghosh8311
@tanmayghosh8311 2 жыл бұрын
never thought we can optimize our code using this methods
@RezaulKarim-cq5ft
@RezaulKarim-cq5ft 3 жыл бұрын
amazing!
@anjani_tiwari
@anjani_tiwari Жыл бұрын
Dhanyavaad🙏
@yahyaaouledamer1466
@yahyaaouledamer1466 2 жыл бұрын
amazing thanks so much
@krishnakumar-kh5vo
@krishnakumar-kh5vo 2 жыл бұрын
well explained
@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?
@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 ;)
@silentcat4292
@silentcat4292 4 жыл бұрын
Crystal clear
@okavango5937
@okavango5937 3 жыл бұрын
If the CPU accesses the addresses only in "word" sizes, then why is 'sizeof(char)' 1? It sould also be 4 (in 32bit) right?
@RAVITEJAP-ss9rl
@RAVITEJAP-ss9rl 3 жыл бұрын
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....
@monarchofaether
@monarchofaether Жыл бұрын
I defined int,char,int in 64-bit computer. I expected sizeof this struct 16 byte but it was 12 byte. 4(int) + 1(char) + 3(blank) + 4(int) = 12byte. Why is it like this ? Shouldn't take also 4 byte ?
@sushaanthsureshkumar
@sushaanthsureshkumar 11 ай бұрын
#include int64_t i2 = 0x0000444400004444LL; This is how you declare 64 bit integer. regular int is not special, it's 32bit i.e 4 bytes. In your problem the alignment value is 4 so 4*3=12 bytes.
@karthicknandhakumar264
@karthicknandhakumar264 5 жыл бұрын
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 5 жыл бұрын
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 4 жыл бұрын
@@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
@RahulRazj
@RahulRazj 4 жыл бұрын
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 4 жыл бұрын
thats called padding.
@_sk_beatz_6051
@_sk_beatz_6051 Жыл бұрын
Your processor is 64 bit architecture may be
@purubhargava8096
@purubhargava8096 4 жыл бұрын
bhaot hard sirjee
@KRGAMAING
@KRGAMAING Жыл бұрын
thx for the super nice video
Structure Packing in C
2:27
Neso Academy
Рет қаралды 125 М.
Introduction to Unions in C
6:58
Neso Academy
Рет қаралды 262 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
Structure Padding in C and C++ | C++ Tutorial for Beginners
5:37
Codaming - Simplified Learning
Рет қаралды 12 М.
Bit Fields in C. What are they, and how do I use them?
13:26
Jacob Sorber
Рет қаралды 85 М.
you will never ask about pointers again after watching this video
8:03
C_116 Structure Padding in C | C Language Tutorials
18:25
Jenny's Lectures CS IT
Рет қаралды 105 М.
Call By Value & Call By Reference in C
8:34
Neso Academy
Рет қаралды 1,4 МЛН
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 395 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
Ep 078: Data Structure Alignment and Endianness
12:19
Intermation
Рет қаралды 19 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН