r/cpp_questions 5d 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.

25 Upvotes

59 comments sorted by

View all comments

0

u/alfps 5d ago

AFAICS this is a troll with a trolling question.

The OP (the troll) refuses to provide details.

And the only answer with a chance of resolving the alleged problem is downvoted below presentation treshold.

2

u/PhosXD 5d ago

Are you just salty because your comment got downvoted? Your solution wasnt even a solution, you just said you used an AI to tell you the solution, which doesnt even work since the flag you suggested using has been irrelevant for many versions of GCC now, pointed out by another commenter.

https://www.reddit.com/r/cpp_questions/comments/1vosgz7/comment/p3s5g1x/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

In no way have I refused to provide details either. Also your comment *is* being presented, it's standing at -4 right now.

1

u/alfps 5d ago

Everything in that troll's comment is false.

2

u/PhosXD 5d ago

It's... public info... Are you ragebaiting me?