0

Code

char* CreateString(char* string1, char* string2) {
    
    int length = strlen(string1) + strlen(string2);

    // Allocate memory for the resulting string
    char* result = malloc((length) * sizeof(char)); 

    // Concatenate the two strings
    strcpy_s(result, sizeof result, string1);
    strcat_s(result,sizeof result, string2);
    return result;
}

I have this simple code of mine , all i want to do is add them together, but whenever I use strcpy_s or strcat_s it gives this error in the picture But it works if I use the CRT library.

Another question is that did I use the Pointers correctly? I'm new to this topic and it is confusing so I don't really understand it.

I tried to add Two Sentences together

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Welcome to SO. Please do not post pictures of code. Instead copy&paste the code as formatted text into the question. – Gerhardh Dec 28 '22 at 15:11
  • `sizeof result` is the size of a `char *`, not the size of the array. You should be doing `strcpy_s(result, strlen(string1), string1);` and `strcat_s(result, strlen(string2), string2);` – codyne Dec 28 '22 at 15:11
  • The assertion basically tells you exactly what is wrong. The buffer size is too small. This is because `sizeof result` does not do what you might expect. It evaluates to the size of a pointer, not to the size of allocated memory. Use `length` instead. – Gerhardh Dec 28 '22 at 15:11
  • 2
    Don't forget that strings in C are really called ***null-terminated** strings*. You need space for the null-terminator in your string. – Some programmer dude Dec 28 '22 at 15:12
  • 1
    @codyne Using `strlen(string1)` basically defeats the purpose of `strcpy_s` as it does not care about the size of the destination buffer. Also, that size is too small. From [documentation](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170): "Ensure that this size accounts for the terminating NULL following the string.". For `strcat_s` is is completely wrong as you would need both string lengths +1 – Gerhardh Dec 28 '22 at 15:16
  • What's the point of using `strcpy_s()` if you use `strlen()`? [**Updated Field Experience With Annex K — Bounds Checking Interfaces**, Common Mistakes](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1969.htm#mistakes): "A widespread fallacy originated by Microsoft's deprecation of the standard functions in an effort to increase the adoption of the APIs is that every call to the standard functions is necessarily unsafe and should be replaced by one to the "safer" API. ..." – Andrew Henle Dec 28 '22 at 16:12

1 Answers1

1
  1. strings require null terminating character at the end. So the buffer is too short.

  2. sizeof result gives the size of the pointer not the size of the referenced object.

char* CreateString(char* string1, char* string2) {
    
    size_t length = strlen(string1) + strlen(string2) + 1;
    // or for Windows
    // rsize_t length = strlen(string1) + strlen(string2) + 1;

    // Allocate memory for the resulting string
    char* result = malloc((length) * sizeof(*result)); 

    // Concatenate the two strings
    strcpy_s(result, length, string1);
    strcat_s(result, length, string2);
    return result;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    Ideally, you should check that the memory allocation succeeded before calling the copy functions. I note that if the body of the function was modified to: `size_t len1 = strlen(string1); size_t len2 = strlen(string2); char *result = malloc(len1 + len2 + 1); if (result != NULL) { memmove(result, string1, len1); memmove(result + len1, string2, len2 + 1); } return result;` then there would be no need to use `strcpy_s()` or `strcat_s()` at all. However, that would be evading the question. – Jonathan Leffler Dec 28 '22 at 15:33