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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:51:10+00:00 2026-06-12T00:51:10+00:00

I’m trying to understand how the wcsncpy_s function works and how it prevents against

  • 0

I’m trying to understand how the wcsncpy_s function works and how it prevents against buffer overflows. First, according to MSDN, the arguments to this function mean the following:

strDest= Destination string.

numberOfElements= The size of the destination string.

strSource= Source string.

count= Number of characters to be copied, or _TRUNCATE.

Now consider this code:

wchar_t a[5];
wcsncpy_s(a, 10, L"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 9);

printf("%d\r\n", sizeof(a));//10
printf("%d\r\n", wcslen(a));//9
wprintf(L"%s", a);//ABCDEFGHI

If I am making sense of all this, “a”, which is supposed to hold at most 4 wide characters plus a null terminator, now holds 9 wide characters.

Now, the following code will cause my app to terminate abruptly due to a failed debug assertion (VS 2005 compiler):

wchar_t a[5];
wcsncpy_s(a, 10, L"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10);

printf("%d\r\n", sizeof(a));
printf("%d\r\n", wcslen(a));
wprintf(L"%s", a);

Can someone please explain the code above and also how wcsncpy_s is supposed to prevent against buffer overflows?

  • 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-06-12T00:51:12+00:00Added an answer on June 12, 2026 at 12:51 am
    wcsncpy_s(a, 10, L"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 9);
    

    You are lying to the function. You tell it, “a has sufficient space to store 10 characters,” when in fact it has only enough space to store five. The function trusts that you are providing it with valid information (how could it know that you aren’t?)

    Note that while you get a runtime error with the second code snippet, the first code snippet is equally wrong. Both write past the end of the array a.

    That said: you are using the wrong overload of wcsncpy_s: when compiling C++ code, there is an additional wcsncpy_s overload that is a template that deduces the size of the target array. If you were to change the call to:

    wcsncpy_s(a, L"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 9);
    

    The template will deduce that the array has five elements and automatically use that as the size. This works only when the target is an array; it does not work if the target is a pointer to the initial element in an array.

    Ideally, if you are using C++, it is best to avoid C string manipulation altogether: use std::wstring or some other string type. If you do want to use these functions that work with C strings, at least use std::vector<wchar_t> or std::array<wchar_t, N> instead of raw arrays: it’s much harder to screw up the code. For example,

    std::array<wchar_t, 5> a;
    wcsncpy_s(a.data(), a.size(), L"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 9);
    

    The code for std::vector<wchar_t> would be identical. Note that obtaining a pointer to the underlying array and obtaining the size of that array follow the same form, so it’s both easy to write the code and easy to check that the code is correct (simple visual inspection of the invocation is all that is required).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.