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

Which type trait would indicate that type is memcpy assignable? (tuple, pair)

I would like to know what type introspection I can do to detect types that assignable by simply raw memory copy? For example, as far I understand, built-in types tuples of built-in types and tuple of such tuples, would fall in this category. The…
alfC
  • 14,261
  • 4
  • 67
  • 118
10
votes
7 answers

trying to copy struct members to byte array in c

I am attempting to copy the members of a struct containing a mixture of ints, char's and arrays of chars into a byte array to send to a serial line. So far I have struct msg_on_send { char descriptor_msg[5]; int address; char space; …
droseman
  • 277
  • 3
  • 5
  • 12
10
votes
1 answer

Explanation of memcpy memmove GLIBC_2.14/2.2.5

My issue originated with a shared library I was given without the option to recompile the library. The error stated undefined reference to memcpy@GLIBC_2.14. The version of GLIBC on my machine was 2.12. I have seen fixes people have done online…
PickleWeasel
  • 101
  • 1
  • 4
10
votes
3 answers

Preference between memcpy and dereference

When copying a known struct in memory, would you prefer using memcpy or dereference? why? Specifically, in the following code: #include #include typedef struct { int foo; int bar; } compound; void…
Shao-Chuan Wang
  • 980
  • 8
  • 16
10
votes
4 answers

Can I memcpy() any type which has a trivial destructor?

I do realize is_pod is a sufficient condition for a type to be memcpy-able, but is has_trivial_destructor also sufficient for this purpose? If not, why?
user541686
  • 205,094
  • 128
  • 528
  • 886
10
votes
3 answers

C++ ifstream::read slow due to memcpy

Recently I decided to optimize some file reading I was doing, because as everyone says, reading a large chunk of data to a buffer and then working with it is faster than using lots of small reads. And my code certainly is much faster now, but after…
retep998
  • 888
  • 1
  • 9
  • 14
9
votes
4 answers

How to split array into two arrays in C

Say i have an array in C int array[6] = {1,2,3,4,5,6} how could I split this into {1,2,3} and {4,5,6} Would this be possible using memcpy? Thank You, nonono
123hal321
  • 2,080
  • 4
  • 24
  • 25
9
votes
1 answer

A curious string copy function in C

When I was reading the nginx code, I have seen this function : #define ngx_cpymem(dst, src, n) (((u_char *) memcpy(dst, src, n)) + (n)) static ngx_inline u_char * ngx_copy(u_char *dst, u_char *src, size_t len) { if (len < 17) { while…
Ghassen Hamrouni
  • 3,138
  • 2
  • 20
  • 31
9
votes
3 answers

What is the correct way to temporarily cast void* for arithmetic?

I am C novice but been a programmer for some years, so I am trying to learn C by following along Stanford's course from 2008 and doing Assignment 3 on Vectors in C. It's just a generic array basically, so the data is held inside a struct as a void…
Sam Hammamy
  • 10,819
  • 10
  • 56
  • 94
9
votes
3 answers

C++ is_trivially_copyable check

How to check whether or not C++ type is trivially copyable? I have a class, which uses memcpy and memcmp functions with specified template type T and I would like to fire assert for types, that are not safe to copy with memcpy. Is there any way to…
axe
  • 217
  • 3
  • 9
9
votes
2 answers

Load of misaligned address and UBsan finding

This question is not about the definition of unaligned data accesses, but why memcpy silences the UBsan findings whereas type casting does not, despite generating the same assembly code. I have some example code to parse a protocol that sends a byte…
Charles
  • 953
  • 1
  • 8
  • 19
9
votes
2 answers

Undefined reference to memcpy_s

I'm trying to fix an undefined reference to memcpy_s() error. I've included string.h in my file and the memcpy() function works okay, and I've also tried including memory.h. I'm on x64 Windows 7 and using gcc 4.8.1 to compile. #include…
hydrazone
  • 111
  • 1
  • 1
  • 5
9
votes
1 answer

Copy 2D array using memcpy?

So I want to copy the contents of a 2D array to another array of the exact same type. Here is how the array is created: GridUnit** newGrid; newGrid = new GridUnit*[width]; for (int i = 0; i < width; i++) newGrid[i] = new…
KieronH
  • 103
  • 1
  • 1
  • 3
9
votes
2 answers

ARMCC: problems with memcpy (alignment exceptions)

I am porting some software from the gcc-toolchain to the armcc-toolchain (processor stays the same (Cortex-A9)). In the C-code memcpy is used. armcc replaces a call to memcpy by a call to __aeabi_memcpy. The FAQ sais the following about…
user3035952
  • 301
  • 5
  • 12
9
votes
2 answers

Why is memset slow?

The spec for my CPU says it should get 5.336GB/s bandwidth to memory. To test this, I wrote a simple program that runs memset (or memcpy) on a big array and reports the timing. I'm showing 3.8GB/s on memset and 1.9GB/s on memcpy. …
Jeff Guy
  • 157
  • 1
  • 9