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.
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!
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).
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.
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.
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.
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.
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!
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
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.
@@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.
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.
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 . TH-cam 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
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.
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.
@@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.
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 !
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 .
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?
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 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 .
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.
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
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 ;)
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?
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.
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.
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!
I couldn't agree with you more , It's awesome how he explained it.
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).
Tnx
speachless!! you are soooo goood in explainging. thanks a lot
Yes agree.
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.
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.
Ok
trying to read how this works is way harder than having pictures and someone explaining. Thank you.
Perfect video to understand the importance of padding and its uses.
I m having 8 yrs of experience. Noone Clarified the doubt lk u..thank you..expecting more content from you
😱
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.
It is a good habit to put first the biggest type in ascending order to help compilator !
It is always the Indian sensei that makes me understand.
You are explaining 💯 times better than my college professor......
1000 bro
Explained very detailed, Cleared every doubt.
Thank you
Awesome work 👏
Lesson: we should be careful while arranging the srruct variables for better memory utilisation
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.
Such an amazing explanation! You made it so simple to understand, thank you :)
Missing to explain __attribute__((packed)) with unaligned memory access which is possible (with ARM).
#pragma pack(1)
Thank you sir for clearing the concept in such an easy way...
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!
Ok
1:15 not necessarily wrong, just that 1-packing is not efficient
you make every concept so easyyyyyyyyy.
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
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.
It is all about time and space.
What does addressable and accessable mean? and what is the difference could you please tell? couldnt find or understand from anywhere
@@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.
@@kitanowitsch thank you very much, i understood! have a nice day
Excellent explanation, thanks for your dedicated time in creating these videos. Regards from Mexico.
Thank you sir. Keep posting more on stacks, queues, linked lists
Amazing initiative by neso academy
Amazing 🤩
its has been clear. thanks Sir
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.
perfect example of 32byte and 64 byte difference thank you very much sir
Awesome explanation, thanks for sharing
Very useful video...Thank you sir.please try to put more videos in advanced c topics..
Thanks for the wonderful explanation.
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.
Have you tried enabling optimizations with your compiler?
really easy to understand for me. thanks a lot!!
Barney Stinson was their... I got so happy after seeing him😆😆
Good explanation on structure padding
I'm wondering why bloody YT algo doesn't recommend your channel in first year.
Same feeling..
Also*
Maybe they haven't done the right SEO
Same
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 . TH-cam 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
Best explanation of the concept. Thank you so much.
something unique and new things I have learned💕💕
just awesome bro you have explained very well
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.
Awesome Explanation sir,Thank you
crystal clear explanation, thanks a lot :)
Great Explanation!!
Excellent video my friend
Good, clear explanation!
very helpful video.....so good explanation
Excellent explanation 👍
Very Interesting!...
Great explanation Sir..
never thought we can optimize our code using this methods
excellent explanation
Best explanation
excellent lecture i have ever seen
Excellent Explanation. All cleared
Very nice explanation thank you
Nice clarification 😍
Tqu sir.
when declaring 3 char, it should give 4 as output, why giving 3
Word size is nothing but highest size of a data type used...
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
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.
@@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.
The null character \0 is 1 byte. It is always at the end of all char or int.
Great explanation.
Excellent👍
Thank you so much for detailed explanation.....
It means if processor is of 64 bit
Then size of the discussed structure will be 8 irrespective of the order.
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 !
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 .
Thank you sir...😃😇
Dhanyavaad🙏
Very well explained, thank you sir
very nice explained....thank you sir 🤝.....
Wonderful ❤
You forget one point. In C programming structure have not any object. struct ABC A; here A is a variable of type struct ABC.
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?
thanks
Amazing, 😍😍
That was just great! I was wondering about that for so long! But what is a "word" in computer architecture?
A word must be 4 or 8 bytes long, depending on the computer architecture I guess.
Amazing
Very good, just loved it
wow great one presentation sir..
Silky smooth!
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
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.
@@riyajoseph9261 Thanks ma'am
@@riyajoseph9261 I don't think word size will be taken as 8 ...
@@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 .
Excellent job
Great video
Very helpful thanku
Very Helpfull...Thank You :)
4:40
Sir, does it mean that total of 3 cyles are required to access the 3 variables???
Yes
Adamsın kral çok güzel anlattın.
Nothing to say ! Very easy explanation.
Life saving! Ty
Awsome xplanation😍
it is very helpful
Also size of struct is not fixed. It varies with the processor.
i think it would be better with CC ^^
Instead of padding and wasting memory, why not just change the order to : struct abc{int b; char a; char c}?
amazing thanks so much
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.
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
wow, my mind is blown
thank you so much
thank you
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 ;)
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?
No, you're wrong
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.
@@patricktomchacko1909 tq