Jump to content
  • Advertisement
Sign in to follow this  
d h k

Getting size of std::string in bytes?

This topic is 3036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.)]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites

ID3DXBuffer *error_buffer = NULL;
std::string shader_code;

// set shader code to HLSL code

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

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!