简体   繁体   中英

Using strcpy_s() and strcat_s() with dynamically allocated strings

In the following code, I try to create a function that gets a dynamically allocated string. The function will ask the user to input another string that will also be dynamically allocated. Finally, you will allocate another large string to which the initial string will be copied, followed by the second string.

My problem: In the function I use strcpy_s() and strcat_s() to copy and concatenate the strings and for a mysterious reason 2 functions override the program ...

#include<iostream>

using namespace std;
void addChars(char** theStr);

void main()
{
    // const def
    const int STR_SIZE = 10;
    char* str = new char[STR_SIZE + 1];

    cout << "Enter 10 chars:" << endl;
    cin.getline(str, STR_SIZE + 1);

    if (cin.fail())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max());
    }

    addChars(&str);

    cout << str << endl;

    system("pause");
}

void addChars(char ** theStr)
{
    int toAdd;
    int oldSize;
    char* AddStr;
    char* tempstr;

    cout << "How many chars to add:" << endl;
    cin >> toAdd;
    AddStr = new char[toAdd + 1];

    cout << "Enter the chars: " << endl;
    cin.clear();
    cin.ignore();
    cin.getline(AddStr, toAdd + 1);

    // check the src string size
    oldSize = strlen(*theStr);

    // alloc the big new str for the 2 exis strings
    tempstr = new char[(oldSize + toAdd) + 1];

    // copy the old string to the new
    strcpy_s(tempstr, oldSize, *theStr);

    // add the AddStr to the string
    strcat_s(tempstr, toAdd, AddStr);

    // delete the older and updates the new address
    delete[] * theStr;
    *theStr = tempstr;
}

strcpy_s(tempstr, oldSize, *theStr);

should be

strcpy_s(tempstr, (oldSize + toAdd) + 1, *theStr);

I referred this doc( https://en.cppreference.com/w/c/string/byte/strcpy ) and it states that you should specify the destination size and not the source size.

errno_t strcpy_s(char *restrict dest, rsize_t destsz, const char *restrict src);

strcpy_s(tempstr, oldSize, *theStr);

Assertion fails, oldSize is too small.

The previous line is

tempstr = new char[(oldSize + toAdd) + 1]; 

You should pass oldSize + toAdd as the second parameter to strcpy_s.

All that said, this code is the best example of how not to code today in C++.

This line:

// add the AddStr to the string
strcat_s(tempstr, toAdd, AddStr);

should be:

// add the AddStr to the string
strcat_s(tempstr, oldSize, AddStr);

Point (2)

because you want to concatentate the new string AddStr after oldSize Bytes of the old string, which is already copied in tempstr.

But this is far away from modern C++. Use std::string instead

Otherwise it should be tagged as C or C11 instead of C++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM