Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:55:23+00:00 2026-05-25T18:55:23+00:00

In an almost complete effort to port libc++ to Windows, I need the function

  • 0

In an almost complete effort to port libc++ to Windows, I need the function mbsnrtowcs, and thought that the easiest way would be to implement it in terms of mbsrtowcs:

size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
               size_t nmc, size_t len, mbstate_t *__restrict__ ps )
{
    char* nmcsrc = new char[nmc+1];
    strncpy( nmcsrc, *src, nmc );
    nmcsrc[nmc] = '\0';
    const size_t result = mbsrtowcs( dst, &nmcsrc, len, ps );
    delete[] nmcsrc;
    return result;
}

The problem here is that mbsrtowcs needs &nmcsrc to be of type const char**, which is isn’t, because it is a string I just copied the first nmc elements into, and appended a \0 character to. How can I work around this? Strictly speaking, this is compiled as C++, so perhaps a const_cast would do little harm here? I also have access to c++0X (libc++ is/requires a subset c++0x).

Edit: the Clang error message reads:

M:\Development\Source\libc++\src\support\win32\support.cpp:37:27: error: no matching function for call to 'mbsrtowcs'
const size_t result = mbsrtowcs( dst, &nmcsrc, len, ps );
                      ^~~~~~~~~
M:/Development/mingw64/bin/../lib/clang/3.0/../../../x86_64-w64-mingw32/include\wchar.h:732:18: note: candidate function not viable: no known conversion from 'char **' to 'const char **restrict' for 2nd argument;
  size_t __cdecl mbsrtowcs(wchar_t * __restrict__ _Dest,const char ** __restrict__ _PSrc,size_t _Count,mbstate_t * __restrict__ _State) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
                 ^

EDIT2: To fix what was wrong Re. Matthieu, I have changed my naive implementation like so:

size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
                   size_t nmc, size_t len, mbstate_t *__restrict__ ps )
{
    char* local_src = new char[nmc+1];
    char* nmcsrc = local_src;
    strncpy( nmcsrc, *src, nmc );
    nmcsrc[nmc] = '\0';
    const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps );
    // propagate error
    if( nmcsrc == NULL )
        *src = NULL;
    delete[] local_src;
    return result;
}

And I have added a FIXME saying that the proper way to do this would be implementing it via mbrtowc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:55:24+00:00Added an answer on May 25, 2026 at 6:55 pm

    There is an issue here (not really related to the const I think).

    mbsrtowcs might set *src to NULL.

    mbsnrtowcs is supposed to do the same, of course.

    However for you here it causes a 2 bugs:

    • Because you created a local alias, if mbsrtowcs set *src to NULL it is not reflected for the caller of mbsnrtowcs
    • mbsnrtowcs has a memory leak if nmcsrc is set to NULL

    Perhaps that simply biting the bullet and implementing mbsnrtowcs in terms of mbrtowc would be more efficient ?

    If you need to implement both (as I guess you do), you could also reverse the problem on its head and implement mbsrtowcs in terms of mbsnrtowcs simply by calling strlen at the beginning without loss of generality (and avoid the copy).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question is almost complete in the title: is there any way, using jquery,
My project that I've spent almost 40hrs on is almost complete, but I have
I thought that the sys.path was a complete list of all search paths for
I have a site using CodeIgniter that is almost complete now. My problem is
I'll begin stating that I'm almost complete dumb in Cmake matter. I have the
I've got the functionality I need almost complete and am looking for a little
I have to implement game of life, it is almost complete, the last thing
The application I'm writing is almost complete and I'd like people who speak different
I am writing multi-thread socket chat in C++Builder 2009. It is almost complete in
Almost every Java book I read talks about using the interface as a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.