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
6
votes
1 answer

Speed of memcpy() greatly influenced by different ways of malloc()

I wrote a program to test the speed of memcpy(). However, how memory are allocated greatly influences the speed. CODE #include #include #include void main(int argc, char *argv[]){ unsigned char * pbuff_1; …
foool
  • 1,462
  • 1
  • 15
  • 29
6
votes
4 answers

memcpy a buffer and an array not working

I have a requirement in which i need to pass an empty array as a parameter to a function. And in this called function, i should be memcpy some data into the passed array. So i have written a small example which is same as my requirement. Below is…
Zax
  • 2,870
  • 7
  • 52
  • 76
6
votes
3 answers

Why does memcpy fail copying to a local array member of a simple object?

Classic memcpy gotcha with C arrays as function arguments. As pointed out below, I have an error in my code but the erroneous code worked in a local context! I just encountered this weird behaviour in a porting job, where I'm emulating the Macintosh…
Andy Dent
  • 17,578
  • 6
  • 88
  • 115
6
votes
3 answers

C Valgrind - Source and destination overlap in memcpy

I'm new to c programming and I'm writing a simple client server application. I get this message: Source and destination overlap in memcpy(0x41f0beb, 0x41f0258, 69141077) ==9522== at 0x402D9A9: memcpy (in…
User Conscious
  • 105
  • 1
  • 2
  • 8
6
votes
2 answers

Better or the same: CPU memcpy() vs device cudaMemcpy() on pinned, mapped memory in CUDA?

I have: Host memory that has been successfully pinned and mapped using cudaHostAlloc(..., cudaHostAllocMapped) or cudaHostRegister(..., cudaHostRegisterMapped); Device pointers have been obtained using cudaHostGetDevicePointer(...). I initiate…
mikepcw
  • 153
  • 1
  • 8
5
votes
1 answer

Why does memcpy fail to copy Eigen matrix data, but std::copy succeed?

When I create a matrix using Eigen, like this: Eigen::MatrixXd M(3,3); M<< 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 ; std::cout<
Alec Jacobson
  • 6,032
  • 5
  • 51
  • 88
5
votes
2 answers

Create an object in memory pointed to by a void pointer

If I have a void* to some chunk of free memory and I know there is at least sizeof(T) available, is there any way to create an object of type T in that location in memory? I was just going to create a T object on the stack and memcpy it over, but it…
wallacer
  • 12,855
  • 3
  • 27
  • 45
5
votes
2 answers

Faster memcpy assuming readability and writability of bytes past source and destination buffers

Assume we want to copy n bytes of data from void* src to void* dst. It is well-known that standard library implementation of memcpy is heavily optimized for using platform-dependent vectorized instructions and various other tricks to perform copying…
Maxim Akhmedov
  • 679
  • 5
  • 10
5
votes
1 answer

Improving performance of generic swap

Context When implementing generic functions in C that work with a range of types, void* is regularly used. The libc function qsort() is a classic example. Internally qsort() and many other algorithms require a swap() function. A simple but typical…
Oliver Schönrock
  • 1,038
  • 6
  • 11
5
votes
4 answers

Is using std::memcpy on a whole union guaranteed to preserve the active union member?

In C++, it's well-defined to read from an union member which was most recently written, aka the active union member. My question is whether std::memcpying a whole union object, as opposed to copying a particular union member, to an uninitialized…
user10015341
5
votes
0 answers

When a union object is copied, is a member subobject created?

When another member of a union is accessed, the C++ standard used to be silent on what happens, but that was fixed to explain that member access to a union object was allowed for the purpose of assigning to that yet no existent object, which would…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
5
votes
3 answers

DMA transfer taking more time than CPU transfer

Our task is intended to demonstrate the benefit of using DMA to copy a large amount of data versus relying on the processor to directly handle the copying. The processor is an STM32F407 on the ST discovery board. In order to measure the copying…
Joe P
  • 79
  • 4
5
votes
5 answers

Is the memcpy() function reentrant?

I call some C++ functions inside a signal handler and my program is terminated by segmentation fault. When I check with gdb, memcpy() function is where i get SIGSEGV. I would like to know if memcpy() is a reentrant function or not?
korhan
  • 301
  • 1
  • 3
  • 6
5
votes
2 answers

Why is memcpy slow in 32-bit mode with gcc -march=native on Ryzen, for large buffers?

I wrote a simple test (code down the bottom) to benchmark the performance of memcpy on my 64-bit Debian system. On my system when compiled as a 64-bit binary this gives a consistent 38-40GB/s across all block sizes. However when built as a 32-bit…
Geoffrey
  • 10,843
  • 3
  • 33
  • 46
5
votes
1 answer

Hint to compiler that it can use aligned memcpy

I have a struct consisting of seven __m256 values, which is stored 32-byte aligned in memory. typedef struct { __m256 xl,xh; __m256 yl,yh; __m256 zl,zh; __m256i co; } bloxset8_t; I achieve the 32-byte alignment by…
Bram
  • 7,440
  • 3
  • 52
  • 94