4 ms·
Would anyone realistically write this C code though? The second point (the initial total amount being 0) is something that anyone paying even a cursory glance a
by fao_ 9y ago
Would anyone realistically write this C code though? The second point (the initial total amount being 0) is something that anyone paying even a cursory glance at the source code could pick up on, as were many of the others (who the hell gets the address of stack memory and returns it? This is C 101).
If this was found-in-the-wild C then I wouldn't be bothered with it, but this is completely contrived.
- seba_dos1 9y ago> who the hell gets the address of stack memory and returns it? Most of beginners out there, who then cannot figure out the reason for their crashes, especially when it's not explicitly returned as the value of the function ;)
- Const-me 9y agoNeither of these two languages are exceptionally friendly towards beginners. Python, Ruby, Java/C#, Go are IMO much better for them.
- macintux 9y agoBut eventually if someone turns to C no amount of Python experience will prepare them for this category of memory problems, so they still qualify as a “beginner” for this discussion.
- Const-me 9y ago> no amount of Python experience will prepare them for this category of memory problems General programming experience helps a lot. Someone coming to C from a higher level language likely knows to read compiler output and warnings, use debugger, read diagnostic output. With modern C toolset, that experience will help solve 99% of such errors. OTOH, someone coming to Rust encounters set of problems no amount of programming experience help to solve. IMO for beginners, Rust’s learning curve is dangerously close to that of pure functional languages.
- danieldk 9y agoNo experienced C programmer will write this code, but years of buffer overflow/double free CVEs show that even experienced programmers make such errors occasionally. And one error can be enough for a system compromise. Of course, this is not only an argument in favor of in Rust, but any memory-safe language. Rust just happens to address some of the same problem domains that C and C++ have traditionally been dominant in.
- viperscape 9y agoWhy is double free so commonly an issue? Isn't setting to Null after free and checking for Null before hand a common practice?
- sidlls 9y agoIt isn't that common. On HN, however, you'd think there isn't a piece of software written in C that isn't responsible for mayhem and death. The sort of FUD around the language, driven helpfully by the Rust Evangelism Strikeforce, isn't helpful.
- tene 9y agoIt's usually not calling free() on the same variable within a single function, although that can sometimes happen. The most common case, I expect, is when two separate data structures both have pointers to the same object and believe they own it, then later end up calling free on the same address at different times.
- gnuvince 9y ago> who the hell gets the address of stack memory and returns it? This is C 101 John Carmack, apparently: https://twitter.com/ID_AA_Carmack/status/587077680652230656 https://twitter.com/ID_AA_Carmack/status/587077680652230656
- fao_ 9y agoThat's... unsettling.