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

C++ equivalent for memset on char*

I have this code char * oldname = new char[strlen(name) + 1]; memcpy(oldname,name,strlen(name) + 1); name = new char[strlen(oldname) + strlen(r.name) + 1]; memset(name, '\0', strlen(name)); strcat(name,oldname); strcat(name," "); …
Para
  • 2,022
  • 5
  • 34
  • 73
8
votes
3 answers

Off-chip memcpy?

I was profiling a program today at work that does a lot of buffered network activity, and this program spent most of its time in memcpy, just moving data back and forth between library-managed network buffers and its own internal buffers. This got…
SoapBox
  • 20,457
  • 3
  • 51
  • 87
8
votes
2 answers

Memcpy of native array to managed array in C++ CLI

Am I doing this right? I get a pointer to a native array and need to copy to a managed array. Use memcpy() with a pin_ptr. unsigned char* pArray; unsigned int arrayCount; // get pArray & arrayCount (from a COM method) ManagedClass->ByteArray =…
Luke Narramore
  • 502
  • 2
  • 6
  • 17
8
votes
1 answer

Why is there no std::data() overload for std::valarray?

C++11 introduced std::begin(std::valarray&) as well as std::end(std::valarray&). C++17 introduced std::data() which works with std::vector, std::array, C-style arrays, etc. But why wasn't an overloaded std::data() introduced for…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
8
votes
2 answers

memcpy was not declared error in eclipse CDT C++

I am trying to do memcpy char *pdata = data pointer; int64_t deviceId; memcpy(&deviceId, pdata+1, 8); And it complains "memcpy was not declared in this scope" I have included below libraries in my header file How…
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
8
votes
1 answer

Avoid `-Wclass-memaccess` on memcpy of a POD type w/copy disabled

If you turn up your warning level to -Wall, GCC 8 (at least g++ (Ubuntu 8.3.0-6ubuntu1~18.10.1) 8.3.0 with --std=c++17) gives -Wclass-memaccess on: #ifdef __cplusplus #include #endif #include struct CantCopy { int…
8
votes
2 answers

Order-preserving memcpy in C++

I'm developing a multicore, multithreaded software library in which I want to offer update-order preserving lock-free shared memory objects that might span multiple cache lines. Specifically, suppose that I have some vector X of cache-line-sized…
Ken Birman
  • 1,088
  • 8
  • 25
8
votes
3 answers

double free or corruption error when copying object with memcpy

I have the following code: #include #include #include struct test { std::string name; size_t id; }; int main() { test t; t.name = "147.8.179.239"; t.id = 10; char a[sizeof(t)] = ""; …
Johnnylin
  • 507
  • 2
  • 7
  • 26
8
votes
4 answers

Go conversion between struct and byte array

I am writing a client - server application in Go. I want to perform C-like type casting in Go. E.g. in Go type packet struct { opcode uint16 data [1024]byte } var pkt1 packet ... n, raddr, err := conn.ReadFromUDP(pkt1) // error here Also…
Shriganesh Shintre
  • 2,428
  • 3
  • 17
  • 16
8
votes
3 answers

GCC with -fno-builtin does not seem to work

I would like to compare the GCC builtin function memcpy versus the one one from libc. However, all iterations of -fno-builtin or -fno-builtin-memcpy seem to be ignored. //g++ -O3 foo.cpp -S or //g++ -O3 -fno-builtin foo.cpp -S #include…
Z boson
  • 32,619
  • 11
  • 123
  • 226
8
votes
3 answers

c++ std::pair, std::vector & memcopy

is it safe to memcopy myvect.size()*sizeof(foo) bytes from the memoryadress of the first element of a std::vector > myvect into an array of struct foo{ T1 first; T2 second; } if the array is allocated with the same…
Mat
  • 11,263
  • 10
  • 45
  • 48
8
votes
4 answers

Is memcpy process-safe?

Ive looked online and have not been able to satisfy myself with an answer. Is memcpy threadsafe? (in Windows) What I mean is if I write to an area of memory shared between processes (using boost::shared_memory_object) using a single memcpy and then…
twerdster
  • 4,977
  • 3
  • 40
  • 70
8
votes
2 answers

Using memcpy to copy part of an array, and other memory manipulation tools

Is it possible to use memcpy to copy part of an array? Say for example we have an array of 10 integers. Can we create a new array, and copy the last 5 integers into it? Are there other memory/array copying/manipulation tools which are available for…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
8
votes
7 answers

C Programming. How to deep copy a struct?

I have the following two structs where "child struct" has a "rusage struct" as an element. Then I create two structs of type "child" let's call them childA and childB How do I copy just the rusage struct from childA to childB? typedef struct{ …
user69514
  • 26,935
  • 59
  • 154
  • 188
8
votes
6 answers

Make compiler copy characters using movsd

I would like to copy a relatively short sequence of memory (less than 1 KB, typically 2-200 bytes) in a time critical function. The best code for this on CPU side seems to be rep movsd. However I somehow cannot make my compiler to generate this…
Suma
  • 33,181
  • 16
  • 123
  • 191