Sign in to follow this  
d h k

Getting size of std::string in bytes?

Recommended Posts

d h k    439
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
jyk    2094
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
d h k    439
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
the_edd    2109
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
d h k    439

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
jyk    2094
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
jyk    2094
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
cache_hit    614
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
Beyond_Repair    116
Quote:
Original post by jyk
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).


Whoops. A case of partial thread reading I'm afraid. :)

Only things I can think of for now that haven't been mentioned already:

To try for string corruption: See if you can copy it and get a legitimate equal string as a result. (Print it to verify.) Does the printed string still crash if not equal?

Try to find out at precisely what range of values for the third param that function crashes with your given string input. You mentioned it works for an arbitrary number other than shader_code.size(): Is this exactly all numbers up to but not including that length (do you think)? The idea would be to hope to observe some pattern in what crashes based on string length rather than arbitrariness. This assumes you can shorten the input string down and it keeps crashing with string.size(), as I guess your shader code has uncomfortably many characters.

These things (esp. the 2nd) aren't likely to help find the problem, only try them if you're completely out of ideas and when you have tried what already was suggested above posts.

At this point is overall still sounds like corrupted string because a bugged common D3DX function is improbable.

[Edited by - Beyond_Repair on March 29, 2010 2:58:06 AM]

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by d h k
*** Source Snippet Removed ***

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.
So did you try assigning the result of size() or length() to a separate variable first and then passing that variable in instead?
That'll tell you if you've corrupted the string earlier such that it returns a ridiculous size.

Share this post


Link to post
Share on other sites
d h k    439
Thanks for the feedback.

Today I was able to run some more tests:

- The contents of the std::string are okay.
- size ( ) returns 666 (not shi**ing you guys) when in reality there are 659 characters and 7 line breaks (std::endls).

When I plug in 665 or anything below that into the function as third parameter I get "unexpected eof", 666 and above crashes.

However, I found out there's another unrelated problem with my code somewhere else that might well cause this behavior. It appears as if using .size (ie. 666) works fine and the crash comes from another parameter to the function.

In any case, I have learned a bunch from this little thread, thanks to everybody who contributed and helped point me to the right direction.

Share this post


Link to post
Share on other sites
iMalc    2466
We pretty much new that the problem was either with the other parameters or some other code. The problem is that you unfortunately haven't posted enough code for us to help with the real problem yet.

P.S. You are able delete your own double posts. There's this little checkbox above the smileys titled "Delete? To delete this post, check this box." when you click the Edit button for a post.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this