Share via


memcpy, wmemcpy (Windows CE 5.0)

Send Feedback

Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference

Copies characters between buffers.

void *memcpy(    void*dest,    const void*src,    size_tcount);inline wchar_t *wmemcpy(   wchar_t*dest,const wchar_t*src,size_tcount);

Parameters

  • dest
    New buffer.
  • src
    Buffer to copy from.
  • count
    Number of characters to copy.

Return Values

memcpy returns the value of dest.

Remarks

The memcpy function copies count characters of src to dest.

If the source and destination overlap, this function does not ensure that the original source characters in the overlapping region are copied before being overwritten.

Use memmove to handle overlapping regions.

Example

/* MEMCPY.C: Illustrate overlapping copy: memmove
 * handles it correctly; memcpy does not.
 */

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/*                           1         2         3         4         5
 *                  12345678901234567890123456789012345678901234567890
 */

void main( void )
{
   printf( "Function:\tmemcpy without overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 40 );
   printf( "Destination:\t%s\n", string1 + 16 );
   memcpy( string1 + 16, string1 + 40, 3 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );

   /* Restore string1 to original contents */
   memcpy( string1 + 16, string2 + 40, 3 );

   printf( "Function:\tmemmove with overlap\n" );
   printf( "Source:\t\t%s\n", string2 + 4 );
   printf( "Destination:\t%s\n", string2 + 10 );
   memmove( string2 + 10, string2 + 4, 40 );
   printf( "Result:\t\t%s\n", string2 );
   printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );

   printf( "Function:\tmemcpy with overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 4 );
   printf( "Destination:\t%s\n", string1 + 10 );
   memcpy( string1 + 10, string1 + 4, 40 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}

Output

Function:   memcpy without overlap
Source:      fox
Destination:   dog jumps over the lazy fox
Result:      The quick brown fox jumps over the lazy fox
Length:      43 characters

Function:   memmove with overlap
Source:      quick brown fox jumps over the lazy dog
Destination:   brown fox jumps over the lazy dog
Result:      The quick quick brown fox jumps over the lazy dog
Length:      49 characters

Function:   memcpy with overlap
Source:      quick brown dog jumps over the lazy fox
Destination:   brown dog jumps over the lazy fox
Result:      The quick quick brown dog jumps over the lazy fox
Length:      49 characters

Security Remarks

The first argument, dest, must be large enough to hold count characters of src; otherwise, a buffer overrun can occur.

This can lead to a denial of service attack against the application if an access violation occurs, or in the worst case, allow an attacker to inject executable code into your process. This is especially true if dest is a stack-based buffer.

The last argument, count, is the number of characters to copy into dest, not the size of the dest.

The following code example, shows a safe way to use memcpy:

void test(char *pbData, unsigned int cbData) {
    char buf[BUFFER_SIZE];
    memcpy(buf, pbData, min(cbData,BUFFER_SIZE));
}

Requirements

OS Versions: Windows CE 2.0 and later.
Header: stdlib.h and wchar_ship.h.
Link Library: coredll.dll.

See Also

_memccpy | memchr | memcmp | memmove | memset | strcpy | strncpy

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.