Sign in to follow this  

_itoa_s use

This topic is 4092 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

Alright, I need to convert an integer to and std::string and the only method I know is to use _itoa_s to create a char buffer, then create th' string based on that. So, for some reason I am not doing it correctly. I get some kind of overflow error. Here is the error text: Debug Assersion failed Program: ... File: f:\rtm\vctools\crt_bld\self_x86\crt\src\xtoa.c Line: 142 Expression: length < sizeInTChars For information blah blah blah blah. Alright, so that is the message I am getting. It's probably to do with the stupid _itoa_s and my not know what I am doing :) So, here is the code that is triggering it:
char tempbuf[3] = { ' ',' ',' '};
_itoa_s( m_weapon_mod, tempbuf, 10);
std::string weapon_mod_s(tempbuf);

Later on, I print this, but as far as I know that is error free (I cannot progress farther into the program w/o fixing this... I am I doing this correctly? I only need 3 characters. like "123". That's it. So... am I doing this the best way? am I even using _itoa_s correctly? Thanks...

Share this post


Link to post
Share on other sites
Old C style functions like _itoa_s and C style strings don't belong anywhere near C++. Use a stringstream to convert between normal types.

std::stringstream stream;
stream << 10;
std::string weapon_mod_s = stream.str();

Share this post


Link to post
Share on other sites
You are using it correctly. The error you are getting is a result of tempbuf not being large enough to hold the result of the number in m_weapon_mod. You need room for the number and the '\0' that it puts at the end of the string.

So, to convert:

int x = 123;

you need:

char tempbuf[4];

since _itoa_s will want to write "123\0" to the output buffer.

Share this post


Link to post
Share on other sites
The third parameter to _itoa_s is the size of the buffer in characters, so if your "tempbuf" contains 3 characters, you should pass in 3 rather than 10. Also note that tempbuf needs room for the trailing NULL character, so you should really have a 4-character buffer and pass in 4 as parameter 3, when trying to convert a 3-digit number.

However, if you are programming C++ rather than C, I second (third?) the recommendation to use a stringstream, or boost::lexical_cast.

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
The third parameter to _itoa_s is the size of the buffer in characters, so if your "tempbuf" contains 3 characters, you should pass in 3 rather than 10. Also note that tempbuf needs room for the trailing NULL character, so you should really have a 4-character buffer and pass in 4 as parameter 3, when trying to convert a 3-digit number.

However, if you are programming C++ rather than C, I second (third?) the recommendation to use a stringstream, or boost::lexical_cast.


Actually there are 2 versions of _itoa_s .. one is templated and the size is acquired from the buffer. He happens to be using that one. He is calling it correctly, his buffer just isn't large enough.

*Edit*

Here are the declarations of the two versions:


errno_t _itoa_s(
int value,
char *buffer,
size_t sizeInCharacters,
int radix
);

template <size_t size>
errno_t _itow_s(
int value,
wchar_t (&buffer)[size],
int radix
); // C++ only


He happens to be using the 2nd one.

Share this post


Link to post
Share on other sites

This topic is 4092 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.

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