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
12
votes
5 answers

c++ use ifstream from memory

I have some code that uses ifstream to read some data from a file and everything works. Now I wish, without modifying some code, read this data from a memory, actually I have a char * that contains the data... How can I put my char * data into a…
ghiboz
  • 7,863
  • 21
  • 85
  • 131
11
votes
3 answers

avoid trap representation with memcpy

Please consider the following code: float float_value = x; // x is any valid float value int int_value = 0; size_t size = sizeof(int) < sizeof(float) ? sizeof(int) : sizeof(float); memcpy(&int_value, &float_value, size); As far as i know this could…
Johannes
  • 871
  • 1
  • 10
  • 16
11
votes
3 answers

Does std::memcpy make its destination determinate?

Here is the code: unsigned int a; // a is indeterminate unsigned long long b = 1; // b is initialized to 1 std::memcpy(&a, &b, sizeof(unsigned int)); unsigned int c = a; // Is this not undefined behavior? (Implementation-defined…
Joel
  • 2,065
  • 2
  • 19
  • 30
11
votes
1 answer

What should replace "memcpy" inside OpenCL kernels?

The OpenCL language, which extends C99, does not provide the memcpy function. What should be used instead?
grrussel
  • 7,209
  • 8
  • 51
  • 71
11
votes
3 answers

Is `memcpy((void *)dest, src, n)` with a `volatile` array safe?

I have a buffer that I use for UART, which is declared this way: union Eusart_Buff { uint8_t b8[16]; uint16_t b9[16]; }; struct Eusart_Msg { uint8_t msg_posn; uint8_t msg_len; union Eusart_Buff …
11
votes
2 answers

do malloc/memcpy function run independently on NUMA?

While trying to increase the speed of my applications on non-NUMA / standard PCs I always found that the bottleneck was the call to malloc() because even in multi-core machines it is shared/synch between all the cores. I have available a PC with…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
11
votes
2 answers

Why does copying a structure by a direct assignment fail?

I am running into an Hard Fault Exception while copying some data on a micro-controller from one struct to another. I tried different implementations which should do all the same. See my code lines: memcpy(&msg.data, data, 8); memcpy(&msg.data,…
eDeviser
  • 1,605
  • 2
  • 17
  • 44
11
votes
6 answers

Writing memcpy conformant with strict aliasing

The general answer when asking "how does one implement memcpy function conformant with strict aliasing rules" is something along the lines of void *memcpy(void *dest, const void *src, size_t n) { for (size_t i = 0; i < n; i++) …
Oleg Andreev
  • 353
  • 1
  • 9
11
votes
2 answers

Using memcpy in C++

I am little confused on the parameters for the memcpy function. If I have int* arr = new int[5]; int* newarr = new int[6]; and I want to copy the elements in arr into newarr using memcopy, memcpy(parameter, parameter, parameter) How do I do this?
DebareDaDauntless
  • 471
  • 2
  • 7
  • 12
11
votes
4 answers

C++ memcpy to char* from c_str

I've done a bit of basic reading and from what I've gathered .c_str() always has a null terminator. I have a fairly simple C++ program: int main(int argc, char** argv) { std::string from = "hello"; char to[20]; memcpy(to, from.c_str(),…
Max
  • 113
  • 1
  • 1
  • 6
11
votes
3 answers

How to memcpy a part of a two dimensional array in C?

How to memcpy the two dimensional array in C: I have a two dimensional array: int a[100][100]; int c[10][10]; I want to use memcpy to copy the all the values in array c to array a, how to do this using memcpy? int i; for(i = 0; i<10; i++) { …
user2131316
  • 3,111
  • 12
  • 39
  • 53
10
votes
7 answers

Microsoft SDL and memcpy deprecation

As some of you may know, Microsoft banned memcpy() from their Security Development Lifecycle, replacing it with memcpy_s(). void *memcpy(void *dest, const void *src, size_t n); /* simplified signature */ errno_t memcpy_s(void *dst, size_t dstsize,…
Alex B
  • 82,554
  • 44
  • 203
  • 280
10
votes
3 answers

Fully optimized memcpy/memmove for Core 2 or Core i7 architecture?

The theoretical maximum of memory bandwidth for a Core 2 processor with DDR3 dual channel memory is impressive: According to the Wikipedia article on the architecture, 10+ or 20+ gigabytes per second. However, stock memcpy() calls do not attain…
Matthew
10
votes
3 answers

'memcpy' is not defined in this scope

I am getting a "memcpy is not defined in this scope error" with the following piece of code: CommonSessionMessage::CommonSessionMessage(const char* data, int size) : m_data(new char[size]) { memcpy(m_data.get(), data, size); } I have looked…
czchlong
  • 2,434
  • 10
  • 51
  • 65
10
votes
3 answers

memcpy vs assignment in C -- should be memmove?

As pointed out in an answer to this question, the compiler (in this case gcc-4.1.2, yes it's old, no I can't change it) can replace struct assignments with memcpy where it thinks it is appropriate. I'm running some code under valgrind and got a…
bstpierre
  • 30,042
  • 15
  • 70
  • 103