The Inline Keyword in C.

  Рет қаралды 59,069

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер
@tannerted
@tannerted 2 жыл бұрын
A couple other things to watch out for with inlining: 1) It usually makes executable size large because it is basically copy-pasting instructions everywhere. This can be an issue, especially in embedded systems. 2) Because there are more instructions in a given routine, the instruction cache is less likely to be able to hold everything so you might get more expensive cache misses with the increased code size.
@soniablanche5672
@soniablanche5672 11 ай бұрын
yeah, but performance is more important than size for regular computers
@BritishBeachcomber
@BritishBeachcomber 2 жыл бұрын
Back in the stone age, assembly was the first thing I learned. The best thing I ever did. I could then picture the code that compilers generated and instinctively know how, why and when to optimize.
@AyushBhattfe
@AyushBhattfe 4 жыл бұрын
In all this Javascript Framework mess thanks for choosing C!
@kermitdafrog8
@kermitdafrog8 4 жыл бұрын
C will always be King.
@framepointer
@framepointer 3 жыл бұрын
@@kermitdafrog8 rust is trying to take its crown
@kermitdafrog8
@kermitdafrog8 3 жыл бұрын
@@framepointer let me repeat. C will ALWAYS be King.
@framepointer
@framepointer 3 жыл бұрын
@@kermitdafrog8 well god damn, so be it!
@jankubala659
@jankubala659 3 жыл бұрын
@@kermitdafrog8 What about C++?
@cyberdank3160
@cyberdank3160 4 жыл бұрын
The double parenthesis when using __attribute or __attribute__ are there to allow it to be "macroed" in c versions that do not support variadic macros.
@JacobSorber
@JacobSorber 4 жыл бұрын
Ah. That makes sense. Thanks.
@benjaminshinar9509
@benjaminshinar9509 4 жыл бұрын
can you explain it in a simpler way (more verbose)?
@cyberdank3160
@cyberdank3160 4 жыл бұрын
@@JacobSorber No problem. I love your videos keep them going :)
@cyberdank3160
@cyberdank3160 4 жыл бұрын
@@benjaminshinar9509 Basically because attributes are different per compiler you would probably want to set up macros accompanied by #ifdefs to make your code compilable by multiple compilers. However because __attribute__ takes a variable number of arguments, macros need to take a variable number of arguments too (they need to be variadic) which is not supported by all c versions/compilers. Placing double parentheses makes compilers treat the series of arguments as a single argument bypassing the need for variadic macros. I hope you understand my explanation :)
@supaderrick3502
@supaderrick3502 4 жыл бұрын
@@cyberdank3160 Thank you :)
@socat9311
@socat9311 4 жыл бұрын
to put an irish tone in it, your content is fooking awesome
@oj0024
@oj0024 3 жыл бұрын
This was missing extern inline. Instead of supplying two different implementations of the function, you can but the inline one in the header file and write an extern inline prototype in exactly one translation unit. This behaves similar to static inline, but is better because static inline may create a copy of the function in every translation unit, assuming it isn't inlined, whiles the extern inline version would, if it isn't inlined, always fall back on the extern inline version, which is only ommited in one translation unit.
@lucianoaraujo6604
@lucianoaraujo6604 4 жыл бұрын
Thank you for your time, effort and content, I love your videos! By the way... Nice t-shirt :)
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks!
@alexmungan9950
@alexmungan9950 Жыл бұрын
The point of the inline keyword is to allow you to provide the definition (not declaration!) in header files (w/o causing multiple definition errors) so that a definition, rather than just declaration, of a function can be present in each compilation unit since the compiler needs the definition in the compilation unit to inline a function.
@nontraditionaltech2073
@nontraditionaltech2073 4 жыл бұрын
Jacob, I love all of your content that allows us to get under the hood! Is there a list of resources (besides you KZbin videos) that you would highly recommend?
@AntonioLongo91
@AntonioLongo91 4 жыл бұрын
Hi Jacob, I found your video while searching material to prepare for a technical interview for an embedded kind of position. I learned a lot and I'm happy to explore your channel. Thank you very much for your hard work.
@nttn3666
@nttn3666 4 жыл бұрын
Love your content!! This series is great :)
@JacobSorber
@JacobSorber 4 жыл бұрын
Glad you enjoy it!
@cihatkececi2310
@cihatkececi2310 2 жыл бұрын
Inline keyword does not ensure inlining, however, it increases the probability of inlining. The compiler (at least clang do it) gives point to each function for inlining and inlines that function if it has a high point for inlining. If we declare a function as inline, it increases that point.
@codertypist
@codertypist 4 жыл бұрын
Now THAT'S an intro!
@plumaligera8885
@plumaligera8885 3 жыл бұрын
This video is great! Thanks for the thorough explanation!
@JacobSorber
@JacobSorber 3 жыл бұрын
My pleasure!
@amrtcpp6203
@amrtcpp6203 4 жыл бұрын
Thank you for this video Jacob.
@wendolinmendoza517
@wendolinmendoza517 2 жыл бұрын
It has been helpful, indeed. Thank you! You always explain yourself very clear
@prajwalshetye9034
@prajwalshetye9034 4 жыл бұрын
Your videos are awesome Jacob ! Don't ever get discouraged by low subscriber count, we like your content ! 😄 Any plans on making videos on how compiler optimization works ? if not will you please suggest some resources to understand the same ?
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks! Not discouraged. In fact, I was just thinking, "Wow, I have 23k subscribers. Cool. I should celebrate." 🎉 I have compiler optimization on my topic list. Not sure how long it will take me to get to them (it's a solid list). As for resources, there are good compiler books out there (Muchnick, Aho+), lots of online resources, but I think compilers themselves are one of the best teachers. You can play with different programs and see how optimizations are applied. You can check out the compiler source code and see how they are implemented. And, of course, you can try your hand at implementing your own optimizations.
@prajwalshetye9034
@prajwalshetye9034 4 жыл бұрын
@@JacobSorber Thank You! for your valuable time and suggestions . Eagerly waiting for your next video 😁
@tonysofla
@tonysofla 4 жыл бұрын
My full preemptive multitasking routine for 4 tasks on an 8051 push A push B push PSW mov A,SP xch A,stack3 xch A,stack2 xch A,stack1 mov SP,A pop PSW pop B pop A clr PSW.5 clr TMR2CN0.7 reti
@rtzstvn
@rtzstvn 2 жыл бұрын
solid video about inline! I learned something new!
@sodiboo
@sodiboo 4 жыл бұрын
I don’t know why I’m being recommended C related videos of yours recently (maybe because I’ve googled a lot of things because I did a bit of C# recently) but they’re damn interesting, like I don’t do C (from my understanding if I would, C++ would probably be a better idea?) but it’s still fun to learn about features in languages I don’t know
@JacobSorber
@JacobSorber 4 жыл бұрын
The video chooses the viewer. 😀 Glad you're enjoying them.
@netanelkomm5636
@netanelkomm5636 Ай бұрын
I knew you'd have a video about this! 😄
@sukivirus
@sukivirus 4 жыл бұрын
nice content :) thanks. Can you make series on compiler design course or operating system course?
@JacobSorber
@JacobSorber 4 жыл бұрын
Sure. I already have videos on a variety of operating system topics (threads, processes, IPC). And, there are definitely more to come.
@michaelbrumbaugh5389
@michaelbrumbaugh5389 2 жыл бұрын
I would also love to see a few topics on this channel for compiler design!
@artemiocabrillosjr.244
@artemiocabrillosjr.244 4 жыл бұрын
Thank you for this video Jacob. More power to this channel 🌟. Ps. Please make a video on debugging C programs using syslog server
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks. I'll add syslog-based debugging to the list and see what I can do.
@CoyMcBob
@CoyMcBob 4 жыл бұрын
I mean you should be able to just define the inlined function in the header file, only once, not static. That works in c++ at least on any modern compiler (past 20 years). Having two separate implementations is definitely not a used solution by anyone of course haha, weird that you even describe it as something that some people do.
@stevedonkers9087
@stevedonkers9087 3 жыл бұрын
I didn't realize this is what inline meant in C. In Pascal the inline keyword allows you to insert machine code into the executable directly (at least in Borland Turbo Pascal, anyway). That was very useful in the 386/486 days when you needed graphics routines to be as fast as possible but you didn't want to write the entire thing in straight assembly. Completely different, and interesting.
@francoisloriot2674
@francoisloriot2674 2 жыл бұрын
wow yes I remember that. that was a very cool feature. I whish you could do that in modern languages. but when you think about it's very close to inline in C, as it would probably just stick the assembly code at every call site.
@stevenbroshar7948
@stevenbroshar7948 8 ай бұрын
How does one use 'extern inline'? I've seen that mentioned on SO, but do not understand it.
@stevenbroshar7948
@stevenbroshar7948 8 ай бұрын
Thanks for the examples. A question: what is the use case to have two versions of the same function where one is for inlining and one is not. Seems to defeat the purpose of inline IMO. And it's confusing since having two implementations of a function is not inherently something related to the general concept of inlining.
@decky1990
@decky1990 Жыл бұрын
Great video! Oils you just have a look at your audio levels? I’ve my AirPods on full blast and I can’t hear a thing
@decky1990
@decky1990 Жыл бұрын
KZbin seems to recommends anywhere from -13 to -15 LUFS. Would be interesting to analyse and see what the video file is producing
@nowhereman1813
@nowhereman1813 4 жыл бұрын
Thanks for the video! Question - would it be better to use function-like define to enforce inlining?
@diegonayalazo
@diegonayalazo 3 жыл бұрын
Thanks
@abhishekupadhyay1929
@abhishekupadhyay1929 4 жыл бұрын
Hi Jacob, I inspired by your videos all the time, could you please explain the "cmake" utility?
@אליהופרוייליך
@אליהופרוייליך 2 жыл бұрын
Jacob, thanks you for all the content! it's perfect! I have a question: Is inline behave the same in Cpp?
@codex8797
@codex8797 2 жыл бұрын
I just started learning c++ in college, but according to the book the professor recommend as source, the inline in c++ is actually what you would expect: removes the call to the function. The book is extremely old thou (and hasn't been updated in years), so maybe it's not like that now a days.
@אליהופרוייליך
@אליהופרוייליך 2 жыл бұрын
@@codex8797 hi, i did some tests with cpp code and it's not so simple. it's not always inline the function...
@codex8797
@codex8797 2 жыл бұрын
thanks for the info bro. I guess compilers are complex beasts after all
@edgeeffect
@edgeeffect 2 жыл бұрын
I'm old and haven't done any C or C++ for many many years.... so excuse me if my knowledge is very out-of-date (I don't think C had `inline` last time I used it ;) )... `inline` definitely will inline a function in C++, yes?
@magno5157
@magno5157 4 жыл бұрын
Well, naturally, could you please make another video talking about all the things about inline?
@JacobSorber
@JacobSorber 4 жыл бұрын
You're going to need to be more specific.
@amaia6393
@amaia6393 4 жыл бұрын
I love that t shirt!!!
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks. I know where you can get one.
@benjaminshinar9509
@benjaminshinar9509 4 жыл бұрын
@@JacobSorber first thing i did when opened the video was search for it. the page in the shop is down, currently :(
@amaia6393
@amaia6393 4 жыл бұрын
@@JacobSorber i found it. :-).nice that you can deliver to europe!!! Viel Erfolg mit dem online-shop! :-). You are one of my favourite serious technical youtubers. I wish i had had you as teacher many years ago at university!
@JacobSorber
@JacobSorber 4 жыл бұрын
@@amaia6393 Super! Danke schön.
@JacobSorber
@JacobSorber 4 жыл бұрын
@@benjaminshinar9509 Hmmm... what URL did it send you to? Was it this one? teespring.com/stores/jacob-sorbers-store
@90_98
@90_98 11 ай бұрын
Personally I think it's useful to learn how to read assembly to kind of get an idea of what compilers are doing with your C behind the scenes. But not really to write assembly. Because, come on. No one hand-writes assembly. Maybe it's marginally faster but 10x the effort of just writing C.
@abdelalkuor6293
@abdelalkuor6293 Жыл бұрын
why does including "nums.h" give a linker error "multiple definition of two"?
@matthewmoulton1
@matthewmoulton1 Жыл бұрын
You cannot have multiple definitions of an identical function signature or else it is ambiguous which one should be used. I cannot see your code (so I am just making an educated guess here) but I would guess that you forgot to make one definition inline like the video showed.
@DrOggy67
@DrOggy67 4 жыл бұрын
What do you think of the idea to write inline functions into the header files?
@OmarRampado87
@OmarRampado87 4 жыл бұрын
I wrote some code to test the behaviour and got errors for a while. The include of nums.h into the main file (test.c) breaks the compilation, both with gcc and clang. Do you know why?
@JacobSorber
@JacobSorber 4 жыл бұрын
Sadly, not without more information.
@OmarRampado87
@OmarRampado87 4 жыл бұрын
@@JacobSorber sure. Just a question: do you include nums.h into the test.c file? Thanks a lot
@JacobSorber
@JacobSorber 4 жыл бұрын
@@OmarRampado87 No, in the example, I did not include nums.h.
@darkstar4734
@darkstar4734 4 жыл бұрын
What is the editor that you use? I really need a good editor. Most just strain my eyes.
@JacobSorber
@JacobSorber 4 жыл бұрын
I'm currently using vscode.
@darkstar4734
@darkstar4734 4 жыл бұрын
@@JacobSorber oh. Thank you. Btw, your way of expression is the best. Even without crafty animations or drawing panels, it's all really easy to understand. I hope to develop coding and communication skills that are close to yours. Great job, professor.
@JacobSorber
@JacobSorber 4 жыл бұрын
@@darkstar4734 Thanks. Glad I could help.
@chriswaggoner4691
@chriswaggoner4691 3 жыл бұрын
So, I guess my takeaway is: don't use the inline keyword or optimization flags, because you don't know what will actually happen.
@tttakkkumi
@tttakkkumi 3 жыл бұрын
Could you explain what a translation unit is?
@sukivirus
@sukivirus 4 жыл бұрын
In C++ but I believe its applicable to C as well, when I use delete keyword to clear a pointer memory. Then is there a way to check if the content of memory has been erased. I try to check the pointer against being nullptr but it doesnt work. So after I delete I have to force the pointer to be equal to nullptr. I was thinking is there any method to check it without doing so?
@benjaminshinar9509
@benjaminshinar9509 4 жыл бұрын
I think that if you use shared_ptr and call .reset() it will take care of the issue and will no it's not pointing anywhere.
@benhetland576
@benhetland576 4 жыл бұрын
No, in both C and C++ you are by design expected to keep track of and be responsible for all pointer values yourself. It is up to you how you choose to achieve that. In C++ you are only supposed to use 'delete' and 'delete[]' with a ptr value that you got from 'new' and 'new[]' respectively, and in both languages calling 'free' with a ptr value not returned from a previous call to 'malloc/calloc/realloc' results in undefined behavior. Note that pointers may quite reasonably have other ptr values, such as to the middle of an array or to a local variable on the stack, but using these with 'free' is still erroneous. That said, some compilers will insert extra debugging or "intrumentation" code in your code if requested to, so that for instance the free function also will fill the memory with a predefined pattern, which then can be inspected from the debugger. This is only intended as a development help, though, and the final program code should never rely on such a feature. As a more reliable "hardening" feature of a commercial program it used to be quite common to wrap the alloc/free mechanism in your own set of such functions, tailored a bit more to the specific needs of that application. A simple alternative in C could be just #define FREE(p) (free(p), p=0) and then use FREE everywhere instead of free. Nowadays there is of course smart pointers as a good alternative in C++. One may also often consider that if one really needs to check if the pointer is still valid/allocated, then that would be a design flaw or "code smell", so should be fixed on a different level than trial and error with a debugger.
@smrtfasizmu6161
@smrtfasizmu6161 2 жыл бұрын
Your pointer doesn't become null after you free the memory, however the memory to which it was pointing to is returned back to the OS.
@lordadamson
@lordadamson 4 жыл бұрын
pretty good stuff (Y)
@shivam4428
@shivam4428 4 жыл бұрын
i am a beginner and a student . i want to learn IOT . IOT focused towards industry requirements rather than just playing with it. (not blinking LEDs on microcontroller but instead focusing on how can we create our own microcontroller ). so, in similar manner working on IOT,doing something that actully matter and we can innovate/produce somethting out of it .
@pauljackson3491
@pauljackson3491 3 жыл бұрын
Maybe inline functions could be used for something similar to a const of define. I.E. "inline float return_pi() {return 3.14;}"
@christopherjurich4472
@christopherjurich4472 4 жыл бұрын
Good video. I personally think that "inline" should be dropped from the standard since the optimizers usually deal with it how they please anyways.
@stevencolborne6845
@stevencolborne6845 11 ай бұрын
When optimizing for speed notsize inline avoids problems with using macros example creating an and macro is easy but dangerous related to arg evaluation. Inl8ne works better without function call overhead.
@sophialawrence7687
@sophialawrence7687 4 жыл бұрын
Anyone please clarify about Static inline function?
@JacobSorber
@JacobSorber 4 жыл бұрын
I recommend you check out my video (or some other resource) on static. All it's doing here is limiting the function to this specific translation unit.
@sophialawrence7687
@sophialawrence7687 4 жыл бұрын
@@JacobSorber Thanks a lot for your reply:-)static and inline both are clear...but in some functions it is used as static inline function_a....static inline (together used for one function) means?
@anindyamitra5091
@anindyamitra5091 3 жыл бұрын
The treble hurts my ear, can you lower the treble a bit, in your later videos please?
@JacobSorber
@JacobSorber 3 жыл бұрын
I've made a lot of adjustments since this video posted. Are my recent videos working better for you?
@anindyamitra5091
@anindyamitra5091 3 жыл бұрын
@@JacobSorber it's there, the "ss.." sound hits really sharp, other than that the sound is really clear, thank you for looking into the matter.
@makveli259
@makveli259 4 жыл бұрын
do you have a github?
@JacobSorber
@JacobSorber 4 жыл бұрын
I do have a github account, but you can find source code from my videos through Patreon.
@myma84
@myma84 4 жыл бұрын
Do the movie about stdatomic in C11
@wChris_
@wChris_ 4 жыл бұрын
Have you heard of Compiler Explorrer? I asume not.
@eternablue730
@eternablue730 2 жыл бұрын
why turbo synthax and not intel %%%%%%%%%
@datawolk
@datawolk 4 жыл бұрын
And C what it is
@zxuiji
@zxuiji 2 жыл бұрын
Okay, this was helpful, now I know, don't waste my time with inline, it's worthless
@thogameskanaal
@thogameskanaal 3 жыл бұрын
Please don't write source code where the code flow and functionality can differ based on optimization flag specified! You're basically shooting yourself in the foot by getting the compiler to do whatever it feels like that day, and a compiler doesn't have a soul, it should just always do what you tell it to do, precisely! Never have a static and a global (linked) function with the same name, unless you're following a strict naming specification! (And only then, make sure you declare every signature as static within the .c file, don't contaminate your headers with it.) That's called ambiguity and it's a HORRIBLY bad coding practice! I see no reason why you shouldn't add an affix to the symbol name, eliminating ambiguity and making clear in which context it is being used. Of course I'm not saying don't inline functions. It's a great optimization feature and you should want to use it. But only within the context of the translation unit! And use the O2 flag or higher to expect it to take effect, otherwise it'll turn the task of debugging into unneeded torture for yourself. Personally I stay away from any compiler specific pragma's and hints, since those are usually death sentence to maintainability. Imagine having to use a different compiler that isn't GCC or Clang, then everything breaks and you waste a lot of time working around that stuff. Just pick a C standard and make sure all platforms you intend to run your program on have a compiler available that accepts that C standard. Anyways make sure it'll link using just object files. The O2 optimized components should link perfectly fine with O0 optimized ones. No repeat code, no code difference at all apart from optimizations. Hope that wasn't too ranty, but I expected better
@samuelgunter
@samuelgunter Жыл бұрын
yikes that sounds hard to maintain
@samuelgunter
@samuelgunter 11 ай бұрын
somehow i have returned after over a year, and I agree with myself that yikes that sounds hare to maintain
@priyanshu_hiro
@priyanshu_hiro 4 жыл бұрын
compare the two codes: ------------------------------------------------------------------------------------------ //VERSION 1 int main(){ int n = 5; cout
@songxu7837
@songxu7837 10 ай бұрын
lost debug ability , that is not what I want
@Yarin5879
@Yarin5879 4 жыл бұрын
If C was a person it was you
@JacobSorber
@JacobSorber 4 жыл бұрын
So, C is your favorite language, right?
@Yarin5879
@Yarin5879 4 жыл бұрын
@@JacobSorber return true;
@dengan699
@dengan699 4 жыл бұрын
really, 16min video for "inline" ?..
@JacobSorber
@JacobSorber 4 жыл бұрын
Yeah, I had the same thought. But, one thing led to another, and... 16 minutes later, here we are.
What is a semaphore? How do they work? (Example in C)
13:27
Jacob Sorber
Рет қаралды 314 М.
How to Write Function-Like Preprocessor Macros (C example)
13:59
Jacob Sorber
Рет қаралды 44 М.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
When do I use a union in C or C++, instead of a struct?
11:18
Jacob Sorber
Рет қаралды 71 М.
How to Check Your Pointers at Runtime
14:12
Jacob Sorber
Рет қаралды 31 М.
Student programmers, ChatGPT is not your friend
13:46
Jacob Sorber
Рет қаралды 12 М.
Header Issues: Guards, Name Mangling, and extern "C"
8:32
Jacob Sorber
Рет қаралды 79 М.
Easy Networking in C (libcurl)
14:12
Jacob Sorber
Рет қаралды 67 М.
Defining Constants. Should I use CONST or #DEFINE?
7:59
Jacob Sorber
Рет қаралды 58 М.
How to Send and Receive UDP packets (in C)
23:20
Jacob Sorber
Рет қаралды 7 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 22 М.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.