sizeof(a) is the total size of the array a. For example, in

int a[3];
printf("%d", sizeof(a));

sizeof a will be 12 on most systems (since int is usually 4 bytes and you have 3 of them).

memcpy doesn't know that a is an array. All it knows is that a points to some memory address, so you have to tell it how many bytes to pass. Most places describe memcpy's signature as:

void *memcpy(void *dst, const void *src, size_t nbytes)
Answer from SheetJS on Stack Overflow
🌐
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.
🌐
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.
Discussions

c - Using 'memcpy' function - Stack Overflow
There be dragons. memcpy needs to know the length of the data -- that's not available at runtime by querying the first two parameters. But get the length wrong and you write all over memory and things go south real fast. ... You can make your own memory allocator, which will allocate some structure ... More on stackoverflow.com
🌐 stackoverflow.com
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?
Find answers to What is memcpy in c? from the expert community at Experts Exchange More on experts-exchange.com
🌐 experts-exchange.com
September 8, 2022
c++ - What C library provides memcpy? - Stack Overflow
How to figure out what gcc library provides the symbol for memcpy? memcpy is provided by the header file , but I don't know what library provides the symbol. $ objdump -ax libboost_filesystem.so | ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
cppreference.com
en.cppreference.com › cpp › string › byte › memcpy
std::memcpy - cppreference.com
February 9, 2025 - #include <cstdint> #include <cstring> #include <iostream> int main() { // simple usage char source[] = "once upon a daydream...", dest[4]; std::memcpy(dest, source, sizeof dest); std::cout << "dest[4] = {"; for (int n{}; char c : dest) std::cout << (n++ ?
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009696899 › functions › memcpy.html
memcpy
Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard. The memcpy() function shall copy n bytes from the object pointed to by s2 into the object pointed to by s1.
🌐
Cplusplus
cplusplus.com › reference › cstring › memcpy
memcpy
<cstring> memcpy · function · <cstring> void * memcpy ( void * destination, const void * source, size_t num ); Copy block of memory · Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › memcpy.3.html
memcpy(3) - Linux manual page
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › memcpy-wmemcpy
memcpy, wmemcpy | Microsoft Learn
March 28, 2024 - Make sure that the destination buffer is large enough to accommodate the number of copied characters. For more information, see Avoiding buffer overruns. ... Because so many buffer overruns, and thus potential security exploits, have been traced to improper usage of memcpy, this function is ...
🌐
Medium
medium.com › @caruychen_48871 › the-curious-case-of-memcpy-bd93936e5136
The curious case of memcpy()
December 9, 2021 - The curious case of memcpy() There’s a deceptively simple function in the standard C library called memcpy. However, the simplest of functions can sometimes offer interesting insights into data …
🌐
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.
🌐
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.
🌐
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 - Understand the challenges and security concerns around real-time operating systems (RTOS). ... memcpy() is a standard function used in the C programming language to copy blocks of memory from one place to another.
🌐
Sub-Etha Software
subethasoftware.com › 2019 › 08 › 09 › c-and-the-dangers-of-memcpy
C and the dangers of memcpy() | Sub-Etha Software
August 12, 2019 - Updates: 2019-08-12 – Added note about using unions. In the C programming language, memcpy (memory copy) is a function used to copy a range of bytes from one location in memory to another. I …
🌐
Experts Exchange
experts-exchange.com › questions › 29246258 › What-is-memcpy-in-c.html
Solved: What is memcpy in c? | Experts Exchange
September 8, 2022 - C was a long time ago for some of us... 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.
Top answer
1 of 3
3

memcpy is specified in the C standard as well as the POSIX standard, as well as a few other operating system specifications. Hence, it is provided by the C standard and/or the operating system library implementation.

The default standard library on a GNU system is the GNU C library aka glibc.

How do I go about getting this information?

Search engines are good for getting information in general, including this one. Searching for memcpy should lead you to the information about what it is (a standard library function), and searching for your system's documentation should lead you to the information about its standard library implementation.

The objdump command that you demonstrated seems to also have shown you the answer.

I'm trying to understand what is the file name in my system.

There's no standard way that applies to all systems. But let's assume that your operating system is Linux. The manual says

The pathname /lib/libc.so.6 (or something similar) is normally a symbolic link that points to the location of the glibc library, and executing this pathname will cause glibc to display various information about the version installed on your system.

You can use ldconfig -p | grep libc.so to find the precise path.

2 of 3
2

what gcc library provides the symbol for memcpy?

The C standard library provides memcpy.

There are some popular implementations of C standard library, on Linux it is most notably glibc (well, and musl on Alpine Linux).

How do I go about getting this information?

There are some approaches you can take. You can run something and see where it links to How to find which shared library exported which imported symbol in my binary? . You can index all libraries on your system (see man ld.so and /etc/ld.so.conf) and index all symbols in those libraries and query the symbol (this my script does that). Then you can query your system package manager to find out to which package the shared library belongs to.

🌐
Linux Man Pages
linux.die.net › man › 3 › memcpy
memcpy(3): copy memory area - Linux man page
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re173.html
memcpy - C in a Nutshell [Book]
December 16, 2005 - typedef struct record { char name[32]; double data; struct record *next, *prev; } Rec_t; Rec_t template = { "Another fine product", -0.0, NULL, NULL }; Rec_t *tmp_new; if (( tmp_new = malloc( sizeof(Rec_t) )) != NULL )memcpy( tmp_new, &template, sizeof(Rec_t) ); else fprintf( stderr, "Out of memory!\n" ), return -1;
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
W3Schools
w3schools.com › cpp › ref_cstring_memcpy.asp
C++ cstring memcpy() Function
The memcpy() function copies data from one block of memory to another.