return string length
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: return string length

  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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center