Advertisement

Problem converting macro'd long to a displayable value

Started by March 11, 2000 08:32 PM
6 comments, last by noxa 24 years, 9 months ago
Hello, just ran into a little problem in my coding... It''s a bit hard to explain, so I''ll give a sample of some code that is like what I''m using: (BTW, this is in MSVC++ w/ MFC) #define DSENGINE_VERSION_LONG 215l int main() { CString csTemp; ltoa(DSENGINE_VERSION_LONG,_ csTemp.GetBuffer(30), 10); OutputDebugString(csTemp); return(0); } now, this looks OK to me, but when run, nothing is printed out! Just a space... In debug mode csTemp is "215" like it should be, but there is nothing displayed but a friggin space! What is wrong with it? Please mail me, as sometimes my boss won''t let me on the net :-) Ben Vanik Lead programmer on the DSEngine project
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
I can''t mail you, as my boss won''t let me have email at work Maybe someone else will pass this on or benefit.

Advice: Wherever possible, use constants instead of #defines. In this case, I''d use const long 216L . Although it may make absolutely no difference here, it allows the compiler to do some type-checking for you, which is always a good thing.

Also, what''s that extra underscore doing in the ltoa function? (After the 1st comma.) This isn''t Visual Basic I''m assuming I''m missing something here.

Does ltoa have a useful return value? (I don''t remember.) If so, check it. Return values are your friend

Sorry I can''t be more help.
Advertisement
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.

LordFoul''s correct.

Why don''t you use CString::Format? Unless you''re using a radix other than 10 or 16, it should work fine:
csTemp.Format ("%d", DSENGINE_VERSION_LONG); // in decimal

Got it all working, thanks!

The underscore was in MSDN, so I figured it couldn''t hurt! :-)

I never knew about the ReleaseBuffer being required.... I can call functions now and their OK, what exactly does it do/prevent?

Ben Vanik
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
CString is a dynamic string, and therefore a dynamic array. The thing that you have to watch when working with dynamic arrays is that you have enough memory if the string grows... so... whenever you change the string, it is actually re-allocated to make sure it has enough memory.

This causes a problem when you attempt to temporarily make this dynamic type into a type that doesn''t have these dynamic attributes (which is what happens when you do a GetBuffer() to a CString and get a character pointer). In order to compensate for the temporary loss of its dynamic attributes, the CString put it in what is more or less a temporary buffer of the length you told it when you pass in the value to GetBuffer() (the null terminating character is not included in this calculation). When you call ReleaseBuffer() you are basically telling it to throw away any unused memory (it doesn''t need it anymore, it''s a dynamic class) and retain the information you changed using the pointer returned by GetBuffer. YOUR CHAR POINTER RETURNED BY GETBUFFER IS NO LONGER VALID AT THIS POINT! (I''ve seen way too many people forget this.) CString then determines whether or not the size has changed, and if so, it reallocates it to a different area with room. If it''s not bigger, it leaves it where it originally was.

As an aside, using Format() is a much better idea.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Advertisement
Thanks! That makes sense now that I think about it... hmmm, good thing my new project is still in it''s infancy, I can warn my partner too...

Just as a side note, does anyone have a class similar to CString that works? I tried to write one, but some of the things (such as all the operators and such) caused some memory leaks and I gave up.. It''s the only reason I''m using MFC and really want to trash it. I need one that can be passed as a const char* and a char* and also be set to const char*''s, char*''s, and other objects like it... If anyone has one/know''s of one, PLEASE REPLY (I''m desperate :-)

Ben Vanik
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
Just use string from the STL library, it''s always worked great for me and has the added benifit of being platform independent. The docs can be a little confusing though

This topic is closed to new replies.

Advertisement