Reputation: 473
I'm currently working on a project that depends on me providing a path to a file (eg. C:\Path.pth
). Now, I had everything working yesterday by calling my std::string
with:
std::string path(`"C:\\Path.pth`");
But now it doesn't work. It throws a bad_alloc
. Seems like the '\
' character is the problem. I even tried using \x5C
as the ascii-value of it instead, but same result.
Now, my question is, is it possible that I have screwed up some #define
, some compiler-option or something else "non-code" that could've caused this? I'm using VS 2005.
Any help would be much appreciated
PierreBdR
.. That sounds very likely. Or at least, it have to :P
Since no one have mentioned some kind of /SetStringCharSize:2bit-compiler option, I think it's safe to assume that my code has to mess something up, somewhere, and that it's not just a silly compiler-option (or similar) that's wrong..
Upvotes: 3
Views: 13816
Reputation: 2573
This could simply be due to a clean build being required; it was certainly the case when I experienced this.
Upvotes: 0
Reputation: 473
The bug has been found and fixed.
Seems like TinyXML was has a bug when used with the TIXML_USE_STL
definition. So for some reason the constructor to the TiDocument
corrupted my memory-layout so badly that the next std::string
I defined has to throw a bad_alloc
exception - and luckily for me, exactly on the 4th char of the string, which in my situation was '\', resulting in a rather subtle error.
Upvotes: 3
Reputation: 16718
Don't forget, you should use forward slashes in paths, even on windows:
[15.16] Why can't I open a file in a different directory such as "..\test.dat"?
Because "\t" is a tab character.
You should use forward slashes in your filenames, even on operating systems that use backslashes (DOS, Windows, OS/2, etc.). For example:
#include <iostream>
#include <fstream>
int main()
{
#if 1
std::ifstream file("../test.dat"); // RIGHT!
#else
std::ifstream file("..\test.dat"); // WRONG!
#endif
...
}
Remember, the backslash ("\") is used in string literals to create special characters: "\n" is a newline, "\b" is a backspace, and "\t" is a tab, "\a" is an "alert", "\v" is a vertical-tab, etc. Therefore the file name "\version\next\alpha\beta\test.dat" is interpreted as a bunch of very funny characters. To be safe, use "/version/next/alpha/beta/test.dat" instead, even on systems that use a "\" as the directory separator. This is because the library routines on these operating systems handle "/" and "\" interchangeably.
Of course you could use "\\version\\next\\alpha\\beta\\test.dat", but that might hurt you (there's a non-zero chance you'll forget one of the "\"s, a rather subtle bug since most people don't notice it) and it can't help you (there's no benefit for using "\" over "/"). Besides "/" is more portable since it works on all flavors of Unix, Plan 9, Inferno, all Windows, OS/2, etc., but "\" works only on a subset of that list. So "\" costs you something and gains you nothing: use "/" instead.
(From C++ FAQ Lite)
Upvotes: 0
Reputation: 43234
As your error suggest, the problem is due to memory allocation (i.e. the bad_alloc exception).
So either you have no more memory (unlikely) or you have a buffer overrun somewhere before (quite likely in my opinion) or some other memory issues like double free.
In short, you do something that messes up the memory management layout (i.e. all these information in between allocated blocks). Check on what happens before this call.
Upvotes: 7
Reputation: 68023
Assuming your double-backslash is correct, I'd guess you're running on Vista?
Vista won't let your write into the root directory of the C drive by default. Try one of the following:
Upvotes: -1
Reputation: 216
No, you didn't have it "working" yesterday either. '\' needs to be escaped like so:
std::string path("c:\\path.pth");
You probably did a forward slash yesterday, which can work in this situation as well.
std::string path("c:/path.pth");
Upvotes: 0