Code Review #2: Generalizing and D.R.Y.ing things up.

  Рет қаралды 7,580

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер: 46
@csbnikhil
@csbnikhil 5 жыл бұрын
Thank you so much for the review! In order to keep everything symmetric, we could make two function calls, for every sorting algorithm, by declaring an internal function for bubble, insertion and selection sorts, just like you did for merge and quick sorts, so that the comparison is more accurate, although more erroneous in case of absolute timing of the functions.
@JacobSorber
@JacobSorber 5 жыл бұрын
Very true. Try it out and let me know if it makes a meaningful difference in your results.
@csbnikhil
@csbnikhil 5 жыл бұрын
​@@JacobSorber Sure! I'd like to put out another observation I made, about you explicitly initializing `copy` to `NULL`. It's your flavor to initialize your vars, but I'd just like to say that all `static` vars are initialized by the compiler to their equivalent 0's, which in my case, for a pointer, should be `NULL`. I did write a simple test program and ran it for enough number of times to come to a conclusion that `static` pointers will be initialized to `NULL`.
@JacobSorber
@JacobSorber 5 жыл бұрын
@@csbnikhil You're right. I prefer explicit initialization, but that part of the code worked fine.
@rohithill
@rohithill 5 жыл бұрын
*Today seems like a good day for a code review.*
@malbolgee
@malbolgee 4 жыл бұрын
wow, new thing learned, #NAME to return a string of the input, did not know that, pretty neat, thanks!
@aluminiumjay9271
@aluminiumjay9271 4 жыл бұрын
Hi Jacob. I've learned a ton from your videos. In my humble opinion your videos on C are top notch. I also love the usual way of your teaching of showing why things don't work first and then going to the solution. If I may ask please do a series on Linux kernel data structures. There is next to nothing on that topic so far on KZbin and i think with your OS knowledge it will be awesome. Thanks for all the hard work so far and for you kind consideration.
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks. I'm glad the channel is helping. I've thought about doing in-kernel stuff for a while. It's a great topic, but those examples can also be pretty time consuming, and time seems to always be my #1 issue. It's on the list. I'll see what I can do.
@aluminiumjay9271
@aluminiumjay9271 4 жыл бұрын
@@JacobSorber thanks heaps for the reply Jacob. Really appreciated.
@xpdseth
@xpdseth 4 жыл бұрын
did we miss a free with the last malloc ?
@hamed9327
@hamed9327 3 жыл бұрын
wow man this is smooth
@markoveniger4533
@markoveniger4533 4 жыл бұрын
I think that the memcopy function should take the size of the array in bytes and not the length of the array, so size*sizeof(int)
@cult.of.o
@cult.of.o 5 жыл бұрын
You may want to declare 'internal' functions as static, so they wouldn't be visible outside of their file.
@JacobSorber
@JacobSorber 5 жыл бұрын
Agreed, especially if we were packaging this as a code library or module that was going to be part of a larger project. Thanks.
@fusca14tube
@fusca14tube 4 жыл бұрын
Hi... at 8:35, you declare an int variable "int NUM_FUNCTIONS = sizeof(sortingfuncs) / sizeof(sort_func_info_t)". Can you use "#define NUM_FUNCTIONS sizeof(sortingfuncs) / sizeof(sort_func_info_t)" instead of this int variable? Tks.
@HansBezemer
@HansBezemer 3 жыл бұрын
A terminator works fine as well.
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
As Jacob addressed in another video, this really should have been "const int ...." The value of this token does not change during execution. As a result, the compiler will likely just use the value where needed in the instruction space of the executable and no 'data' space will be consumed. Your #define (pre-processor) macro has the same ultimate effect. I tend to use the "const int numXXX = " version immediately below the definition of the variables initialising an array. (Often this is only within the scope of a function, so the symbol used is not in the global namespace for the entire compilation unit.) Because things CAN and do change, I prefer naming the array twice instead of specifying (sometimes wrongly) the datatype of the elements of the array: const int numElem = sizeof( arr )/sizeof( arr[0] ); Changing the array type without changing the token in the divisor is a gateway to a bug. The compiler is far more careful than any human about these things.
@muji_dipto
@muji_dipto 5 жыл бұрын
Also a couple more questions, can you make a video on the usage of function pointers like you used on this one? I sort of understand how they work but never really used them for anything. & Do you think "Compilers and Interpreters" is a good/necessary course to take for software engineers?
@JacobSorber
@JacobSorber 5 жыл бұрын
I made one a while ago: kzbin.info/www/bejne/l6nRmKp6f5xsfbs Let me know if it doesn't clear things up or if you think there's something still missing.
@benjaminshinar9509
@benjaminshinar9509 5 жыл бұрын
nice video! it's feels really weird seeing struct initialization in C code.
@JacobSorber
@JacobSorber 5 жыл бұрын
Thanks. If you use it more often, the feeling will wear off.
@muji_dipto
@muji_dipto 5 жыл бұрын
can you show your vscode settings in a video? looks pretty neat
@JacobSorber
@JacobSorber 5 жыл бұрын
I think I'm just using the defaults. Not sure. What are you seeing that's different?
@muji_dipto
@muji_dipto 5 жыл бұрын
@@JacobSorber the arrows for the lines & the dots for indents
@JacobSorber
@JacobSorber 5 жыл бұрын
@@muji_dipto Ah, right. In Preferences, you can turn "Render Whitespace" to "all" and it will turn that on. It's super handy, especially if you do any python programming.
@muji_dipto
@muji_dipto 5 жыл бұрын
@@JacobSorber thanks a lot ❤️
@MM-doremifaso
@MM-doremifaso 5 жыл бұрын
What keyboard are you using? Is it an apple keyboard?
@clarkgray4109
@clarkgray4109 5 жыл бұрын
M Maeusebuss sounds like the butterfly switches on the MacBook pros
@JacobSorber
@JacobSorber 5 жыл бұрын
Good ear. :)
@HansBezemer
@HansBezemer 3 жыл бұрын
OMG! Are people actually WRITING this kind of code? When I saw this, my immediate thought was "This needs to be a table with function pointers". Tables are so easy to maintain. The code already works, so all you need to do is plug in a new tuple - and viola! I wrote entire compilers and interpreters on tables. And it's so easy to maintain, because very little code is affected - which makes debugging and testing a whole lot easier. And tables - when properly laid out - allow for VERY fast access (in my experience only switches and GCC goto's are faster). Binary searches already give you O(log n). If you manage to squeeze an index out of them subsequent accesses are O(1). What's not to love? And yes - a single function call is negligible. And a no-brainer where simplicity and maintainability is involved. I was a bit scared about the table initialization, but you caught up pretty nicely ;-) Just const and static that one IRL.
@TomStorey96
@TomStorey96 3 жыл бұрын
Not everyone has a CS degree or years of experience behind them when they start out to know there are better ways to do things. I can draw parallels to this code when I started programming. My earliest code was very much cringe worthy when I look at what I can write today. But I started when I was in high school and only in my early teens. 20+ years of hindsight does that.
@rajashreedeka652
@rajashreedeka652 5 жыл бұрын
can you please tell us how C utilizes graphic library created by third party when it does not have anything in its standard to access graphics ? Please make a video on C GUI programming. Thanks for your videos. You are very helpful.
@JacobSorber
@JacobSorber 5 жыл бұрын
Do you have a specific graphic or GUI library that you're interested in?
@sukivirus
@sukivirus 5 жыл бұрын
@@JacobSorber I think only GTK+ is the most advanced gui library for C language. Is there any other library which is popular as a GUI for C language?
@JacobSorber
@JacobSorber 5 жыл бұрын
It really depends on what you are looking for. A lot of GUI frameworks are object-oriented. So, they tend to favor C++ rather than C (or other languages). For cross-platform development, you could look into Qt, wxWidgets. Native APIs (like the standard Windows API) often allow GUI development in C. The low-level graphics APIs that the GUI frameworks use are all C accessible.
@sukivirus
@sukivirus 5 жыл бұрын
@@JacobSorber I started learning C back in college but later on picked up C++ for its OOP nature. I mostly do scientific programming . Even though I like C, the language seems limited and C++ is ugly if you look outside standard library. :) How does one language access graphic output if it does not have a std library function associated with it. For example we have in C which takes care all the input from keyboard and printing on the screen. But for any other data presented on the screen, how does one achieve it? Thanks
@wolverine9632
@wolverine9632 2 жыл бұрын
Honestly, I thought Jacob was a deepfake in this at first. But when I heard him speak, without taking a breath, I knew this was the real deal.
@JacobSorber
@JacobSorber 2 жыл бұрын
😂
@laibamustafa108
@laibamustafa108 4 жыл бұрын
Pls make java videos, or OO in general
@JacobSorber
@JacobSorber 4 жыл бұрын
What sort of OO topics would you like to see?
@TheBigWazowski
@TheBigWazowski 4 жыл бұрын
Maybe examples of OO design principles like SOLID or GRASP?
@diwakar8815
@diwakar8815 5 жыл бұрын
hi jacob!!i have asked about 8086 processor complete guide.Do you know any resources that help me learn it.I have an entrance exam with syllabus being 8086 processor.
@JacobSorber
@JacobSorber 5 жыл бұрын
I really don't. My x86 ISA experience is pretty limited, and I have only used random online resources and Intel's ISA manual.
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
Worth noting that both of you have mixed-up SIZE and array_size in a most unhealthy fashion... So much for using generic tokens. This code deals with int's, so rename SIZE as num_ints and array_size as array_bytes. You won't accidentally copy 12 bytes when you mean to copy 12 * sizeof(int). A more interesting problem is defining data in a header file. Beginners need to be taught the proper use of header files (ie: they are for #define tokens and function prototypes only.) You ended the video without mentioning that the timing for each sort technique should be an element of your array of structures, too... When another sort algorithm is added, it will want a place to store its running time, too.
@shekharmaela2308
@shekharmaela2308 5 жыл бұрын
DO NOT append _t to your types, it's a standard library thing only.
@JacobSorber
@JacobSorber 5 жыл бұрын
Interesting. I never realized that POSIX claimed that as reserved. Duly noted. Thanks.
How to Blink an LED in C (avr, msp430)
16:37
Jacob Sorber
Рет қаралды 35 М.
Reviewing your Code: Refactoring
16:21
Jacob Sorber
Рет қаралды 31 М.
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
How to Check Your Pointers at Runtime
14:12
Jacob Sorber
Рет қаралды 31 М.
Allocator
7:31
C++ Data Structures
Рет қаралды 131
When do I use a union in C or C++, instead of a struct?
11:18
Jacob Sorber
Рет қаралды 71 М.
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 73 М.
Why Your Brain Sabotages Your Goals (and How to Fix It)
11:56
Productive Peter
Рет қаралды 38 М.
How to Intercept and Modify Library Calls with Shims.
10:14
Jacob Sorber
Рет қаралды 19 М.
Understanding and implementing a Hash Table (in C)
24:54
Jacob Sorber
Рет қаралды 370 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 22 М.
Header Issues: Guards, Name Mangling, and extern "C"
8:32
Jacob Sorber
Рет қаралды 79 М.