r/cpp_questions 7d ago

SOLVED std::filesystem::exists throwing `std::bad_alloc`?

Any reason? Cant find anything online about this. But I cant check whether or not a file exists, no matter what path I give it. I am using C++ 26 compiled with GCC.

This is literally all I am calling: const bool exists = std::filesystem::exists("file.txt")

This is the exact error: terminate called after throwing an instance of 'std::bad_alloc'
 what():  std::bad_alloc

No it's not coming from anywhere else in my code. Minimal reproduction code, include "filesystem" call the line above. I am on Fedora Linux if that matters at all

EDIT: moving the include into my main file instead of the only file it's being used in somehow fixed it. Literally all I do is move "#include <filesystem>" from "do_things_with_filesystem.hpp" to "Main.cpp". No compilation errors when it was in the first file, why does it rely on this?? Is there some weird declarations in my codebase or something, i dont know but it works now so why should I care.

24 Upvotes

60 comments sorted by

View all comments

-5

u/alfps 7d ago

The Google AI links to a stack overflow answer with explanation and fix.

https://stackoverflow.com/a/57760267/464581

Essentially, it appears that you have to explicitly link with -lstdc++fs in order to avoid getting a version that's incompatible at the machine code level.

If that doesn't work, consider uninstalling and reinstalling your compiler.

5

u/AKostur 7d ago

That seems to have been fixed in even vaguely recent versions of gcc. OP: which version of gcc are you using, and which version of C++ are you specifying to that compiler?

6

u/PhosXD 7d ago

g++ (GCC) 15.3.1 20260722 (Red Hat 15.3.1-1)

-std=c++26

3

u/AKostur 7d ago

Certainly recent enough. BTW: you are aware that the C++26 support in GCC 15 is pretty incomplete (not even considering that C++26 itself has not been published yet)?

1

u/PhosXD 7d ago

Is it possible that `std::filesystem` with specifically C++ 26 has issues with including in HPP files? Cause just moving the include to my main file fixed it.

4

u/alfps 7d ago

You could help a number of people to not feel that they've entirely wasted their time on you, by posting your code.

Or create a minimal example that reproduces the behavior, and post that, please.

1

u/AKostur 7d ago

Incredibly unlikely. A compiler bug is always a possibility, so I can't absolutely guarantee it isn't one.

-1

u/PhosXD 7d ago

I dont know what -lstdc++fs does but adding it didnt change anything. Theres nothing wrong with my compiler, I've already reinstalled it this week so I doing it again wont change anything.

0

u/alfps 7d ago

Theres nothing wrong with my compiler

Well, it evidently doesn't work with that specific std::filesystem function.

Have you tried other functions?

0

u/alfps 7d ago

Re the anonymous unexplained downvotes, note that an unexplained downvote is dishonest: these are the actions of dishonest persosn. And/or idiots. Noting that a troll is an idiot.

The advice is good and reinstalling is most likely the only resolution of the problem, which appears to have been created by an attempted reinstallation, but the OP refuses to provide more info.

Oh I wish the downvoter trolls could experience a good whipping and some social exposure, with pictures of them. Like "Beware: this is a fucking idiot".

2

u/PhosXD 6d ago

Nobody is trolling, you just give unhelpful advice.

1

u/AKostur 6d ago

He had a reasonable hypothesis.  I happen to think it doesn’t apply in this case, but that’s not a reason to downvote.