-
June 7th, 2013, 12:57 PM
#1
return string length
Today we have run into a little problem, too tiny for me to have paid my attention to
PHP Code:
int processString(xxx)
{
//do something
return non-null terminated string length
}
to make the function work better we change its internals
PHP Code:
int processString(xxx)
{
//do something
return null terminated string length
}
This phenonmenon can be observed by use of mutibyte to wide char conversions, string copy , concatenation functions of MS etc
Should I minus one in the return value of the second function to match both versions ? Thank you.
-
June 7th, 2013, 01:09 PM
#2
Re: return string length
???????? The length of a c-style char array is the number of characters from (and including) the start position proceeding (but not including) the null terminator.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 7th, 2013, 01:18 PM
#3
Re: return string length
Originally Posted by 2kaud
???????? The length of a c-style char array is the number of characters from (and including) the start position proceeding (but not including) the null terminator.
Ok why does the example in http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
yield 14 while
http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx 13 ?
-
June 7th, 2013, 01:34 PM
#4
Re: return string length
The difference is because the functions work slighty different.
wcstombs
If wcstombs successfully converts the multibyte string, it returns the number of bytes written into the multibyte output string, excluding the terminating NULL (if any). So correctly returns 13
wcstombs_s
If wcstombs_s successfully converts the source string, it puts the size in bytes of the converted string, including the null terminator. So correctly returns 14.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 8th, 2013, 06:54 AM
#5
Re: return string length
Yes, thank yu, that is why I ask whether or not should I minus one from the second function to match the two version ? For example, my current code is using the first function and I need to upgrade my code by eliminating use of unsafe function or replace it with the safer one (wcstombs_s)
-
June 8th, 2013, 07:02 AM
#6
Re: return string length
If you are upgrading your code from using the first function (wcstombs) to using the second (wcstombs_s), then to have your function processString return the same value, then IMO you will need to subtract one from the size in bytes used by the second function.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 8th, 2013, 07:24 AM
#7
Re: return string length
Thank you for your confirmation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|