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);