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 Overflowc - Using 'memcpy' function - Stack Overflow
How does memcpy( ) work in C lang?
What is memcpy in c?
c++ - What C library provides memcpy? - Stack Overflow
Videos
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)
#include <string.h>
void *
memcpy(void *s1, const void *s2, register size_t n)
{
register char *p1 = s1;
register const char *p2 = s2;
if(n) {
n++;
while (--n > 0) {
*p1++ = *p2++;
}
}
return s1;
}
Here is a source code for memcpy. As you can see it literally moves through each element from register 1 and moves it into register 2 one by one.
size of is used so it knows how big the register is so basically how many elements it must skip through before it has copied the whole register.
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!
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.
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.
Hi all.
I recently got told to use memcpy instead of strcpy (in multithreaded applications)
Reason being: “strcpy copys byte by byte where as memcpy will copy all at once. & if the variable changes in another thread it can copy wrong & cause issues”?
That doesnt really make sense to me, is it true?