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
2 answers

Overwriting an object with an object of same type

Is the following well defined? #include #include using namespace std; struct Const { const int i; Const (int i) : i(i) {} int get0() { return 0; } // best accessor ever! }; int main() { Const *q,*p = new…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
4
votes
3 answers

memcpy performance vs byte shift

There are 2 variables: uint32_t var32 = 0xAABBCCDD; uint8_t var8[4] = { 0, 0, 0, 0 }; Which copying way of var32 to var8 would be faster? for (size_t i = 0; i < sizeof(uint32_t); i++) var8[i] = (uint8_t)(var32 >> (i * 8)); or memcpy(var8,…
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
4
votes
2 answers

memcpy where size is known at compile time

I find myself tuning a piece of code where memory is copied using memcpy and the third parameter (size) is known at compile time. The consumer of the function calling memcpy does something similar to this: template void foo() { void*…
Thomas Kejser
  • 1,264
  • 1
  • 10
  • 30
4
votes
1 answer

Convert safely between uint8_t[8] & uint64_t via cast?

The way I'm currently doing it (I'd prefer to get rid of the memcpy call): uint64_t integer; uint8_t string[8]; ... memcpy(&integer, &string, 8); //or swap the parameters Assuming integer array length to always be a multiple of 8 (64 bits total…
Engineer
  • 8,529
  • 7
  • 65
  • 105
4
votes
1 answer

Most efficient way to pass data from C++ to C#

I am looking for the best way to transfer a large amount of data from C++ (struct or a value class?) into a C# class doing as little data copying as possible. In the sample code below, I have a vector of SubClass objects that has the potential to…
user3072517
  • 513
  • 1
  • 7
  • 21
4
votes
1 answer

Copying files using memory map

I want to implement an effective file copying technique in C for my process which runs on BSD OS. As of now the functionality is implemented using read-write technique. I am trying to make it optimized by using memory map file copying technique.…
Bose
  • 381
  • 3
  • 16
4
votes
4 answers

memcpy segmentation fault on linux but not os x

I'm working on implementing a log based file system for a file as a class project. I have a good amount of it working on my 64 bit OS X laptop, but when I try to run the code on the CS department's 32 bit linux machines, I get a seg fault. The API…
Andre Azzolini
  • 95
  • 1
  • 1
  • 6
4
votes
1 answer

How to add a hook in memcpy function of linux kernel?

The following are my steps,but it did not work as intended. linux-3.16-rc2\arch\x86\lib\memcpy_64.S: changed…
Chen
  • 105
  • 6
4
votes
2 answers

Memory Bandwidth Performance for Modern Machines

I'm designing a real-time system that occasionally has to duplicate a large amount of memory. The memory consists of non-tiny regions, so I expect the copying performance will be fairly close to the maximum bandwidth the relevant components (CPU,…
porgarmingduod
  • 7,668
  • 10
  • 50
  • 83
4
votes
3 answers

Memcpy Char Pointers

I have this simple program in which I want to concatenate two char pointers using memcpy, but I get access violation reading location on the memcpy line. char *first = new char[10], *second=new char[10]; first="Hello "; printf("\second:…
Tanatos Daniel
  • 558
  • 2
  • 9
  • 27
4
votes
3 answers

C# copy array of structs to array of doubles

[StructLayout(LayoutKind.Sequential)] public struct Demo { double X; double Y; } var data = new Demo[128]; FillWithMeaningfulValues(data); double[] doubles; Copy(data, out doubles); // ? How do I copy the demo array into the doubles…
Wilbert
  • 7,251
  • 6
  • 51
  • 91
4
votes
3 answers

memcpy: warning: dereferencing ‘void *’ pointer

I use the read() function to read in 40 characters from a file, and need to copy from the offset of 10 for the length of 20. In other words, I need to do memcpy from the 10th to 30th characters into a new memory address. When I run my code (see…
TonyW
  • 18,375
  • 42
  • 110
  • 183
4
votes
4 answers

how to memcpy iterator element in C++?

I have the following code in C++: typedef struct { int age; int roomNumber; } Student; Student student; student.age = 20; student.roomNumber = 10; vector
ratzip
  • 113
  • 2
  • 11
4
votes
2 answers

Can I use memcpy_s with ObjC on iOS?

Is there any replacement for memcpy in iOS? So far as I know, memcpy is not 'safe', and the suggested alternative is 'memcpy_s' But, code fail to compile due to 'Undefined symbols for architecture armv7:' problem, after replacing memcpy with…
Sanbrother
  • 601
  • 5
  • 12
4
votes
1 answer

Are the restrictions of std::copy more relaxed than std::memcopy?

With regard to the issues copy vs. memcpy vs memmove(excellent info here, btw.), I have been reading up and it would seem to me, that unlike what is colloquially said, for example at cppreferenceNote: memcpy has been changed to memmove since taking…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337