Getting size of std::string in bytes?

Started by
12 comments, last by iMalc 14 years ago
Hey there, how can I retrieve the size of a std::string in bytes? There seems to be no such function in the STL and, for whatever reason, Google doesn't seem to yield any useful search results either (in fact, the phrase "size of std::string in bytes" doesn't get a single hit). In my case, my engine creates a basic shader at run-time for drawing objects which have no custom shader assigned to them by the user. I have a std::string containing the shader code and try to pass it to D3DXCreateEffect ( ) in D3D9. The function requires an old-school C string as first argument (which is my std::vector.c_str ( )) and then the size of that thing in bytes (which is my, well, problem). So, how would that work? I'm really surprised to see that this apparently hasn't come up frequently before as it seems like a pretty common thing you wanna do with std::strings. Thanks ahead of time.
Advertisement
Are you looking for size/length()?

[Edit: Whether that corresponds to the size in bytes depends on the character type, but in the given context, I imagine that these are the functions you're looking for. (I've used size() successfully in the same context.)]
c++ reference is your friend.
std::string::length() will probably work, but if you have extraneous null terminators in there, you may need to use std::string::size().
Remember to add 1 to length() if you care about the null terminator.
Hm, it doesn't seem to work. That was the first thing I tried actually. :D

If I, for the length of the string, just enter some number like 10, it works without crashing (although the shader doesn't compile of course as that creates an unexpected eof) but with string.size ( ) it crashes.

This is the documentation of the function BTW (as it seems the problem might be related to the function):

D3DXCreateEffect

EDIT: Oh, just realized that a byte is exactly what you need to describe one character, doh. :D That clears up a bunch of things in my head. EXCEPT why it crashes my function.
Quote:Original post by d h k
Hm, it doesn't seem to work.


It does :)

More information is needed. Show us how/where you're using string::size().
What are you trying to do with the std::string? Serializing it?
ID3DXBuffer *error_buffer = NULL;std::string shader_code;// set shader code to HLSL codeD3DXCreateEffect ( device, shader_code.c_str ( ), static_cast<unsigned int> ( shader_code.size ( ) ), NULL, NULL, SHADER_COMPILE_FLAGS, NULL, &effect, &error_buffer );


That's it and, as I said, the problem is the third parameter there, if I put in an arbitrary number the function won't crash. But like that there above it does. It also does not fail in any case BTW.

Thanks so far.
Quote:std::string::length() will probably work, but if you have extraneous null terminators in there, you may need to use std::string::size().
Are you sure about that? I always thought that size() and length() were equivalent. (MSDN seems to substantiate this, FWIW.)
Quote:What are you trying to do with the std::string? Serializing it?
The OP explained in his post what he's trying to do (he's trying to create a shader in Direct3D from text stored in a string object).
Try adding 1 to the size before you pass it in.

Save the result of string.size() to a temporary variable and check the value first, see if it makes sense with the behavior you're seeing.

This topic is closed to new replies.

Advertisement