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
43
votes
2 answers

What header should I include for memcpy and realloc?

I am porting a project to the iPhone and it uses realloc and memcpy which are not found. What is the header to include? It's a project mixing Objective C and C++ and I am starting to be lost. Thanks in advance for your help!
AP.
  • 5,205
  • 7
  • 50
  • 94
40
votes
6 answers

Does "&s[0]" point to contiguous characters in a std::string?

I'm doing some maintenance work and ran across something like the following: std::string s; s.resize( strLength ); // strLength is a size_t with the length of a C string in it. memcpy( &s[0], str, strLength ); I know using &s[0] would be safe…
oz10
  • 153,307
  • 27
  • 93
  • 128
39
votes
8 answers

Very fast memcpy for image processing?

I am doing image processing in C that requires copying large chunks of data around memory - the source and destination never overlap. What is the absolute fastest way to do this on the x86 platform using GCC (where SSE, SSE2 but NOT SSE3 are…
horseyguy
  • 29,455
  • 20
  • 103
  • 145
38
votes
5 answers

Is the std::array bit compatible with the old C array?

Is the underlying bit representation for an std::array v and a T u[N] the same? In other words, is it safe to copy N*sizeof(T) bytes from one to the other? (Either through reinterpret_cast or memcpy.) Edit: For clarification, the emphasis is on…
shinjin
  • 2,858
  • 5
  • 29
  • 44
36
votes
2 answers

memcpy zero bytes into const variable - undefined behavior?

In C and C++, is it undefined behavior to memcpy into a const variable when the number of bytes to be copied is zero? int x = 0; const int foo = 0; memcpy( (void *)&foo, &x, 0 ); This question is not purely theoretical. I have a scenario in which…
Jackson Allan
  • 727
  • 3
  • 11
36
votes
5 answers

C++ memcpy return value

According to http://en.cppreference.com/w/cpp/string/byte/memcpy C++'s memcpy takes three parameters: destination, source and size/bytes. It also returns a pointer. void* memcpy( void* dest, const void* src, std::size_t count ); Why is that so?…
knittl
  • 246,190
  • 53
  • 318
  • 364
34
votes
1 answer

memcpy vs assignment in C

Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware? I am using GCC 4.2.x on a 32 bit Intel platform (but am interested in 64 bit as well).
Setjmp
  • 27,279
  • 27
  • 74
  • 92
31
votes
3 answers

How does the internal implementation of memcpy work?

How does the standard C function 'memcpy' work? It has to copy a (large) chunk of RAM to another area in the RAM. Since I know you cannot move straight from RAM to RAM in assembly (with the mov instruction) so I am guessing it uses a CPU register as…
PersonWithName
  • 848
  • 2
  • 13
  • 19
28
votes
3 answers

Forcing GCC to perform loop unswitching of memcpy runtime size checks?

Is there any reliable way to force GCC (or any compiler) to factor out runtime size checks in memcpy() outside of a loop (where that size is not compile-time constant, but constant within that loop), specializing the loop for each relevant size…
Stephen Lin
  • 5,470
  • 26
  • 48
27
votes
2 answers

Is memcpy of a trivially-copyable type construction or assignment?

Let's say you have an object of type T and a suitably-aligned memory buffer alignas(T) unsigned char[sizeof(T)]. If you use std::memcpy to copy from the object of type T to the unsigned char array, is that considered copy construction or…
Myria
  • 3,372
  • 1
  • 24
  • 42
26
votes
5 answers

Copy a std::vector to a repeated field from protobuf with memcpy

At first I have this simple protobuf file message messagetest { ... repeated float samples = 6; .... } Which creates a headerfile with this methods //repeated float samples = 6; inline int samples_size() const; inline…
akristmann
  • 434
  • 1
  • 7
  • 15
26
votes
6 answers

What is the difference between memset and memcpy in C

I've read the function headers, but I'm still not sure what exactly the difference is in terms of use cases.
Dirk
  • 6,774
  • 14
  • 51
  • 73
26
votes
9 answers

Go- Copy all common fields between structs

I have a database that stores JSON, and a server that provides an external API to whereby through an HTTP post, values in this database can be changed. The database is used by different processes internally, and as such have a common naming…
beatgammit
  • 19,817
  • 19
  • 86
  • 129
25
votes
12 answers

memcpy(), what should the value of the size parameter be?

I want to copy an int array to another int array. They use the same define for length so they'll always be of the same length. What are the pros/cons of the following two alternatives of the size parameter to memcpy()? memcpy(dst, src,…
Tomas
  • 514
  • 1
  • 5
  • 15
24
votes
6 answers

Difference between strncpy and memcpy?

How can I access s[7] in s? I didn't observe any difference between strncpy and memcpy. If I want to print the output s, along with s[7] (like qwertyA), what are the changes I have to made in the following code: #include #include…
user559208
  • 1,485
  • 3
  • 13
  • 9