3

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..

3
  • If you want to display a double-anti-slash, select the part containing it an define it as code. Commented Oct 7, 2008 at 9:36
  • Just to check, can you first assign the string to a const char *, and then pass that to the string constructor? Commented Oct 7, 2008 at 10:20
  • Meeh: Could you edit your title? the problem has nothing to do with strings or backslashes Commented Oct 7, 2008 at 12:31

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

Glad you found the problem. But, I don't understand what you meant by "throw a bad_alloc exception ... exactly on the 4th char of the string". That makes no sense to me.
I agree.. Doesnt make sense to me either. Actually I think I was a bit vargue in that point :) If I did a std::string path = "C:\\Test.xml", a bad_alloc would be thrown. If I did a path = "C:Test.xml", no bad_alloc would be thrown. So don't ask my why it did what it did :P
0

Define the string as: "C:\\Path.pth"

Comments

0

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");

Comments

0

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)

Comments

0

This could simply be due to a clean build being required; it was certainly the case when I experienced this.

Comments

-1

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:

  • Turn off UAC, or
  • Run your application as "Administrator", or
  • Write into a subdirectory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.