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.

1427 questions
167
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
  • 1,793
  • 2
  • 11
  • 6
128
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
112
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
  • 159,303
  • 82
  • 478
  • 911
94
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,081
  • 8
  • 16
83
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 Chourasiya
  • 18,417
  • 31
  • 80
  • 95
81
votes
4 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.
  • 251,718
  • 39
  • 369
  • 642
75
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
  • 196,807
  • 13
  • 136
  • 247
73
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 E5-2680 @…
nick
  • 453
  • 1
  • 8
  • 12
72
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…
Z boson
  • 29,230
  • 10
  • 105
  • 195
61
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,291
  • 1
  • 12
  • 25
56
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
  • 21,996
  • 37
  • 90
  • 111
51
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,283
  • 2
  • 31
  • 48
50
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,073
  • 4
  • 10
  • 15
44
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,108
  • 1
  • 16
  • 25
43
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
95 96