memcpy() does not copy data to a temporary location: that's why memmove() and memcpy() are different functions, memmove() copies to a temporary location, memcpy() doesn't. (Well, memcpy() will copy to a register, but that doesn't really count) What compiler and libc are you using? What is the nature of 0x7C000? Is it "real" memory, or is it a mmio device? glibc on desktop will do virtual address magic to delay actually performing a copy, but this doesn't work for mmio devices. This code path should probably be disabled in embedded environments. What is the declaration of sample_waveform? double sample_waveform[384]; is much different from double* sample_waveform; /* remember to 'sample_waveform = malloc(3072);' in init() */ Does it work if you do memcpy(sample_waveform, (void*)0x7C000, sizeof(*sample_waveform) * NUMBER_OF_ELEMENTS);? (where NUMBER_OF_ELEMENTS is 384 for a 3kB array of doubles, obviously) Does the source memory address have limitations about what order you're allowed to read it in? It's not defined whether memcpy() will read front to back, or back to front, or in a seemingly nonsensical order. Does stepping through it in the debugger yield any surprises? If you roll your own memcpy() does it work? Side note: void xorSwap (int* x, int* y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } Don't do that. Don't ever do that. It's measurably slower than doing it the naive way with a "temporary variable" that will be optimized out by the compiler. xor swap is a waste of both time and memory. Your code is bad and you should feel bad. If you use xor swap in production you do not deserve love. Answer from pigeon768 on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › memcpy-in-cc
memcpy() in C - GeeksforGeeks
September 22, 2025 - The memcpy function in C copies the specified number of bytes from one memory location to another memory location regardless of the type of data stored. Where both source and destination are raw memory addresses.
🌐
Aticleworld
aticleworld.com › home › implementation of memcpy in c language
Implementation of memcpy in C language - Aticleworld
December 11, 2023 - The memcpy function copies n characters from the source object to the destination object. If the source and destination objects overlap, the behavior of memcpy is undefined. In memcpy, we need to pass the address of the source and destination ...
Discussions

How does memcpy( ) work in C lang?
memcpy() does not copy data to a temporary location: that's why memmove() and memcpy() are different functions, memmove() copies to a temporary location, memcpy() doesn't. (Well, memcpy() will copy to a register, but that doesn't really count) What compiler and libc are you using? What is the nature of 0x7C000? Is it "real" memory, or is it a mmio device? glibc on desktop will do virtual address magic to delay actually performing a copy, but this doesn't work for mmio devices. This code path should probably be disabled in embedded environments. What is the declaration of sample_waveform? double sample_waveform[384]; is much different from double* sample_waveform; /* remember to 'sample_waveform = malloc(3072);' in init() */ Does it work if you do memcpy(sample_waveform, (void*)0x7C000, sizeof(*sample_waveform) * NUMBER_OF_ELEMENTS);? (where NUMBER_OF_ELEMENTS is 384 for a 3kB array of doubles, obviously) Does the source memory address have limitations about what order you're allowed to read it in? It's not defined whether memcpy() will read front to back, or back to front, or in a seemingly nonsensical order. Does stepping through it in the debugger yield any surprises? If you roll your own memcpy() does it work? Side note: void xorSwap (int* x, int* y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } Don't do that. Don't ever do that. It's measurably slower than doing it the naive way with a "temporary variable" that will be optimized out by the compiler. xor swap is a waste of both time and memory. Your code is bad and you should feel bad. If you use xor swap in production you do not deserve love. More on reddit.com
🌐 r/C_Programming
50
43
October 16, 2020
c - Understanding the source code of memcpy() - Stack Overflow
I was just going through an implementation of memcpy, to understand how it differs from using a loop. But I couldn't see any difference between using a loop rather than memcpy, as memcpy uses loop again internally to copy. More on stackoverflow.com
🌐 stackoverflow.com
c - memcpy() implementation - Code Review Stack Exchange
I have tried to write a function like memcpy. It copies sizeof(long) bytes at a time. What surprised me is how inefficient it is. It's just 17% more efficient than the naivest implementation with -O3. More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
February 6, 2014
c - How does the internal implementation of memcpy work? - Stack Overflow
Without reference to a specific implementation, it's effectively a byte-for-byte copy with a one-place buffer. Here's a fun article that describes one person's adventure into optimizing memcpy. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @caruychen_48871 › the-curious-case-of-memcpy-bd93936e5136
The curious case of memcpy()
December 9, 2021 - If the remaining trail of data after copying in long data chunks, copy data in incrementally smaller sizes from int, short, then char. With this knowledge, we can implement a version of memcpy that can be up to eight times faster than the simplest approach above.
🌐
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; }
🌐
Reddit
reddit.com › r/c_programming › how does memcpy( ) work in c lang?
r/C_Programming on Reddit: How does memcpy( ) work in C lang?
October 16, 2020 -

Long time ago when i was a wee little lad learning C, before I was learning pointers, I learned how to swap values between two variables ;

uint32_t x=1;
uint32_t y=2;
uint32_t tmp;

tmp = y;
y=x;
x=tmp;

Of-course, with pointers, we can do ;

void xorSwap (int* x, int* y) {
    if (x != y) {
       *x ^= *y;
       *y ^= *x;
       *x ^= *y;
    }
}

Yesterday at work, we write bare metal C for fw i had this uniquely weird issue, we work in RISC V environment,

where i have to use memcpy() something similar to a

memcpy( uint32_t * dest, const uint32_t * source, size_t sz );

and the issue was that, in the linker I had specified maximum buffer size too, so it should've worked.

This below is actual code:

memcpy(sample_waveform, (void *)(0x7C000), sizeof(sample_waveform));

and when i had sample_waveform which was a signed double GLOBAL array of 3K bytes, it won't copy, However it did was able to copy first 500 bytes though when I made the sample_waveform size smaller.

but when I made sample_waveform local on the stack which took a stack space of 3K, I was able to do memcpy( ).

I can't explain why this is so? I didn't want to copy and take/use that much space on the stack.

Has this happened to anyone?

How does memcpy( ) work? Does it copy to temp somewhere and then copy to your destination, or does it copy like that pointer method. How is this different from memmove()?

Thanks!

Top answer
1 of 8
11
memcpy() does not copy data to a temporary location: that's why memmove() and memcpy() are different functions, memmove() copies to a temporary location, memcpy() doesn't. (Well, memcpy() will copy to a register, but that doesn't really count) What compiler and libc are you using? What is the nature of 0x7C000? Is it "real" memory, or is it a mmio device? glibc on desktop will do virtual address magic to delay actually performing a copy, but this doesn't work for mmio devices. This code path should probably be disabled in embedded environments. What is the declaration of sample_waveform? double sample_waveform[384]; is much different from double* sample_waveform; /* remember to 'sample_waveform = malloc(3072);' in init() */ Does it work if you do memcpy(sample_waveform, (void*)0x7C000, sizeof(*sample_waveform) * NUMBER_OF_ELEMENTS);? (where NUMBER_OF_ELEMENTS is 384 for a 3kB array of doubles, obviously) Does the source memory address have limitations about what order you're allowed to read it in? It's not defined whether memcpy() will read front to back, or back to front, or in a seemingly nonsensical order. Does stepping through it in the debugger yield any surprises? If you roll your own memcpy() does it work? Side note: void xorSwap (int* x, int* y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } Don't do that. Don't ever do that. It's measurably slower than doing it the naive way with a "temporary variable" that will be optimized out by the compiler. xor swap is a waste of both time and memory. Your code is bad and you should feel bad. If you use xor swap in production you do not deserve love.
2 of 8
6
sizeof(sample_waveform) returns the size of the first element of the array pointer ( u/OldWolf2 corrected this). So you will be copying the first 8 bytes size of a pointer and that is it. If you know the size of the array (in bytes) use this value instead. EDIT: Ok, I made the assumption that sample_waveform is a pointer and you allocated it by malloc, if the array is static, i.e, know at compile time sizeof(sample_waveform) should give you the array size. EDIT2: Check u/OldWolf2 comment bellow, he got it right.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_memcpy.htm
C library - memcpy() function
The C library function memcpy() uses three parameters− destination string(dest), source string(src), and strlen() function where it calculates the length of the source string and the number of bytes to be copied.
Find elsewhere
🌐
w3resource
w3resource.com › c-programming-exercises › c-snippets › implementing-custom-memcpy-function-in-c.php
C - Implementing a custom memcpy() function
November 1, 2025 - If copying takes place between objects that overlap, the behaviour is undefined. ... # include <stdio.h> void *my_memcpy(void *dest, const void *src, size_t n) { char *cdest = (char *)dest; const char *csrc = (const char *)src; for (size_t i = 0; i < n; i++) { cdest[i] = csrc[i]; } return dest; } int main() { char src[] = "C language snippets."; char dest[20]; my_memcpy(dest, src, sizeof(src)); printf("Copied string is: %s\n", dest); return 0; }
🌐
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 - The memcpy() function copies the contents of a source buffer to a destination buffer, starting from the memory location pointed to by src, and continuing for n bytes. The areas of memory should not overlap, and the behavior is undefined if they do.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › write-memcpy
Write your own memcpy() and memmove() - GeeksforGeeks
February 18, 2026 - // Sample program to show that memmove() is better than memcpy() // when addresses overlap. #include <stdio.h> #include <string.h> int main() { char csrc[100] = "Geeksfor"; memmove(csrc+5, csrc, strlen(csrc)+1); printf("%s", csrc); return 0; } Output: GeeksGeeksfor · While a common trick is to use a temp array instead of directly copying from src to dest to handle overlapping addresses, this is inefficient. A professional implementation avoids double copies by picking the direction of the copy based on the relative positions of src and dest.
🌐
Scaler
scaler.com › home › topics › memcpy in c
memcpy() in C
April 14, 2024 - In the C Programming Language, the memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. It returns a pointer to the destination.
🌐
cppreference.com
en.cppreference.com › c › string › byte › memcpy
memcpy, memcpy_s - cppreference.com
November 5, 2020 - 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] =
🌐
Medium
medium.com › @future_fanatic › memcpy-in-c-programming-8000753463e1
memcpy() in C Programming. In the realm of C programming… | by Future Fanatic | Medium
April 23, 2024 - At its core, memcpy() facilitates the seamless transfer of data from one memory location to another. Its versatility extends across all data types, making it an indispensable asset for copying arrays, structures, and more with precision and speed.
🌐
TutorialsPoint
tutorialspoint.com › write-your-own-memcpy-in-c
Write your own memcpy() in C
July 30, 2019 - To make our own memcpy, we have to typecast the given address to char*, then copy data from source to destination byte by byte. Just go through the following code to get better idea. #include<stdio.h> #include<string.h> void custom_memcpy(void *dest, void *src, size_t n) { int i; //cast src ...
🌐
IncludeHelp
includehelp.com › c-programs › write-your-own-memcpy-function-in-c.aspx
Write your own memcpy() function in C
December 6, 2018 - #include <stdio.h> #include <string.h> #define MAXLEN 11 //function to print array void printArray(unsigned char str[], int length){ int i; for(i=0; i<length;i++) printf("X ", str[i]); printf("\n"); } //memcpy() implementation, name: myMemCpy() void myMemCpy(void* target, void* source, size_t n){ int i; //declare string and type casting char *t = (char*)target; char *s = (char*)source; //copying "n" bytes of source to target for(i=0;i<n;i++) t[i]=s[i]; } int main(){ unsigned char arr1[MAXLEN] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0x95}; unsigned char arr2[MAXLEN] = {0}; pri
🌐
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
🌐
Wordpress
jlmedina123.wordpress.com › 2014 › 03 › 11 › memcpy-implementation
memcpy implementation | Learning C by example
March 19, 2015 - The first one is the trivial implementation. The second one improves security by avoiding overwriting memory from the pointers. The third ones improves performance by copying one word at a time, instead of 1 bytes at a time. The fourth one copies a word at a time, and start copying with destination aligned to word byte boundary.
🌐
Naukri
naukri.com › code360 › library › memcpy-c
memcpy() in C - Naukri Code 360
September 16, 2024 - Almost there... just a few more seconds
Top answer
1 of 3
29

Depends. In general, you couldn't physically copy anything larger than the largest usable register in a single cycle, but that's not really how machines work these days. In practice, you really care less about what the CPU is doing and more about the characteristics of DRAM. The memory hierarchy of the machine is going to play a crucial determining role in performing this copy in the fastest possible manner (e.g., are you loading whole cache-lines? What's the size of a DRAM row with respect to the copy operation?). An implementation might instead choose to use some kind of vector instructions to implement memcpy. Without reference to a specific implementation, it's effectively a byte-for-byte copy with a one-place buffer.

Here's a fun article that describes one person's adventure into optimizing memcpy. The main take-home point is that it is always going to be targeted to a specific architecture and environment based on the instructions you can execute inexpensively.

2 of 3
15

The implementation of memcpy is highly specific to the system in which it is implemented. Implementations are often hardware-assisted.

Memory-to-memory mov instructions are not that uncommon - they have been around since at least PDP-11 times, when you could write something like this:

    MOV FROM, R2
    MOV TO,   R3
    MOV R2,   R4
    ADD LEN,  R4
CP: MOV (R2+), (R3+) ; "(Rx+)" means "*Rx++" in C
    CMP R2, R4
    BNE CP

The commented line is roughly equivalent to C's

*to++ = *from++;

Contemporary CPUs have instructions that implement memcpy directly: you load special registers with the source and destination addresses, invoke a memory copy command, and let CPU do the rest.