Strcpy() doesn't have memory leaks. It's actually one of the faster and most reliable string library functions. Of course, the way you're using it, it will crash, because you're trying to write data to address 0x0 in memory, which won't work no matter what string copy function you use. You have to allocate memory first, with malloc() or by using an array.
If you're looking to write a more efficient strcpy function (which will be very difficult, as the default is highly optimized), you could try:
- Copying the data in larger blocks of data, by using unsigned long long (8 bytes) or xmm registers that can hold up to sixteen bytes at a time.
- Writing the loop in assembly so you can access the said xmm registers, and bypass the stack access by only writing to and from registers and memory
- Prefetching data before you copy if your architecture's documentation specifies that this will improve performance
- Aligning data before and during the copy if your architecture can handle aligned i/o faster than unaligned i/o (most can)
- Unrolling the loop and copying multiple blocks of 16 bytes in each iteration
- Checking for zero (when to stop copying) with the mmx-specific "pcmpeq", "pmovmskb", and "bsf" instructions. You can find examples online on how to implement them in assembly.
Also, you might want to check out a quick strlen() rendition I scrapped together, as knowing the length beforehand will speed up the copy:
Note: you can find something _similar_ by disassembling the default strlen() command in a debugger.
movq %rdi, %rax
pxor %xmm0, %xmm0
andq $-16, %rax
addq $16, %rax
movdqa (%rax), %xmm1
pcmpeqb %xmm0, %xmm1
pmovmskb %xmm1, %edx
bsf %edx, %edx
subq %rdi, %rax
addq %rdx, %rax