I couldn't understand if part they do for integers. i < len/sizeof(long). Why is this calculation required ?

Because they are copying words, not individual bytes, in this case (as the comment says, it is an optimization - it requires less iterations and the CPU can handle word aligned data more efficiently).

len is the number of bytes to copy, and sizeof(long) is the size of a single word, so the number of elements to copy (means, loop iterations to execute) is len / sizeof(long).

Answer from Andreas Fester on Stack Overflow
🌐
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.
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
What is memcpy in c?
Memcpy vs memmove in c | memmove implementation | own code More on experts-exchange.com
🌐 experts-exchange.com
September 8, 2022
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
Implementing my own memcpy - Post.Byes
If so, then don't allocate memory - memcpy requires that the memory is already allocated. The second parameter needs to be qualified with const. Also the type of the third parameter is size_t not int - this requires that you include stddef.h. Even if it were declared as int it should be unsigned. Here is an implementation... More on post.bytes.com
🌐 post.bytes.com
November 15, 2005
🌐
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.
🌐
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 ...
🌐
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.
🌐
Linux Man Pages
man7.org β€Ί linux β€Ί man-pages β€Ί man3 β€Ί memcpy.3.html
memcpy(3) - Linux manual page
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Interface β”‚ Attribute β”‚ Value β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ memcpy() β”‚ Thread safety β”‚ MT-Safe β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Β· C11, POSIX.1-2008.
Find elsewhere
🌐
Experts Exchange
experts-exchange.com β€Ί questions β€Ί 29246258 β€Ί What-is-memcpy-in-c.html
Solved: What is memcpy in c? | Experts Exchange
September 8, 2022 - But as I recall, memcpy() copies a block of data from one memory location to another. I always thought of it as a fairly "raw" way to move data around in memory, but it can be fast since it is doing very little type manipulation or error checking.
🌐
Post.Byes
post.bytes.com β€Ί home β€Ί forum β€Ί topic
Implementing my own memcpy - Post.Byes
November 15, 2005 - If so, then don't allocate memory - memcpy requires that the memory is already allocated. The second parameter needs to be qualified with const. Also the type of the third parameter is size_t not int - this requires that you include stddef.h. Even if it were declared as int it should be unsigned. Here is an implementation: #include <stddef.h> void *mem_cpy(void *dest, const void *src, size_t bytes) { unsigned char *srcmax = dest + bytes; while (src < srcmax) *(unsigned char *)dest++ = *(unsigned char *)src++; return dest; }
🌐
musl
git.musl-libc.org β€Ί cgit β€Ί musl β€Ί tree β€Ί src β€Ί string β€Ί memcpy.c
memcpy.c\string\src - musl - musl - an implementation of the standard library for Linux-based systems
path: root/src/string/memcpy.c Β· blob: 06e88742b1144a3b770184cc34dc14d96bdd2134 (plain) (blame) Β· generated by cgit v1.2.1 (git 2.18.0) at 2026-02-17 08:02:56 +0000
🌐
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.
🌐
Equestionanswers
equestionanswers.com β€Ί c β€Ί memcpy-vs-memmove.php
Memcpy vs memmove in c | memmove implementation | own code
Applications which might deal with dst and src might overlap should use memmove(3) instead. RETURN VALUE The memcpy() function returns the original value of dst. memcpy() is generally used to copy a portion of memory chuck from one location to another location.
🌐
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] =
🌐
freebasic.net
freebasic.net β€Ί board index β€Ί programming β€Ί general
memcpy function location - freebasic.net
I wonder if the current implementation is a byte per byte copy. Or if it is actually advanced enough to copy bigger blocks at a time if it can. Are there other memcpy functions out there than the crt's? When FreeBASIC translates to C, does it use the crt library? ... Provoni wrote: ↑Feb 26, 2022 13:09 When FreeBASIC translates to C, does it use the crt library? Of course it uses CRT, you can find crt.bi in the include folder
🌐
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.
🌐
Embedded
embedded.com β€Ί home β€Ί optimizing memcpy improves speed
Optimizing Memcpy improves speed - Embedded
April 29, 2004 - ... void * memcpy(void * dst, void const * src, size_t len) { char * pDst = (char *) dst; char const * pSrc = (char const *) src; ... An algorithm that offers better performance on wider memory buses, such as the one on the evaluation board ...
🌐
SAS
support.sas.com β€Ί documentation β€Ί onlinedoc β€Ί sasc β€Ί doc700 β€Ί html β€Ί lr1 β€Ί z2055843.htm
Function Descriptions : memcpy
The third argument to memcpy is size_t . If a negative number is passed, overlaying of memory may occur. The compiler generates inline code for memcpy unless memcpy is undefined (by an #undef statement) to prevent this.
🌐
Hacker News
news.ycombinator.com β€Ί item
Making memcpy(NULL, NULL, 0) well-defined | Hacker News
December 19, 2024 - https://godbolt.org/z/aPcr1bfPe Β· I think the blog should mention `-fno-delete-null-pointer-checks`
🌐
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 ...
🌐
Cprogramming
cboard.cprogramming.com β€Ί c-programming β€Ί 177774-memcpy-its-derivatives.html
memcpy and its derivatives
Yes, as of C11, memcpy_s part of the standard. I doubt you want to implement your own version. It is conceptually quite simple, but several compilers resort to inline assembler to implement memcpy most efficiently for a given CPU. If your concern is about safely implementing memcpy in a system without memcpy_s, what seems better is to consider a function that first tests all that is required before calling memcpy.