Flexible Array Members | C Programming Tutorial

  Рет қаралды 2,676

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 33
@gpythona
@gpythona Жыл бұрын
professor, thank you very much for all these precious tutorials, in addition to the fact that you are a great lecturer, this and many more corner cases which are not covered very much on the internet what separates you from the whole internet. I appreciate each and every video you've published, they're all just great. Thank you for everything, greetings from Turkey 🇹🇷
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome and thank you for this kind feedback! :-) I really have fun covering topics like this that are more niche and aren't often covered by other sources online.
@starc0w
@starc0w Жыл бұрын
Very good tutorial! I love your stil! Thank you so much! One thing I have missed: The flexible array member does not need any space itself. So far so good. However, more space can be incurred by the alignment. struct data { char c; int array[]; }; Sizeof struct data now results in 4 bytes and not 1 byte, which one might have expected. This, although the flexible array member does not need any space. But since the elements of the flexible array member must always come directly to the end of the structure and these in turn must be aligned according to their types, the compiler inserts padding within the structure depending on the situation (alignment of int is "normally" a multiple of 4). The following structure has now a sizeof of 16 bytes (alignment of long long is "normally" a multiple of 8): struct data { char elements[9]; long long array[]; }; Maybe a topic for another video (This and the topic of alignment in general)? Cheers
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Wow thanks for the 'super thanks'! :-) And yes structure padding can come into play with structs, this video covers the concept: kzbin.info/www/bejne/n3WnZ6Shbaagotk. Maybe one day I can do a video on structure padding and flexible array members specifically. :-)
@nuligebla1173
@nuligebla1173 Жыл бұрын
typedef struct { int id; int salary; char name[]; char nickname[]; //error: flexible array member not at end of struct } Employee; typedef struct { int id; int salary; char name[50]; char nickname[]; //works! no warnings or issues! } Employee; printf("sizeof (struct employee): %zu ", sizeof(Employee)); // sizeof (struct employee): 60 // 4 + 4 + 50 + ? // (2 extra bytes: offset? not a pointer, that would be 4...) just an experiment... great lesson. thanks!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Nice experimenting! :-) I suspect the extra two bytes is structure padding: kzbin.info/www/bejne/n3WnZ6Shbaagotk
@nuligebla1173
@nuligebla1173 Жыл бұрын
that makes sense, thanks! these videos are really helping!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@nuligebla1173 You're welcome, glad to hear the videos are helping! 🙂
@ple9652
@ple9652 9 ай бұрын
Thank you so much! You just saved me hours of debugging!
@logos_42
@logos_42 Жыл бұрын
Thanks a lot. Your exercises are really valuable for a coding student like me🙏
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome David, I’m glad you’re finding the exercises valuable! :-)
@Algebraiic
@Algebraiic Жыл бұрын
unrelated but currently learning RV32I and i couldn’t help to imagine how goated portfolio courses would be at teaching the logic of it 🔥
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Hmmm, I don't have any plans to cover that topic, but you never know, maybe one day. :-)
@fifaham
@fifaham Жыл бұрын
It would be nice to have the compiler assume a default size for the "name" if placed as first element in the struct. Later it will change the addresses accordingly after determaining the desired size. I wonder if this is something the compiler designers have thought of! The issue is as a programmers at times we get so many bugs errors (LOL) that need to be fixed, the less the errors the more we can focus on develpment. Great presentation. Thank you Kevin.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it, and you're welcome! :-)
@JamBadguy
@JamBadguy 9 ай бұрын
fantastic vid, subbed and liked, was super helpful for understanding :)
@togosakutaro5882
@togosakutaro5882 Жыл бұрын
Great 2 hear from u again. Are u feeling better now?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Yes, I'm feeling much better now and hoping to get back to regularly posting videos. :-)
@yogeshchauhan9401
@yogeshchauhan9401 Жыл бұрын
Will that flexible member follow structure padding ?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's a good question! Ultimately the compiler has some say in how structure padding will work. And I'm not 100% sure myself. But my understanding is that the structure padding will be done *before* the flexible array member, and then after that, we just have an array accessed via struct_variable->array[3], etc. This article seems to confirm that understanding... "the flexible array member will not participate in the trailing padding of the structure": sites.google.com/site/embeddedmonologue/home/c-programming/data-alignment-structure-padding-and-flexible-array-member
@dimitrioskalfakis
@dimitrioskalfakis Жыл бұрын
interesting and strange feature. since one uses malloc anyway why not define *name instead of name[] and proceed with pointer allocation as usual?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That’s a really good question! :-) We could do that too, but the block of memory allocated for name could be “somewhere else” in memory and name would be a pointer to that block of memory. So there would be a block of memory for the struct and another one for the array. More blocks of memory could be worse for heap fragmentation: chortle.ccsu.edu/assemblytutorial/chapter-33/ass33_3.html. Also if the struct is dynamically allocated we would then have to use free() twice, once for the struct and once for the array.
@awekeningbro1207
@awekeningbro1207 7 ай бұрын
so we need to somehow hardcode the length of the data before the compilation? how is this method any useful?
@PortfolioCourses
@PortfolioCourses 7 ай бұрын
No, that could come from user input. We’re using dynamic memory allocation so we can make the length whatever we like at execution time.
@starc0w
@starc0w Жыл бұрын
10:36 Obviously you are creating the struct on the stack here. Is there any case where it makes sense to create a struct with a flexible array member on the stack? If this struct is on the stack, any access to the flexible array is UB, because it is not clear what follows behind this struct on the stack. Or am I perhaps missing something? Shouldn't it be consequently impossible to create a struct with a flexible array member on the stack?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m doing that for the reason stated in the video, to demonstrate that we cannot statically initialize flexible array members. If it wasn’t on the stack it couldn’t be statically initialized because we cannot statically initialize things on the heap. It’s something that is specifically not allowed in C so I’m demonstrating a specific example of that.
@starc0w
@starc0w Жыл бұрын
@@PortfolioCourses Ok great, thanks for the explanation! Would you also advise against creating such a struct on the stack in any case? Or am I missing a scenario where this could make sense?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
If it is a struct that contains different members that are useful in different related contexts someone might declare one on the stack and use it in contexts where the flexible array member isn’t used. I’m not sure if a specific situation off the top of my head, maybe the struct stores “results” in an array along with statuses, someone might make a struct on the stack just for storing statuses maybe for the sake of outputting a report or something. As long as they don’t try to initiate the flexible array member, they could still use the struct on the stack and the other members.
@starc0w
@starc0w Жыл бұрын
@@PortfolioCourses Ok, thank you very much!
@keithg8875
@keithg8875 Жыл бұрын
'promo sm'
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I don't get it? :-)
Null Terminator | C Programming Tutorial
15:05
Portfolio Courses
Рет қаралды 3,4 М.
struct Basics | C Programming Tutorial
24:44
Portfolio Courses
Рет қаралды 138 М.
小丑和白天使的比试。#天使 #小丑 #超人不会飞
00:51
超人不会飞
Рет қаралды 44 МЛН
Секрет фокусника! #shorts
00:15
Роман Magic
Рет қаралды 104 МЛН
why do header files even exist?
10:53
Low Level Learning
Рет қаралды 404 М.
the cleanest feature in C that you've probably never heard of
8:13
Low Level Learning
Рет қаралды 136 М.
Is Computer Science still worth it?
20:08
NeetCodeIO
Рет қаралды 80 М.
Structure Padding | C Programming Tutorial
8:10
Portfolio Courses
Рет қаралды 11 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 306 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 806 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,2 МЛН
Memory Leaks And How To Prevent Them | C Programming Tutorial
15:15
Portfolio Courses
Рет қаралды 9 М.
A const int is not a constant.
9:16
Jacob Sorber
Рет қаралды 68 М.