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
4
votes
4 answers

Is memcpy() safe when copy some content larger than dst?

Is it safe when the request_token.size() is larger than LEN? char dst[LEN]; memcpy(dst, request_token.c_str(), request_token.size());
amazingjxq
  • 4,487
  • 7
  • 33
  • 35
4
votes
1 answer

Under what conditions is it safe to use std::memcpy to copy between objects?

Under what set of conditions is it safe to use std::memcpy to copy from one object to another? For example, what conditions must T, src and dest satisfy for the following to be safe: template void copy_bytewise(T& dest, const T& src) { …
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
4
votes
3 answers

Using memcpy for copying array to struct and vice versa in C

Suppose I have structure: typedef struct { double re; double im; }ComplexStruct; and array: typedef double ComplexArr[2];// [0] -> real, [1] -> imag today I am copying from ComplexStruct to ComplexArr and vice versa using simple for…
Benny K
  • 1,957
  • 18
  • 33
4
votes
5 answers

Is memcpy accelerated in some way on the iPhone?

Few days ago I was writing some code and I had noticed that copying RAM by memcpy was much-much faster than copying it in for loop. I got no measurements now (maybe I did some time later) but as I remember the same block of RAM which in for qas…
grunge fightr
  • 1,360
  • 2
  • 19
  • 38
4
votes
3 answers

failure scenarios for memcpy

I am trying to understand the scenarios in which call to memcpy can fail silently because invalid pointers will result in access violation/segfaults. Also, there will be issues in case of overlapping pointers. Apart from these, are there any other…
user1669844
  • 703
  • 9
  • 23
4
votes
3 answers

Can an access violation be a disguised out-of-memory error?

I'm debugging a 64-bit C++ (managed) crash dump (access violation). The dump has a total size of 32.374.535 kb. The application is multi-threaded, and the corresponding call stack only mentions mscvrt.dll!memcpy (I don't know which other thread is…
Dominique
  • 16,450
  • 15
  • 56
  • 112
4
votes
1 answer

Copying values into a byte array at a specific offset in a safe context

I am trying to copy the value of a uint into a byte array in C#. I have managed to accomplish this using code in an unsafe context but ideally, I would like to do this in a safe context The code I am currently using is this var bytes = new byte[]…
user7962273
4
votes
1 answer

Is it safe memcpy data directly to a union rather than to one of its particular members?

Say I have a union like this union blah { foo f; bar b; }; where both foo and bar are trivially copyable. Is it safe to do this: blah b; foo f; memcpy(&b, &f, sizeof(f)); and then use b.f as the active union member? Or, do I have to…
jcarpenter2
  • 5,312
  • 4
  • 22
  • 49
4
votes
1 answer

Difference between memcpy and copy by assignment

Struct A { uint16_t len; uint8_t cnt; uint8_t unit; uint32_t seq; }; This struct A is serialized into a char * buf. If I want to deserialize the individual values eg: uint16_t len = 0; memcpy(&len, buf, sizeof(len)); or I can just…
Eternal Learner
  • 3,800
  • 13
  • 48
  • 78
4
votes
1 answer

memcpy not optimised out during attempt at ‘fast’ pimpl

I need to use a very large and complex header-only class (think boost::multiprecision::cpp_bin_float<76>, called BHP below) which I would like to hide behind a pimpl-like implementation, purely to reduce compilation time in a somewhat large project…
Claudius
  • 550
  • 3
  • 14
4
votes
3 answers

memcpy and C++ class templates - how to use it?

So.. How can we call something like memcpy(dataCopy, data, length); to copy abstract data T? Or if abstract T is not safe lets say we know that T is a POD (plain old data, basically a C struct) - is it possible to copy it?
Rella
  • 65,003
  • 109
  • 363
  • 636
4
votes
1 answer

Memory copy speed comparison CPU<->GPU

I am now learning boost::compute openCL wrapper library. I am experiencing very slow copy procedure. If we scale CPU to CPU copy speed as 1, how fast is GPU to CPU, GPU to GPU, CPU to GPU copy? I don't require precise numbers. Just a general idea…
Zeta
  • 913
  • 10
  • 24
4
votes
2 answers

How to deduce contiguous memory from iterator

Somehow, the native stl::copy() algorithm on VC++ (Dinkumware) figures out that it can use memcpy() on data that is trivially copy-able. Is it possible for a mere mortal to do that? - assuming each element is_trivially_copyable. Does…
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
4
votes
4 answers

C structure assignment of same address valid?

If I have something like this in my code: void f(struct foo *x, struct foo *y) { *x = *y; // structure copy (memcpy?) } If x and y point to the same address, what happens? Is this valid code, and what if the compiler converts the assignment into…
David Gardner
  • 6,952
  • 4
  • 35
  • 37
4
votes
1 answer

std::memcpy or explicit char value assignment - equal and legal in C++

In the following example the value-representation of an uint32_t is copied to an uint8_t array. This is done by std::memcpy. As I understand the C++ standard this is totally legal: we are accessing an object of type T via T* casted to a unsigned…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39