# 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

##### Share on other sites
Is it a release build?

##### Share on other sites
............grrr

thankyou, i apologise for my stupidity

##### Share on other sites
Quote:
 Original post by wforlHmm, 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 on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by wforlHmm, 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 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.

##### 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 on other sites
Quote:
 Original post by ZahlmanIf 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 on other sites
Quote:
Original post by DevFred
Quote:
 Original post by ZahlmanIf 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 on other sites
... You know, I could just take the credit for that, but I'm not that kind of guy. Well spotted [smile]

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628293
• Total Posts
2981868

• 11
• 10
• 10
• 11
• 17