In C:

#include <string.h> // memcpy
#include <stdlib.h> //realloc

In C++, remove the .h and prefix with a c. In C++, they will be placed in the std namespace, but are also global.

Answer from GManNickG on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com β€Ί c_standard_library β€Ί c_function_memcpy.htm
C library - memcpy() function
The C library memcpy() function is also known as Copy Memory Block function / Memomy to Memory Copy. It is used to specify the range of characters which could not exceed the size of the source memory.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c++ β€Ί memcpy-in-cc
memcpy() in C - GeeksforGeeks
September 22, 2025 - It does not perform a deep copy or handle objects at a higher level. memcpy() copies the actual bytes of the memory you specify. If you pass it a pointer variable, it copies the pointer’s value (the address).
🌐
TechOnTheNet
techonthenet.com β€Ί c_language β€Ί standard_library_functions β€Ί string_h β€Ί memcpy.php
C Language: memcpy function (Copy Memory Block)
/* Example using memcpy by TechOnTheNet.com */ #include <stdio.h> #include <string.h> int main(int argc, const char * argv[]) { /* Create a place to store our results */ int result; /* Create two arrays to hold our data */ char original[50]; char newcopy[50]; /* Copy a string into the original array */ strcpy(original, "C memcpy at TechOnTheNet.com"); /* Copy the first 24 characters of the original array into the newcopy array */ result = memcpy(newcopy, original, 24); /* Set the character at position 24 to a null (char 0) in the newcopy array to ensure the string is terminated (This is important since memcpy does not initialize memory and printf expects a null at the end of a string) */ newcopy[24] = 0; /* Display the contents of the new copy */ printf("%s\n", newcopy); return 0; }
🌐
Microsoft Learn
learn.microsoft.com β€Ί en-us β€Ί cpp β€Ί c-runtime-library β€Ί reference β€Ί memcpy-wmemcpy
memcpy, wmemcpy | Microsoft Learn
March 28, 2024 - #define _CRT_SECURE_DEPRECATE_MEMORY #include <memory.h> ... For more compatibility information, see Compatibility. See memmove for a sample of how to use memcpy.
🌐
cppreference.com
en.cppreference.com β€Ί c β€Ί string β€Ί byte β€Ί memcpy
memcpy, memcpy_s - cppreference.com
Where strict aliasing prohibits examining the same memory as values of two different types, memcpy may be used to convert the values. ... #define __STDC_WANT_LIB_EXT1__ 1 #include <stdio.h> #include <stdint.h> #include <inttypes.h> #include <string.h> #include <stdlib.h> int main(void) { // simple usage char source[] = "once upon a midnight dreary...", dest[4]; memcpy(dest, source, sizeof dest); for(size_t n = 0; n < sizeof dest; ++n) putchar(dest[n]); // setting effective type of allocated memory to be int int *p = malloc(3*sizeof(int)); // allocated memory has no effective type int arr[3] =
🌐
Cplusplus
cplusplus.com β€Ί reference β€Ί cstring β€Ί memcpy
memcpy
Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination. The underlying type of the objects pointed to by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
🌐
Sternum IoT
sternumiot.com β€Ί home β€Ί memcpy c function – syntax, examples, and security best practices
memcpy C Function | Syntax, Examples & Security Best Practices | Sternum IoT
January 30, 2024 - In this example, memcpy is used to copy the contents of the source array to the buffer array. Since it does not add a null terminator, the programmer must manually add one to ensure that the copied string is properly terminated. In contrast, the strcpy function is used to copy the same string to the buffer array, and automatically adds a null terminator to the end of the copied string. #include <stdio.h> #include <string.h> int main() { char source[] = "Hello, world!"; char buffer[20]; // Use memcpy to copy the string to the buffer memcpy(buffer, source, strlen(source)); buffer[strlen(source)] = '\0'; printf("Using memcpy: %s\n", buffer); // Use strcpy to copy the string to the buffer strcpy(buffer, source); printf("Using strcpy: %s\n", buffer); return 0; }
🌐
Linux Man Pages
man7.org β€Ί linux β€Ί man-pages β€Ί man3 β€Ί memcpy.3.html
memcpy(3) - Linux manual page
For an explanation of the terms used in this section, see attributes(7). β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Interface β”‚ Attribute β”‚ Value β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ memcpy() β”‚ Thread safety β”‚ MT-Safe β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Β· C11, POSIX.1-2008.
Find elsewhere
🌐
Scaler
scaler.com β€Ί home β€Ί topics β€Ί memcpy in c
memcpy() in C
April 14, 2024 - The memcpy function in C is used to copy a specific memory size from a source location to the destination location. It copies the data directly from the source memory to the destination memory.
🌐
KooR
koor.fr β€Ί C β€Ί cstring β€Ί memcpy.wp
KooR.fr - memcpy - Langage C
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int array [] = { 54, 85, 20, 63, 21 }; int * copy = NULL; int length = sizeof( int ) * 5; /* Memory allocation and copy */ copy = (int *) malloc( length ); memcpy( copy, array, length ); /* Display the copied values */ for( length=0; length<5; length++ ) { printf( "%d ", copy[ length ] ); } printf( "\n" ); free( copy ); return EXIT_SUCCESS; }
🌐
GitHub
github.com β€Ί gcc-mirror β€Ί gcc β€Ί blob β€Ί master β€Ί libgcc β€Ί memcpy.c
gcc/libgcc/memcpy.c at master Β· gcc-mirror/gcc
#include <stddef.h> Β· void * memcpy (void *dest, const void *src, size_t len) { char *d = dest; const char *s = src; while (len--) *d++ = *s++; return dest; }
Author Β  gcc-mirror
🌐
Linux Hint
linuxhint.com β€Ί memcpy_function_c_programming
How to use memcpy function in C language? – Linux Hint
First, we copied 6 characters β€˜H’, ... second memcpy() function copied 8 characters β€˜ ’, β€˜w’, β€˜o’, β€˜r’, β€˜l’, β€˜d’, β€˜!’, β€˜\0’ to the dest after 5 characters ( Line 15 ). Pictorially we can represent this as follows: //Example2.c #include<stdio.h> ...
🌐
TutorialsPoint
tutorialspoint.com β€Ί memcpy-function-in-c-cplusplus
memcpy() function in C/C++
June 26, 2020 - The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in β€œstring.h” header file in C language. It does not check overflow. ... #include <stdio.h> #include <string.h> int main () ...
🌐
University of Helsinki
cs.helsinki.fi β€Ί group β€Ί boi2016 β€Ί doc β€Ί cppreference β€Ί reference β€Ί en.cppreference.com β€Ί w β€Ί c β€Ί string β€Ί byte β€Ί memcpy.html
memcpy, memcpy_s - cppreference.com
Where strict aliasing prohibits examining the same memory as values of two different types, memcpy may be used to convert the values. ... #define __STDC_WANT_LIB_EXT1__ 1 #include <stdio.h> #include <stdint.h> #include <inttypes.h> #include <string.h> #include <stdlib.h> int main(void) { // simple usage char source[] = "once upon a midnight dreary...", dest[4]; memcpy(dest, source, sizeof dest); for(size_t n = 0; n < sizeof dest; ++n) putchar(dest[n]); // reinterpreting data double d = 0.1; // int64_t n = *(int64_t*)(&d); // strict aliasing violation int64_t n; memcpy(&n, &d, sizeof d); // OK
🌐
W3Resource
w3resource.com β€Ί c-programming β€Ί string β€Ί c-memcpy.php
C memcpy() function
2 weeks ago - This example demonstrates memcpy() by copying part of a string over itself. Starting from index 3, it copies 5 characters from the beginning, which results in an overlapping copy within the same string. ... #include <stdio.h> #include <string.h> char str1[9] = "AAABBCCD"; int main( void ) { printf( "Original string: %s\n", str1 ); memcpy( str1 + 3, str1, 5 ); printf( "New string: %s\n", str1 ); }
🌐
Aticleworld
aticleworld.com β€Ί home β€Ί implementation of memcpy in c language
Implementation of memcpy in C language - Aticleworld
December 11, 2023 - In the above code, you will not get the compile-time error but when you run the program you will get the undefined behavior because the source pointer is not pointing to valid memory. ... 9.The pointers have been declared void * for source and destination buffers so that memcpy may be used for any data type. The following program explains the working of memcpy() with different data types. ... #include <stdio.h> #include <string.h> int main() { //Source buffer char src[20] = "Hi Aticleworld"; //dst buffer char dst[20] = {0}; //copy source buffer int dst memcpy(dst,src,sizeof(src)); //printing dst buffer printf("dst = %s\n", dst); return 0; }
🌐
Microsoft Learn
learn.microsoft.com β€Ί en-us β€Ί cpp β€Ί c-runtime-library β€Ί reference β€Ί memcpy-s-wmemcpy-s
memcpy_s, wmemcpy_s | Microsoft Learn
December 2, 2022 - // crt_memcpy_s.c // Copy memory in a more secure way. #include <memory.h> #include <stdio.h> int main() { int a1[10], a2[100], i; errno_t err; // Populate a2 with squares of integers for (i = 0; i < 100; i++) { a2[i] = i*i; } // Tell memcpy_s to copy 10 ints (40 bytes), giving // the size of the a1 array (also 40 bytes).
🌐
Cprogramming
cboard.cprogramming.com β€Ί c-programming β€Ί 141613-memcpy.html
memcpy
October 2, 2011 - Last edited by CommonTater; 10-02-2011 at 07:28 PM. ... I have read the documentation but i still need some example, such as, let say i want to memcpy the JPEG picture in My Documents, and i want to put the JPEG into a buffer[200], what should i put in the argument? Refer below: ... /* memcpy example */ #include <stdio.h> #include <string.h> int main () { FILE* pFile //how the program wanna fine the file ??