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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:53:36+00:00 2026-06-04T09:53:36+00:00

Does using _tcsncpy_s() on a string multiple times write over the old contents? Or

  • 0

Does using _tcsncpy_s() on a string multiple times write over the old contents? Or does it create new contents and then point to the new contents? As a simple example, if i have:

    LPTSTR myString = new TCHAR[MAX_PATH];
    LPTSTR copiedString1 = "Hello";
    LPTSTR copiedString2 = "Rock";
    _tcsncpy_s(myString,MAX_PATH,copiedString1,5); //1
    //delete [] myString; //3
    //LPTSTR myString = new TCHAR[MAX_PATH]; //3
    _tcsncpy_s(myString,MAX_PATH,copiedString2,4); //2

I understand at 1: we have myString –> ‘H’ ‘e’ ‘l’ ‘l’ ‘o’

But at point 2: Does ‘R’ ‘o’ ‘c’ ‘k’ get copied over ‘H’ ‘e’ ‘l’ ‘l’, while ‘o’ remains? Or is it now pointing to a new area in memory? Do I need to delete and recreate myString like in 3? What if I have copiedString2 first and then copiedString1? Does anything different happen? Anything else that might be useful to know?

Thank you for your time and have a nice day.

  • 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-04T09:53:38+00:00Added an answer on June 4, 2026 at 9:53 am

    But at point 2: Does ‘R’ ‘o’ ‘c’ ‘k’ get copied over ‘H’ ‘e’ ‘l’ ‘l’,
    while ‘o’ remains?

    Yes. Read the documentation:

    These functions try to copy the first D characters of strSource to strDest, where D is the lesser of count and the length of strSource. If those D characters will fit within strDest (whose size is given as numberOfElements) and still leave room for a null terminator, then those characters are copied and a terminating null is appended; otherwise, strDest[0] is set to the null character and the invalid parameter handler is invoked, as described in Parameter Validation.

    Now, for your other questions:

    Or is it now pointing to a new area in memory?

    No, mystring is still refers to the same array.

    Do I
    need to delete and recreate myString like in 3?

    Perhaps. Depends on what you want to do. If you want copies of both strings, then yes, you will need two character arrays (either static or dynamic).

    What if I have
    copiedString2 first and then copiedString1?

    In that case, you’d get Rock as your string.

    Does anything different
    happen?

    A different string is present in mystring after the operations, so yes.

    Anything else that might be useful to know?

    Do you want to concatenate these two strings? If so, use a concatenating function, such as strcat. Also, note that functions beginning with an underscore are non-standard, vendor specific functions and thus will not be portable in all probability. Try to use a standard defined function (such as strncpy, strcat, strncat etc.).

    The _t prefixed functions in MS world are most often macros that switch to the appropriate ASCII/Unicode versions of the respective functions depending on your project settings (i.e. whether UNICODE/_UNICODE pre-processor macro has been defined or not).

    Finally, the variations of string copy and concatenation with an n in between reads atmost n characters from the source. This design is allow programmers to write secure code (and thus prevent buffer overflows).

    Oh, and before we forget, if you’re using C++, you ought to forget all about C-style string manipulation and simply switch over to the neater, easier to use std::string (or std::wstring as the case maybe). String copy happens via simple assignment (i.e. = suffices) and concatenation is equally idiomatic (i.e. += suffices). For more check the documentation.

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

Sidebar

Related Questions

I heard that microsoft does not recommend using DataSets and DataTables in new projects.
Does using operator new in c# might fail (if it requires a large memory
What does using (SqlConnection cn = new SqlConnection(connectionString)) do?
Does using then instead of semicolon or linebreak have only decorative purpose (to make
Does using Multiple .NET Languages (Delphi, VB.NET, C#) into the same application (but in
Possible Duplicate: At what point does using a StringBuilder become insignificant or an overhead?
What advantage does using :refer in :require have over using :only in :use ?
Possible Duplicate: What's the point of the var keyword? What advantages does using var
Does using Service Oriented Architecture (SOA) in a system mean it is a distributed
Does using the computer for something else while benchmarking ( with the Benchmark module

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.