We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Fred Overflow · 2.2K views · 74 likes
Analysis Summary
Worth Noting
Positive elements
- This video provides a very clear, line-by-line technical breakdown of how RAII (Resource Acquisition Is Initialization) prevents common memory errors like leaks and use-after-free.
Be Aware
Cautionary elements
- The comparison uses a somewhat 'straw man' version of C (fixed-size arrays) to make the C++ version appear infinitely more capable, rather than comparing two equally optimized production-grade programs.
Influence Dimensions
How are these scored?About this analysis
Knowing about these techniques makes them visible, not powerless. The ones that work best on you are the ones that match beliefs you already hold.
This analysis is a tool for your own thinking — what you do with it is up to you.
Related content covering similar topics.
Storing state in O(1) space? | Leetcode With Vim
nycrat
⚡ Learning C (1)
RWXROB
Writing Unit Tests in C???
teej_daily
Prot Asks: Ro about programmatic thinking, political discourse, and art as self-discovery
Protesilaos Stavrou
Zig structs are cracked
Melkey
Transcript
unix has a program called sort that can sort the content of a text file by redirecting it to standard in so for example here is the text file just some paragraph from a novel and if we sort that then the lines are basically sorted so first the uppercase characters f and t and then the lowercase characters from a to w okay today i want to look at two implementations of this program in c plus and c to compare them okay so let's start with the c plus plus version um here we have a vector of string for all our lines because we need to read them into memory because before we can sort them then a string for a single line the current line that we want to read in and then we call the getline function to say we want to get from standard in the current line and while that works the line is pushed back into the vector okay then all the lines are in memory then we can sort them by passing the begin and end iterator into the standard sort algorithm and finally with the for each loop for each line and lines we print the lines to the terminal okay cool now let's compare that with the c version let me split that to the right so we can see both versions okay so of course c doesn't have vectors or strings here i just use a fixed size array of char pointers so my program can only handle at most 10 000 lines okay and how many of those 10 000 lines are used so far initially of course that is zero okay then yeah we don't have strings so let's just use a thousand characters hopefully no line will be uh longer than that and then the while loop looks rather similar we call f gets this is the buffer where we don't want to store this is the size of the buffer we have to pass that explicitly because arrays decay to point us when we pass them to functions and finally we want to read from standard in okay and then unfortunately fgets will store the new line character at the end of the string if there is one so we check for that and then replace that with the null terminator and you may think why why do i even check for that if that always should always happen well if we have lines longer than a thousand characters let's say a thousand five hundred then in one iteration we will get thousand lines without um the new line and then in the next iteration we will get five hundred characters um with a new line okay and then finally we have to yeah push the line back into lines how do we do that we allocate enough memory for the payload and the null terminator store that at the next available slot and then increment used okay yeah and then we copy the line into that quite a compact line maybe you would split that into two lines in the real world okay then we have that in memory how would we sort um we call the q sort function it expects the array the number of elements to be sorted and the size of all the elements which are char pointers in our case okay and then a custom compare function which always looks like this it takes two const void pointers to two elements in the array and we have to decide via negative zero or positive number if the first element is less than um equal to or greater than the second element okay and finally instead of for each we have a classic counting loop so all the numbers less than used and then we print the current line and then we free it because every malloc call that happens at runtime must be matched by a free call happening at runtime otherwise we get a memory leak okay since the program ends after this loop maybe it wouldn't be so critical here to actually perform the the free but in longer running programs memory leaks are a big issue okay so does this actually work maybe let's try that i have my sword c here with para yeah that seems to give us the exact same output and instead of a paragraph let's sort an entire book then we get a segmentation for it because the book has more than ten thousand lines does it work with the c plus version yeah that works so these quoting characters seem to come quite late in our character set uh but here we have from o to y and then it starts again at b because these quoting characters are obviously different right unicode with all its quoting characters okay what would happen if the free call comes before the printf um that is undefined behavior you can't use lines i after freeing it but of course we can try so i prepared that as well that would be dangling and let's do it with the para so it's more obvious then we get some normal lines uh interspersed with lots of garbage and if we do it again that we can then we get different garbage so it's non-deterministic what happens if you use memory after freeing it that's called a use after free bug okay and interestingly in the c plus plus version we have we have neither malloc nor free so the dynamic memory is managed by string and by vector and when these variables go out of scope the memory is released automatically that's arguably the most important feature of c plus plus has nothing to do with garbage collection is completely deterministic and if we wanted to release the dynamic memory handled by line sooner because after the while we don't really need this variable anymore we could simply introduce another scope here like this and then for beauty let's indent it and then the line destructor would run here releasing the managed memory so i think that's pretty cool doesn't only work for memory also works for file handles and database connections and such again probably the most important c plus plus feature okay um oh yes i didn't show you the string compare yet so how does that work string compare text to construct pointers to two elements and we can't what we actually want to do is delegate to still comp but still comp expects the char pointers themselves not pointers to them and that's why we have to cast the construct pointers to construct our pointer const pointer and d reference to get one step closer to the actual um construct pointers so this is very highly idiomatic c chord you would never write something like this in c plus plus but you could the compiler would accept it but probably not your senior during a code review okay so um i definitely don't want to hate on c i just want to show you c plus code tends to be more high level more stuff managed for you behind the back and where c chord tends to be more low level every step is very explicit the memory management is explicit and you will never get magic at closing braces or something like this or let's say magic object copies for example in the c plus plus code if you remove the ampersands here then line will no longer be the original inside lines but a copy and maybe that's not obvious to a c programmer that this will perform a copy so every language has its pros and cons of course