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

Understanding memcpy

int a = 10; int* pA = &a; long long b = 200; long long* pB = &b; memcpy (pB,pA,4); memcpy (pB+1,pA,4); cout<<"I'm a memcpy!: "<<*(pB)<
Greg G
  • 697
  • 2
  • 8
  • 17
7
votes
4 answers

C - Malloc and memcpy (memory management)

I'm a bit new to C and I'm having trouble understanding how memory works, especially in-built functions like memcpy. Here's a struct I'm using struct data_t { int datasize; void *data; }; And here's an auxiliary function that I'm…
PTdude
  • 71
  • 1
  • 1
  • 3
7
votes
7 answers

Copying byte array to various fields in class/struct in C#

In the example C# code below, I have a byte array which has been read from a socket. I want to parse the data into the various fields of 'exampleClass' (first 8 bytes into the 64-bit variable 'field1', next 4 bytes into 32-bit variable 'field2',…
JamesPD
  • 243
  • 1
  • 3
  • 7
7
votes
1 answer

using memcpy to convert from array to int

I was experimenting with pointer manipulation and decided to try converting an array of numbers into an integer by directly copying from memory using memcpy. char aux[4] = {1,2,3,4}; int aux2 = 0; memcpy((char*) &aux2, &aux[0], 4); printf("%X",…
7
votes
4 answers

Does memcpy() uses realloc()?

#inlcude #inlcude #inlcude int main() { char *buff = (char*)malloc(sizeof(char) * 5); char *str = "abcdefghijklmnopqrstuvwxyz"; memcpy (buff, str, strlen(str)); while(*buff) { printf("%c" ,…
shan
  • 1,164
  • 4
  • 14
  • 30
7
votes
1 answer

Fast interleave operation in C?

I need to combine two arrays into a third in chunks of four. Specifically, for input arrays A0, A1, A2, A3, A4, A5, A6, A7 ... B0, B1, B2, B3, B4, B5, B6, B7 ... the output should be A0 A1 A2 A3 B0 B1 B2 B3 A4 A5 A6 A7 B4 B5 B6…
MikeLima
  • 79
  • 3
7
votes
2 answers

How does binary I/O of POD types not break the aliasing rules?

Twenty plus years ago, I would have (and didn't) think anything of doing binary I/O with POD structs: struct S { std::uint32_t x; std::uint16_t y; }; S s; read(fd, &s, sizeof(s)); // assume this succeeds and reads sizeof(s) bytes std::cout << s.x +…
Brad Spencer
  • 952
  • 6
  • 16
7
votes
2 answers

ARM Cortex M7 unaligned access and memcpy

I am compiling this code for a Cortex M7 using GCC: // copy manually void write_test_plain(uint8_t * ptr, uint32_t value) { *ptr++ = (u8)(value); *ptr++ = (u8)(value >> 8); *ptr++ = (u8)(value >> 16); *ptr++ = (u8)(value >> 24);…
Lou
  • 4,244
  • 3
  • 33
  • 72
7
votes
2 answers

Is it safe to memcpy to a dynamic storage struct?

Context: I was reviewing some code that receives data from an IO descriptor into a character buffer, does some control on it and then use part of the received buffer to populate a struct, and suddenly wondered whether a strict aliasing rule…
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
7
votes
7 answers

Linux Device Driver: Symbol "memcpy" not found

I'm trying to write a Linux device driver. I've got it to work really well, until I tried to use "memcpy". I don't even get a compiler error, when I "make" it just warns me: WARNING: "memcpy" [/root/homedir/sv/main.ko] undefined! OK and when I try…
Hinton
  • 2,320
  • 4
  • 26
  • 32
7
votes
3 answers

Dynamic allocation of array issue C

To be clear, my code works perfectly. The issue that concerns me is that i am unsure of my array allocation type. My task is rather simple: i am required to do some operations within a dynamically allocated array. Yet, the values are already given…
Catalin Ghita
  • 794
  • 8
  • 26
7
votes
3 answers

Can I use memcpy to write to multiple adjacent Standard Layout sub-objects?

Disclaimer: This is trying to drill down on a larger problem, so please don't get hung up with whether the example makes any sense in practice. And, yes, if you want to copy objects, please use / provide the copy-constructor. (But note how even the…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
7
votes
3 answers

Memcpy performance on /dev/mem outside kernel ram

I'm using a SoC with a custom linux on it. I have reserved the upper 512MB of 1GB total RAM by specifying kernel boot parameter mem=512M. I can access the upper memory from a userspace program by opening /dev/mem and mmap the upper 512MB which is…
Rafael
  • 113
  • 1
  • 6
7
votes
1 answer

Is it possible to statically link libstdc++ and wrap memcpy?

I am trying to build an executable on Linux that meets the following criteria: statically linked to libstdc++ and libgcc built with a recent version of gcc (version >= 4.8.2) and glibc (version > 2.14) backwards compatible with old versions of…
yoshimonster
  • 147
  • 2
  • 7
7
votes
4 answers

memcpy adds ff ff ff to the beginning of a byte

I have an array that is like this: unsigned char array[] = {'\xc0', '\x3f', '\x0e', '\x54', '\xe5', '\x20'}; unsigned char array2[6]; When I use memcpy: memcpy(array2, array, 6); And print both of them: printf("%x %x %x %x %x %x", array[0], //…
Hock
  • 165
  • 1
  • 5