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
24
votes
3 answers

Is it undefined behaviour to memcpy from an uninitialized variable?

Is using an uninitialized variable as the src for memcpy undefined behaviour in C? void foo(int *to) { int from; memcpy(to, &from, sizeof(from)); }
Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51
24
votes
2 answers

Fast copy of `std::vector`

I have an std::vector, which needs to be duplicated. This is done simply by calling the copy constructor. My profiling results show, that the Microsoft Visual C++ (msvc100) implementation, uses std::uninitialized_copy internally. This…
Ruud
  • 3,118
  • 3
  • 39
  • 51
24
votes
5 answers

Copying structure in C with assignment instead of memcpy()

Up until recently, I have only seen copying of structure fields done with memcpy(). In classes and online instructions, copying the contents of one struct into another generally looks like struct block *b0 = malloc(sizeof(struct block)); struct…
olliezhu
  • 752
  • 1
  • 8
  • 17
22
votes
17 answers

C strcpy() - evil?

Some people seem to think that C's strcpy() function is bad or evil. While I admit that it's usually better to use strncpy() in order to avoid buffer overflows, the following (an implementation of the strdup() function for those not lucky enough to…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
22
votes
2 answers

Is memcpy() usually faster than strcpy()?

Is memcpy() usually faster than strcpy() (on most real platforms)? (I assume that size of the string is known.) If I remember i386 assembler correctly, there are loop instructions which copy a given number of bytes or words. So it is the fastest…
porton
  • 5,214
  • 11
  • 47
  • 95
22
votes
1 answer

When __builtin_memcpy is replaced with libc's memcpy

There is a version of C99/posix memcpy function in GCC: __builtin_memcpy. Sometimes it can be replaced by GCC to inline version of memcpy and in other cases it is replaced by call to libc's memcpy. E.g. it was noted here: Finally, on a compiler…
osgx
  • 90,338
  • 53
  • 357
  • 513
21
votes
8 answers

optimized memcpy

Are there faster alternatives to memcpy() in C++?
Bi.
  • 1,846
  • 8
  • 25
  • 34
19
votes
10 answers

Memcpy() in secure programming?

I recently stumbled across an article that claims Microsoft is banning the memcpy() function in its secure programming shops. I understand the vulnerabilities inherent in the function, but is it necessary to ban its use entirely? Should programs I…
Tim
  • 59,527
  • 19
  • 156
  • 165
18
votes
3 answers

Using memcpy to copy a range of elements from an array

Say we have two arrays: double *matrix=new double[100]; double *array=new double[10]; And we want to copy 10 elements from matrix[80:89] to array using memcpy. Any quick solutions?
Eminemya
  • 379
  • 1
  • 6
  • 15
18
votes
2 answers

C memcpy in reverse

I am working with audio data. I'd like to play the sample file in reverse. The data is stored as unsigned ints and packed nice and tight. Is there a way to call memcpy that will copy in reverse order. i.e. if I had 1,2,3,4 stored in an array, could…
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
18
votes
5 answers

What makes Apple's PowerPC memcpy so fast?

I've written several copy functions in search of a good memory strategy on PowerPC. Using the Altivec or fp registers with cache hints (dcb*) doubles the performance over a simple byte copy loop for large data. Initially pleased with that, I threw…
user242091
18
votes
3 answers

Performance of list(...).insert(...)

I thought about the following question about computer's architecture. Suppose I do in Python from bisect import bisect index = bisect(x, a) # O(log n) (also, shouldn't it be a standard list function?) x.insert(index, a) # O(1) +…
ilya n.
  • 18,398
  • 15
  • 71
  • 89
16
votes
1 answer

Is there a standard, strided version of memcpy?

I have a column vector A which is 10 elements long. I have a matrix B which is 10 by 10. The memory storage for B is column major. I would like to overwrite the first row in B with the column vector A. Clearly, I can do: for ( int i=0; i < 10;…
M. Tibbits
  • 8,400
  • 8
  • 44
  • 59
16
votes
7 answers

What are real significant cases when memcpy() is faster than memmove()?

The key difference between memcpy() and memmove() is that memmove() will work fine when source and destination overlap. When buffers surely don't overlap memcpy() is preferable since it's potentially faster. What bothers me is this potentially. Is…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
15
votes
1 answer

MSVC: Invalid memcpy optimization?

Consider the following code: void MemMove8(void* dst, void* src) { char tmp[8]; memcpy(tmp, src, 8); memcpy(dst, tmp, 8); } MSVC (16.7.1) x86 with /O2 generates the following assembly for this function: ; _dst$ = ecx ; _src$ = edx …
Alex
  • 709
  • 3
  • 10