Example 1 — Duplicate With new[]
Copy a C-string portably.
#include <cstring>
#include <iostream>
int main() {
const char* src = "hi";
char* p = new char[std::strlen(src) + 1];
std::strcpy(p, src);
std::cout << p << '\\n';
delete[] p;
return 0;
} How It Works
- Allocate length+1 bytes.
- strcpy copies including null.
- delete[] releases the buffer.

