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
205
votes
11 answers

memcpy() vs memmove()

I am trying to understand the difference between memcpy() and memmove(), and I have read the text that memcpy() doesn't take care of the overlapping source and destination whereas memmove() does. However, when I execute these two functions on…
user534785
  • 2,173
  • 2
  • 13
  • 6
138
votes
9 answers

What is the difference between memmove and memcpy?

What is the difference between memmove and memcpy? Which one do you usually use and how?
ultraman
131
votes
2 answers

Can I call memcpy() and memmove() with "number of bytes" set to zero?

Do I need to treat cases when I actully have nothing to move/copy with memmove()/memcpy() as edge cases int numberOfBytes = ... if( numberOfBytes != 0 ) { memmove( dest, source, numberOfBytes ); } or should I just call the function without…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
99
votes
6 answers

Enhanced REP MOVSB for memcpy

I would like to use enhanced REP MOVSB (ERMSB) to get a high bandwidth for a custom memcpy. ERMSB was introduced with the Ivy Bridge microarchitecture. See the section "Enhanced REP MOVSB and STOSB operation (ERMSB)" in the Intel optimization manual…
Z boson
  • 32,619
  • 11
  • 123
  • 226
97
votes
9 answers

strcpy vs. memcpy

What is the difference between memcpy() and strcpy()? I tried to find it with the help of a program but both are giving the same output. int main() { char s[5] = { 's', 'a', '\0', 'c', 'h' }; char p[5]; char t[5]; strcpy(p, s); …
Sachin
  • 20,805
  • 32
  • 86
  • 99
95
votes
2 answers

How does the JPEG of Death vulnerability operate?

I’ve been reading about an older exploit against GDI+ on Windows XP and Windows Server 2003 called the JPEG of death for a project I’m working on. The exploit is well explained in the following…
Rafa
  • 1,151
  • 9
  • 17
93
votes
3 answers

Is it guaranteed to be safe to perform memcpy(0,0,0)?

I am not so well-versed in the C standard, so please bear with me. I would like to know if it is guaranteed, by the standard, that memcpy(0,0,0) is safe. The only restriction I could find is that if the memory regions overlap, then the behavior is…
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
80
votes
10 answers

Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

From http://en.cppreference.com/w/cpp/string/byte/memcpy: If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined. At my work, we have used std::memcpy for a long time to bitwise swap…
R Sahu
  • 204,454
  • 14
  • 159
  • 270
77
votes
16 answers

faster alternative to memcpy?

I have a function that is doing memcpy, but it's taking up an enormous amount of cycles. Is there a faster alternative/approach than using memcpy to move a piece of memory?
Tony Stark
  • 24,588
  • 41
  • 96
  • 113
77
votes
7 answers

Poor memcpy Performance on Linux

We have recently purchased some new servers and are experiencing poor memcpy() performance. The memcpy() performance is 3x slower on the servers compared to our laptops. Server Specs Chassis and Mobo: SUPER MICRO 1027GR-TRF CPU: 2x Intel Xeon…
nick
  • 513
  • 1
  • 8
  • 12
66
votes
3 answers

Why does the speed of memcpy() drop dramatically every 4KB?

I tested the speed of memcpy() noticing the speed drops dramatically at i*4KB. The result is as follow: the Y-axis is the speed(MB/second) and the X-axis is the size of buffer for memcpy(), increasing from 1KB to 2MB. Subfigure 2 and Subfigure 3…
foool
  • 1,462
  • 1
  • 15
  • 29
55
votes
1 answer

error: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]

I get this error. error: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default] This is the code: int arr[ 12] = {1,0,0,0,0,0,0,0,0,0,9370, 0}; void *a = &arr; memcpy(machine->mem, a,12*4); What I am doing…
user2073729
  • 1,151
  • 4
  • 10
  • 16
53
votes
4 answers

Struct assignment or memcpy?

If I want to replicate a structure in another one (in C), what are the pro&con's of : struct1 = struct2; vs memcpy(&struct1, &struct2, sizeof(mystruct_t)); Are they equivalent ? Is there a difference in performance or memory use ?
ofaurax
  • 1,417
  • 1
  • 20
  • 27
52
votes
8 answers

How to increase performance of memcpy

Summary: memcpy seems unable to transfer over 2GB/sec on my system in a real or test application. What can I do to get faster memory-to-memory copies? Full details: As part of a data capture application (using some specialized hardware), I need to…
leecbaker
  • 3,611
  • 2
  • 35
  • 51
50
votes
9 answers

memcpy with startIndex?

I wish to copy content of specific length from one buffer to another from a specific starting point. I checked memcpy() but it takes only the length of content to be copied while I want to specify the starting index too. Is there any function which…
soloasylum
1
2 3
99 100