0

My goal is to generate a new array with the correct amount of spots and copy an old character array into it.

When using strcpy_s, an exception is thrown. I can not figure out why the exception is being thrown which states that the Buffer is too small. I can not use vectors or strings. How can I fix this using strcpy_s and char arrays?

    char str[4] = { 't', 'e', 's', 't' };
    int allocated = 4;
    char * reservedString = new char[allocated]();
    strcpy_s(reservedString, allocated, str);

EDIT: Changing my code to add one to the array gives me the same "buffer too small" exception.

char str[4] = { 't', 'e', 's', 't' };
int allocated = 4;
char * reservedString = new char[allocated+1]();
strcpy_s(reservedString, allocated, str);

EDIT 2: As someone commented str needed to be set to 5 in size and have a null terminator included. Thank you this fixed my problem.

Updated code:

    char str[5] = { 't', 'e', 's', 't', '\0'};
    int allocated = 5;
    char * reservedString = new char[allocated]();
    strcpy_s(reservedString, allocated, str);
EmptyStuff
  • 228
  • 1
  • 5
  • 11

4 Answers4

1

char str[4] = { 't', 'e', 's', 't' }; is a 4-byte array in the memory. It is not a string, and it is completely random where a "trailing" zero will occur after these 4 bytes, and an arbitrary amount of other data in between.
However, strcpy_s() expects copying a zero-terminated string, just one of the extras it does is checking if the source string will fit into the destination. It will not, that is why you get the error.

[...] the following errors are detected at runtime and call the currently installed constraint handler function:
* src or dest is a null pointer
* destsz is zero or greater than RSIZE_MAX
* destsz is less or equal strnlen_s(src, destsz); in other words, truncation would occur
* overlap would occur between the source and the destination strings

You get the third one, a truncation of the "garbage" bytes would occur.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
1

You need five characters to store the zero-terminated string "test". Your str array is just four characters, with no zero terminator. If you want a zero-terminator, declare it like this:

char str[] = "test";

Then you need of course

int allocated = 5;

And after that:

char * reservedString = new char[allocated];
strcpy_s(reservedString, allocated, str);
TonyK
  • 16,761
  • 4
  • 37
  • 72
  • @tevemadar (camelbird?): Thank you for pointing that out. I have edited my answer accordingly. – TonyK Apr 06 '19 at 22:01
  • Yes, that's Hungarian. Tevemadár is a character from an older cartoon series, https://en.m.wikipedia.org/wiki/Jamie_and_the_Magic_Torch (called "Yoo-hoo Bird" in the original) – tevemadar Apr 06 '19 at 22:10
1
  1. str is not a string. A string is a sequence of non-NUL characters, terminated by NUL.

  2. You should pass the size of the buffer to strcpy_s(), not the maximum string-size (which is one less).

  3. That is, if you should use strcpy_s() at all. You shouldn't.

    Use strcpy(), or as you already have the exact size, memcpy() or std::copy_n().

  4. As a side-note, zeroing memory just to turn around and overwrite it is a pointless waste.

Community
  • 1
  • 1
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

You are not allocating the proper memory:

char str[4] = { 't', 'e', 's', 't' };

It allocates 5 bytes, 4 for each character plus the null terminator.---

Do:

char str[4] = { 't', 'e', 's', 't' };
char * reservedString = new char[5]();
strcpy_s(reservedString, allocated, str);

Or:

char str[4] = { 't', 'e', 's', 't' };
char * reservedString = new char[5]();
strcpy(reservedString, str);
mohabouje
  • 3,867
  • 2
  • 14
  • 28
  • 2
    It does not allocate five bytes! That is part of the problem: `str` is not null-terminated. – TonyK Apr 06 '19 at 21:06
  • I tried adding one to the array and it still gave me the "buffer too small" exception. I am worried about using strcpy because it will write memory too far if I have a bug in my code. – EmptyStuff Apr 06 '19 at 21:06
  • Thank you @TonyK that was my problem! – EmptyStuff Apr 06 '19 at 21:10