Questions tagged [memcpy]

memcpy() is a C standard library function used for copying a block of memory bytes from one place to another.

Synopsis

#include <string.h>

void *memcpy(void * restrict s1,
     const void * restrict s2,
     size_t n);

Description
The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

Returns
The memcpy function returns the value of s1.

1578 questions
15
votes
2 answers

copy_to_user vs memcpy

I have always been told(In books and tutorials) that while copying data from kernel space to user space, we should use copy_to_user() and using memcpy() would cause problems to the system. Recently by mistake i have used memcpy() and it worked…
Sandeep
  • 18,356
  • 16
  • 68
  • 108
15
votes
6 answers

'memdup' function in C?

In C, you can use strdup to succinctly allocate a buffer and copy a string into it. As far as I'm aware, however, there is no similar function for general memory. For example, I can't say struct myStruct *foo = malloc(sizeof(struct…
Dan
  • 2,952
  • 4
  • 23
  • 29
14
votes
1 answer

how to use movntdqa to avoid cache pollution?

i am trying to write a memcpy function that does not load the source memory to the cpu cache. The purpose is to avoid cache pollution. The memcpy function below works, but pollutes the cache like the standard memcpy does. i am using P8700…
yigal
  • 3,923
  • 8
  • 37
  • 59
14
votes
3 answers

Put bytes from unsigned char array to std::string using memcpy() function

I have std::string variable. And I need to put some bytes from array of unsigned chars to it. I know the first byte and the the legth. I can use the std::string::assign function. I've done it. But I want to solve that issue in a right way using the…
Oleksandr Balabanov
  • 629
  • 1
  • 6
  • 30
14
votes
3 answers

memcpy of overlapping buffers

I ran into strange behavior when using the Aztec linear system solver library. Using valgrind, I found out that this library does a memcpy on overlapping buffers. Specification says that behavior of memcpy on overlapping buffers is not defined. It…
Michael
  • 7,407
  • 8
  • 41
  • 84
14
votes
2 answers

Understanding the implementation of memcpy()

I was looking the implementation of memcpy.c, I found a different memcpy code. I couldnt understand why do they do (((ADDRESS) s) | ((ADDRESS) d) | c) & (sizeof(UINT) - 1) #if !defined(__MACHDEP_MEMFUNC) #ifdef _MSC_VER #pragma…
Angus
  • 12,133
  • 29
  • 96
  • 151
14
votes
6 answers

Fastest de-interleave operation in C?

I have a pointer to an array of bytes mixed that contains the interleaved bytes of two distinct arrays array1 and array2. Say mixed looks something like this: a1b2c3d4... What I need to do is de-interleave the bytes so I get array1 = abcd... and…
Anton
  • 4,554
  • 2
  • 37
  • 60
13
votes
5 answers

Getting GCC to compile without inserting call to memcpy

I'm currently using GCC 4.5.3, compiled for PowerPC 440, and am compiling some code that doesn't require libc. I don't have any direct calls to memcpy(), but the compiler seems to be inserting one during the build. There are linker options like…
Brian
  • 133
  • 1
  • 1
  • 4
13
votes
2 answers

Signedness aliasing using reinterpret_cast

Take the following code #include void func() { int i = 2147483640; while (i < i + 1) { std::cerr << i << '\n'; ++i; } return; } int main() { func(); } This code is clearly wrong, as the while…
Jonas Müller
  • 317
  • 1
  • 8
13
votes
3 answers

What is the difference between memcpy() and strncpy() given the latter can easily be a substitute for the former?

What is the significant difference between memcpy() and strncpy()? I ask this because we can easily alter strncpy() to copy any type of data we want, not just characters, simply by casting the first two non-char* arguments to char* and altering the…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
13
votes
3 answers

C: memcpy speed on dynamically allocated arrays

I need help with the performance of the following code. It does a memcpy on two dynamically allocated arrays of arbitrary size: int main() { double *a, *b; unsigned n = 10000000, i; a = malloc(n*sizeof(double)); b =…
angainor
  • 11,760
  • 2
  • 36
  • 56
13
votes
4 answers

How to prevent memcpy buffer overflow?

There are some binary buffers with fixed sizes in a program that are used to store data, and memcpy is used to copy the buffer from one to another one. Since the source buffer may be larger than the destination buffer, how can I detect if there is…
Michael D
  • 1,449
  • 5
  • 18
  • 31
12
votes
4 answers

memcpy and pointers

I am confuse on how to read the pointers copied in an array using memcpy. Following is what I have tried, but does not work. Basically, I have allocated block of memory in which I am copying pointers similar to array fashion, but during retrial it…
Avinash
  • 12,851
  • 32
  • 116
  • 186
12
votes
2 answers

Is it technically impossible to implement memcpy from scratch in Standard C?

Howard Chu writes: In the latest C spec it is impossible to write a "legal" implementation of malloc or memcpy. Is this right? My impression is that in the past, the intent (at least) of the standard was that something like this would work: void…
Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96
12
votes
5 answers

C++ memcpy from double array to float array

Is is possible to memcpy from a double array to a float array safely?
dmessf
  • 1,025
  • 3
  • 11
  • 19