4 ms·
C and C++ are completely different languages. The arbitrary parsing of data into structs in C is what makes it incredibly suitable for file parsing. Among other
by futharkshill 5y ago
C and C++ are completely different languages. The arbitrary parsing of data into structs in C is what makes it incredibly suitable for file parsing. Among other things.
- mrich 5y agoC is a subset of C++. The key point is that both languages suffer from the same problems, e.g. memory safety issues, undefined behavior, data races. The software industry has been dealing with the fallout of these issues for the past three decades. It's about time we moved the bar higher. By the way, the main data structure of my first large Rust program was simply a vector of a custom struct, optimized for memory footprint. Just like in C, with the same low overhead serialization. With C, you always have to be careful to not overrun a buffer. The recent NSS vulnerability is a great example of this: https://googleprojectzero.blogspot.com/2021/12/this-shouldnt-have-happened.html https://googleprojectzero.blogspot.com/2021/12/this-shouldnt... Even though this code was heavily fuzzed and tested, the problem slipped through. Not possible in Rust. Also, with Rust you can throw away all your sanitizer infrastructure and save all the associated costs.
- isitdopamine 5y ago> C is a subset of C++ It is not, they diverged since decades now. Still, even if this was the case, the programming styles are so different that there’s nothing to gain considering them as any more similar as any two other languages.
- ReleaseCandidat 5y ago> It is not, they diverged since decades now. AFAIK you always had to cast `void *` in C++ (like the return value of `malloc`).
- yaris 5y agoWhy on Earth would one use malloc in C++? Instead of using ’new’ if explicit allocation is desired.
- ReleaseCandidat 5y agoWe are talking about the question if C is a subset of C++. `new` certainly isn't a part of C, so also not an element of the intersection of C and C++. Idiomatic C code doesn't explicitly cast the return value of malloc foo *bar = malloc(sizeof *bar); C++ did AFAIK never (certainly not with C++98, the question is if it had been allowed sometime before the standardization, but I think it never did) allow this, so you always had to do foo *bar = (foo *)malloc(sizeof *bar); Therefore, C is not a subset of C++. But a (non-empty ;) intersection of C and C++ exists.
- ByteJockey 5y agoIf you need to change the buffer size afterwards. My understanding is that most implementations of new call malloc under the hood (this may or may not be outdated at this point, I haven't kept up with C++ implementation) and both of these systems introduce a layer of record keeping, so if you're in an extremely memory constrained environment, you may want to use malloc directly. If you want your code to be noexcept, you need to call malloc and handle the case where it returns null as new can throw (this is UB in theory, but in practice I'm pretty sure everything just aborts) to strip out all the stack-unwinding code. If you want to avoid the constructor call (for whatever reason).
- futharkshill 5y ago> Why on Earth would one use malloc in C++? Instead of using ’new’ if explicit allocation is desired. That was the point of my original comment that C++ and C are not the same language :)
- johnisgood 5y agoYeah. Someone gave me a file and told me to parse it. I used structs and fread. It was easy-peasy! I do not know of any other languages where what I wanted to do would have been just as easy as it was in C.
- futharkshill 5y agoIt is almost as if it was written with people doing a lot of IO and internet :D
- MonkeyClub 5y agoSockets are a later addition, but definitely wrt IO.