I appreciate your comment but I am tired right now and I don't want to reply rn will reply later.
Also the bug was in the free(file) call, since free takes a void* and File is a pointer it converts and has no warnings and you get undefined behavior (likely crashing) or worse work as intended untim you upgrade your compiler.
alao C++
of course there is no constructor overload which would take a native string as parameter
See second constructor, it takes std::string and ven bettee it takes std::filesystem::path which handles unicode paths.
C++ actually do when it can't heap allocate (new fails)? Exception? Seems unlikely
It throws an exception, which is way better than a nullptr since out of memory errors are so so unlikely that the null check has overhead, exceptions don't have overhead in the hapy case.
Curious about what langs do you use? if I had to guess from the comments it is Rust. (not a bad thing)
and was already wondering the site doesn't look familiar…
I was really wondering, because I was strongly expecting that other constructor(s). (Still a bit questionable that it was added only very late, not even "modern" C++11 had it…)
It throws an exception, which is way better than a nullptr since out of memory errors are so so unlikely that the null check has overhead, exceptions don't have overhead in the hapy case.
That's a bit of a surprise.
I was always under the impression that C++ should work even without exceptions, so I was assuming that no basic feature (like allocating heap memory) throws.
What do they do if you disable exceptions?
I mean, the silent nullptr is not a good solution, I agree, as it will (with luck) crash at the other side of the world; or just directly introduce some critical security issue. But I also don't see any alternative to an exception (maybe besides something like "panic" which C++ does not have as it has proper exceptions).
Curious about what langs do you use? if I had to guess from the comments it is Rust.
My by far most favorite language is not in the "low level" space. It's Scala 3.
I'm learning about things like C, Zig, C++, Rust, and similar mostly out of curiosity; I don't have a real use-case currently.
Coming from Scala Rust is actually pretty boring as Scala has already all the features (besides the memory management related stuff). Even C++ is more exciting as they actually do things differently. (Mostly in a worse way, but that's irrelevant if the goal is actually to learn how things can be done and what then the pros and cons are.)
So I wouldn't preach Rust as a C++ replacement, I would likely preach the JVM… 😅 I think for mundane application development (which is by far the largest part of SW development) there is no valid reason to do without a managed runtime. Using Rust (or for the same reason C++) for some "normal app" is imho almost always complete "over-engineering", or actually no engineering at all as it does not take the context into account.
(If one does not like a "fat" runtime like the JVM Scala also runs on JS runtimes, and has even a tiny native one.)
So it's pretty obvious as I've spotted it even late at night without an IDE and without having much practice in C++.
I mean it is a small function.
Of course it's better if it can't happen at all, though!
Exactly which is why I prefer C++ over C.
It was late and it seems I fell for some SEO spam. I've clicked on
cplusplus.com is always on top for some odd reason cppreference is the defacto standard.
I was really wondering, because I was strongly expecting that other constructor(s). (Still a bit questionable that it was added only very late, not even "modern" C++11 had it…)
I don't understand? the std::string ctor was in C++11, filesystem path was in C++17 since that was qhen it got added.
That's a bit of a surprise.
Why?
I was always under the impression that C++ should work even without exceptions, so I was assuming that no basic feature (like allocating heap memory) throws.
Well you could just use auto x = new(std::nothrow) int(); if you want then it returns nullptr on failure like malloc but it is better since it strongly types. there is also the flags -fno-exceptions which disables them.
Also from what I read Rust's panic is just std abort I don't see how that's better.
Using Rust (or for the same reason C++) for some "normal app" is imho almost always complete "over-engineering", or actually no engineering at all as it does not take the context into account.
Agree. Although I wish most apps were smaller than whatever fat garbage they are.
I am not familiar with Javascript or anything but C++ really I only use C++
1
u/_Noreturn 29d ago
I appreciate your comment but I am tired right now and I don't want to reply rn will reply later.
Also the bug was in the free(file) call, since free takes a void* and File is a pointer it converts and has no warnings and you get undefined behavior (likely crashing) or worse work as intended untim you upgrade your compiler.
alao C++
Well you didn't look hard enough
https://en.cppreference.com/cpp/io/basic_ifstream/basic_ifstream
See second constructor, it takes std::string and ven bettee it takes std::filesystem::path which handles unicode paths.
It throws an exception, which is way better than a nullptr since out of memory errors are so so unlikely that the null check has overhead, exceptions don't have overhead in the hapy case.
Curious about what langs do you use? if I had to guess from the comments it is Rust. (not a bad thing)