-1

I am having difficulty using the strcat_s() function. Concatinating char strings works find, but trying with char* string will have runtime errors. As shown here

Here is the code:

#include "Mystring.h"
#include <iostream>
#include <cstring>
#include <cctype>   

int main() {
    char* str1 = new char[100];
    char* str2 = new char[10];
    strcpy_s(str1, strlen("Hello") + 1, "Hello");
    std::cout << str1;
    strcpy_s(str2, strlen(" World") + 1, " World");
    std::cout << str2;
    strcat_s(str1, sizeof(str1), str2);
    std::cout << str1;

    char str3[100] = "Hello";
    char str4[10] = " World";
    strcat_s(str3, sizeof(str3), str4);
}

I search everywhere for the implementation of strcat_s(), and did everything as instructed but there must be some underline principles that I do not understand.

interjay
  • 107,303
  • 21
  • 270
  • 254
Harry Vu
  • 1
  • 1
  • See https://stackoverflow.com/questions/61842754/sizeof-on-array-variable-vs-sizeof-on-pointer-in-c. You also have an issue with the `strcpy_s` call, although it doesn't cause an error here: The second parameter should be the number of bytes in the destination, not the length of the source. – interjay Jul 04 '23 at 08:43
  • Note that the name of the source file is mentioned in the message! I find it at `C:\Program Files (x86)\Windows Kits\10\Source\10.0.22621.0\ucrt\inc\corecrt_internal_string_templates.h` – BoP Jul 04 '23 at 08:46
  • Why are you even still using those functions??? In C++ use 'std::strirg` like this `std::string str1{"hello"}; str2{" world"}; str1 = str1 + str2;`. Certainly if you want to write safe code you snould not be using manual `new`. (You already have a memory leak too) – Pepijn Kramer Jul 04 '23 at 08:51
  • Second parameter of `strcpy_s` is the size of buffer (param1) and not the length of copied string. 100 and 10 in your first 2 cases. – i486 Jul 04 '23 at 08:52
  • I am very new to programming, and this is just to solve a problem in my assignment (learning how to write my own string class, and add some functionalities). Thank you all for the help – Harry Vu Jul 04 '23 at 09:48

1 Answers1

1

This line:

strcat_s(str1, sizeof(str1), str2);

sizeof(str1) is not evaluating to what you think it is. str1 is a pointer to the first byte in an array, not the array itself.

So sizeof(str1) will evaluate the size of a pointer: either 4 or 8 bytes. That's less than the number of bytes required to hold "Hello World". That's why it asserts. You're basically invoking it like this:

strcat_s(str1, 4, " World");

As you have discovered, the quick fix is change this:

char* str1 = new char[100];

To this:

char str1[100];

And don't invoke delete[] on it since it's no longer being allocated with new. Then sizeof() will work as you might expect.

Or, the more general fix is to always associate a size capacity variable with any given pointer to an array.

Also, while we are here, this is bad form:

strcpy_s(str1, strlen("Hello") + 1, "Hello");

This doesn't cause a problem with your program, but your second param to destination should be the capacity of the destination buffer, not the length of the source string. It just happens to fit in your code.

Better program:

int main() {
    const size_t len1 = 100;
    const size_t len2 = 10;
    char* str1 = new char[len1];
    char* str2 = new char[len2];

    strcpy_s(str1, len1, "Hello");
    std::cout << str1;

    strcpy_s(str2, len2, " World");
    std::cout << str2;

    strcat_s(str1, len1, str2);
    std::cout << str1;

    char str3[100] = "Hello";
    char str4[10] = " World";
    strcat_s(str3, sizeof(str3), str4);

    delete [] str1;
    delete [] str2;
}
selbie
  • 100,020
  • 15
  • 103
  • 173