Structure Padding in C
ฝัง
- เผยแพร่เมื่อ 26 ส.ค. 2024
- C Programming: Structure Padding in C Programming.
Topics discussed:
1) Memory allocation to Structures.
2) Need for Structure Padding.
3) The concept of Structure Padding.
C Programming Lectures: goo.gl/7Eh2SS
Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
Contribute: www.nesoacademy...
Memberships: bit.ly/2U7YSPI
Books: www.nesoacademy...
Website ► www.nesoacademy...
Forum ► forum.nesoacade...
Facebook ► goo.gl/Nt0PmB
Twitter ► / nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#CProgrammingByNeso #CProgramming #StructuresInC #StructurePadding #Structure&Union
speachless!! you are soooo goood in explainging. thanks a lot
Yes agree.
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
Thanks God ! Your video saves my embedded programming career
ờ mây zing gút chóp
Ok
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
Perfect video to understand the importance of padding and its uses.
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.
trying to read how this works is way harder than having pictures and someone explaining. Thank you.
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.
Missing to explain __attribute__((packed)) with unaligned memory access which is possible (with ARM).
#pragma pack(1)
It is a good habit to put first the biggest type in ascending order to help compilator !
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.
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
I m having 8 yrs of experience. Noone Clarified the doubt lk u..thank you..expecting more content from you
😱
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.
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?
Awesome explanation, thanks for sharing
Such an amazing explanation! You made it so simple to understand, thank you :)
Thank you sir. Keep posting more on stacks, queues, linked lists
Thank you sir for clearing the concept in such an easy way...
Very well explained. Thanks!
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?
thats called padding.
Your processor is 64 bit architecture may be
something unique and new things I have learned💕💕
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.
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
its has been clear. thanks Sir
If we use one int,one double and again one int variable then the size of variable should be 16 but it is 24 why???
Because double is 8 bytes long, so the word size would be 8 bytes long. Hence 8*3 = 24 bytes
@@kumargaurav8234 your reply cleared every doubt i had thanks
Very useful video...Thank you sir.please try to put more videos in advanced c topics..
you make every concept so easyyyyyyyyy.
It is always the Indian sensei that makes me understand.
perfect example of 32byte and 64 byte difference thank you very much sir
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?
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).
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
Good explanation on structure padding
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.
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.
really easy to understand for me. thanks a lot!!
Amazing 🤩
Well explained, thank you so much
Excellent explanation, thanks for your dedicated time in creating these videos. Regards from Mexico.
Barney Stinson was their... I got so happy after seeing him😆😆
Excellent video my friend
Very nice explanation thank you
Great Explanation!!
excellent explanation
Very Interesting!...
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 means if processor is of 64 bit
Then size of the discussed structure will be 8 irrespective of the order.
Amazing initiative by neso academy
just awesome bro you have explained very well
Thanks for the wonderful explanation.
crystal clear explanation, thanks a lot :)
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?
Best explanation of the concept. Thank you so much.
Adamsın kral çok güzel anlattın.
Best explanation
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....
Awesome Explanation sir,Thank you
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 .
thanks sir, but why you are not prepared for gate computer science
Good, clear explanation!
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.
Tqu sir.
Very helpful thanku
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
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
Nice clarification 😍
very helpful video.....so good explanation
struct mystrct
{
char a;
double b;
char a;
}; what will be sizeof struct in 32 bit system
it shows 10 in m y compiler i donno whats wrong ...it should be 16 though
@@sameeksharl7655 yaa same here
@@sameeksharl7655
Optimization you need optimization is off let it on
Instead of padding and wasting memory, why not just change the order to : struct abc{int b; char a; char c}?
i think it would be better with CC ^^
If the CPU accesses the addresses only in "word" sizes, then why is 'sizeof(char)' 1? It sould also be 4 (in 32bit) right?
Wonderful ❤
Dhanyavaad🙏
thanks
wow, my mind is blown
Thank you so much for detailed explanation.....
Great explanation Sir..
Sir in 2nd case also 2 cycles are used for 8 bytes.then what is the use of padding.
Second case is 3 cycles and not 2.
using padding is a compiler thing.
You will care about aligning you variables if you program in assembly but in c the compiler will do that for you.
Aligning is great for higher performance and speed but take more memory
Great video
wow bro just thank you
amazing thanks so much
4:40
Sir, does it mean that total of 3 cyles are required to access the 3 variables???
Yes
never thought we can optimize our code using this methods
Amazing
thank you so much
it is very helpful
Amazing explanation. Thank you 😃
thank you
excellent lecture i have ever seen
Also size of struct is not fixed. It varies with the processor.
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 ;)
well explained
Very well explained, thank you sir
Excellent Explanation. All cleared
Amazing explanation
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?
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
@@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??
@@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..
@@debabratamohapatra6638 thanks for clearing my doubt