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 3941532
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:33:16+00:00 2026-05-20T00:33:16+00:00

Here is a C code that converts a wchar_t* string into a char* string

  • 0

Here is a C code that converts a wchar_t* string into a char* string :

wchar_t *myXML = L"<test/>";
size_t length;
char *charString;
size_t i;
length = wcslen(myXML);
charString = (char *)malloc(length);
wcstombs_s(&i, charString, length, myXML, length);

The code compiles but at exectution it detects a fatal error at the last line and stops running.

Now, if I replace the last line with this one :

wcstombs_s(&i, charString, length+1, myXML, length);

I just added +1 to the third argument. Then it works perfectly…

Why is there a need to add this trick ? Or is there a flaw elsewhere in my code ?

  • 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-20T00:33:16+00:00Added an answer on May 20, 2026 at 12:33 am
    DESCRIPTION
           The wcslen() function is the wide-character
           equivalent of the strlen(3) function.  It determines
           the length of the wide-character string pointed to by
           s, not including the terminating L'\0' character.
    

    The trick is that you should always look for code of the form:

    string = malloc(len);
    

    very suspiciously, because both wcslen(3) and strlen(3) return the string length without the nul byte, and malloc(3) must allocate the space with that byte. C kinda sucks sometimes.

    So every time you see string = malloc(len); rather than string = malloc(len+1);, be very careful to read how len gets assigned.

    char String = (char *)malloc(length + 1);
    

    Ought to do the trick. 🙂

    EDIT:

    Better would be to ask wcstombs() for the size to allocate in the first place:

    size_t len = wcstombs(NULL,src,0) + 1;
    char *dest = malloc(len);
    len = wcstombs(dest, src, len);
    if (len == -1) /* handle error */ ...
    

    The +1 allocates for the ascii nul, and wcstombs() will report how much memory is required to do the conversion. It’ll do the conversion twice, once to keep track of the memory required, and then once to store the result, but it will be MUCH simpler to maintain. The second time, when it stores the result, it will write at most len bytes including the ascii nul.

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

Sidebar

Related Questions

i have code that takes a csharp datetime and converts it into a long
I have the following code that converts my twitter account rss feed into a
We have some old C code here that's built with nmake. Is there an
I have a piece of code here that i really could use some help
Here are two chunks of code that accomplish (what I think is) the same
Here is an interesting piece of code that my fellow team members were just
In writing the code that throws the exception I asked about here , I
Here's the situation: We have some generic graphics code that we use for one
Given some JS code like that one here: for (var i = 0; i
Here's a wierd one. I'm reusing a code base that unfortunately must not be

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.