2

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.

JHowzer
  • 3,684
  • 4
  • 30
  • 36
  • Why should _tcsncpy_s allocate new memory?? You are passing it a pointer to a memory location where it should write into, and it simply writes into that location... If you pass it the same location twice, it writes into the same location twice... No magic is happening here. – Lanzelot Jun 17 '20 at 13:19

1 Answers1

3

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.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • @JHowzer: I'd strongly suggest you re-read the last para and see if you can move to a suitab`std::string` -- that's how it is done in C++. – dirkgently May 23 '12 at 20:00