Sign in to follow this  
wforl

string error

Recommended Posts

Hmm, i seem to be having in issue with declaring my string variables, they weren't displaying correctly, so i start to debug the code, and notice that they dont get initialised with what i set, and instead get some random junk. Also when i view them while in debug mode i get the warning below. http://i13.photobucket.com/albums/a280/benjidrummer/Untitled-1-1.jpg Photobucket

Share this post


Link to post
Share on other sites
Quote:
Original post by wforl
Hmm, i seem to be having in issue with declaring my string variables


Uh, I don't see any string variables there. All I see is a char[256].

Quote:

// Make static so memory is not allocated every frame


You're calling Font::DrawText every frame, and you're worried about a simple memory allocation on the stack?

Meanwhile, are you sure mFPS, mMilliSecPerFrame and the camera position will always be floats, and mNumTris and mNumVertices always ints? How about that memResources thing that's scrolled off the edge of the screenshot - always a char*? Would you like the flexibility to change those things without worrying about your whole program being crashed by what was supposed to help you debug? Or, for that matter, the flexibility to have the message at the end be as long as it needs to be?

Fear not, modern C++ is here.


void GfxStats::display() {
using std::string;
using std::stringstream;
using std::setprecision;
using std::fixed;
stringstream ss;
// Note that the 'precision' setting is "sticky".
ss << "Frames Per Second = " << fixed << setprecision(2) << mFPS
<< "\nMilliseconds Per Frame = " << setprecision(4) << mMilliSecPerFrame
<< "\nTriangleCount = " << mNumTris
<< "\nVertexCount = " << mNumVerticies
<< "\ncam = " << gCamera->mPosW.x << gCamera->mPosW.y << gCamera->mPosW.z
<< "\n" << memResources;

RECT R = {5, 5, 0, 0};
HR(mFont->DrawText(0, ss.str().c_str(), &R, DT_NOCLIP, D3DXCOLOR(0.5, 0.5, 0.5, 1)));
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by wforl
Hmm, i seem to be having in issue with declaring my string variables


Uh, I don't see any string variables there. All I see is a char[256].



I think the OP was referring to word1, word2, and word3 in addMemResource.

Share this post


Link to post
Share on other sites
Yes i was refering to the string word1,word2 and word3. :D


Quote:
You're calling Font::DrawText every frame, and you're worried about a simple memory allocation on the stack?


huh?


Quote:
Meanwhile, are you sure mFPS, mMilliSecPerFrame and the camera position will always be floats, and mNumTris and mNumVertices always ints? How about that memResources thing that's scrolled off the edge of the screenshot - always a char*? Would you like the flexibility to change those things without worrying about your whole program being crashed by what was supposed to help you debug? Or, for that matter, the flexibility to have the message at the end be as long as it needs to be?


Well yes the camera will always be floats, and the mNumTris and mNumVertices will always be whole numbers, (ints).

Thanks for the time you took to type the next bit out, that looks very interesting, but i dont understand how it works though, how would it be flexible if the variables im passing into the function are going to be fixed anyway.

PLease explain more.

Share this post


Link to post
Share on other sites
Quote:
Original post by wforl
Quote:
You're calling Font::DrawText every frame, and you're worried about a simple memory allocation on the stack?


huh?


If you make the variable normally (not 'static'), it will be stack allocated, just like any other local variable, because you aren't asking for a dynamic allocation (with 'new' or something like that). It will automatically be cleaned up when the function exits, and setting this up takes a tiny amount of time. It is much, much, much faster than rendering text to the screen, so there is no realistic "performance" argument to make.

By writing 'static', you write more, make the code harder to understand (leading to writing even more in the form of the comment), and make the function non-reentrant (potentially causing problems with multithreading), for no real gain.

Quote:
Thanks for the time you took to type the next bit out, that looks very interesting, but i dont understand how it works though, how would it be flexible if the variables im passing into the function are going to be fixed anyway.

PLease explain more.


The operator<< on the stream is overloaded for all the various primitive types. It will automatically "do the right thing" for each kind of data that you append to the stream. If you decide later that you want a double instead of a float for your camera positions, there is no need to change a format specifier from '%f' to '%d', because there are no format specifiers. The compiler sees that the expression 'gCamera->mPosW.x' is of type double, and calls the appropriate overload. Similarly if 'memResources' changes from a char* to a std::string. You can't even feed a std::string directly to printf(), because it's an object, not a primitive type. There doesn't exist a format specifier for it. With the stream, if you want to make it possible to "output an object", all you need to do is write the appropriate overloaded function: ostream& operator<<(ostream&, const Class&).

If you are using classes and all this other C++ stuff, you are supposed to have been shown how the stream classes work, right near the beginning of your C++ education. Since that seems not to be the case here, you can try starting here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
If you decide later that you want a double instead of a float for your camera positions, there is no need to change a format specifier from '%f' to '%d'

Isn't %d for integers?

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Zahlman
If you decide later that you want a double instead of a float for your camera positions, there is no need to change a format specifier from '%f' to '%d'

Isn't %d for integers?


Yes. Zahlman obviously intended it as a an example of the kind of mistake one can make with hand written format specifiers. [grin]

Share this post


Link to post
Share on other sites
Well, yes your right, we haven't been taught much in terms of string/file streams, just your basic cout/cin stuff.

But in any case, most of what i have learnt with regards to c++ has largely come out of books that i have purchased, such as c++ in 21 days, and also Herbert Scheidt's book. And these only seem to touch on it.

But yes, i know how important this knowledge is, and will do my best to read up on information from that link, despite its immense size :D

I actually use cplusplus all the time, love that site :D

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