I would like to concatenate 2 strings in C or C++ without new memory allocation and copying. Is it possible?
Possible C code:
char* str1 = (char*)malloc(100);
char* str2 = (char*)malloc(50);
char* str3 = /* some code that concatenates these 2 strings
without copying to occupy a continuous memory region */
Then, when I don't need them any more, I just do:
free(str1);
free(str2);
Or if possible, I would like to achieve the same in C++, using std::string
or maybe char*
, but using new
and delete
(possibly void operator delete ( void* ptr, std::size_t sz )
operator (C++14) on the str3).
There are a lot of questions about strings concatenation, but I haven't found one that asks the same.