CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Posts
    38

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    Jun 2012
    Posts
    38

    Re: return string length

    Quote Originally Posted by 2kaud View Post
    ???????? 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 ?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  5. #5
    Join Date
    Jun 2012
    Posts
    38

    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)

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  7. #7
    Join Date
    Jun 2012
    Posts
    38

    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
  •  





Click Here to Expand Forum to Full Width

Featured