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

Dereferencing function pointers in C to access CODE memory

We are dealing with C here. I'm just had this idea, wondering if it is possible to access the point in memory where a function is stored, say foo and copying the contents of the function to another point in memory. Specifically, I'm trying to get…
theideasmith
  • 2,835
  • 2
  • 13
  • 20
7
votes
2 answers

Is there a portable way to copy a block of memory in C#?

If you want to do a memory copy between two arrays, there's an Array.Copy function for that in .NET: char[] GetCopy(char[] buf) { char[] result = new char[buf.Length]; Array.Copy(buf, result); return result; } This is usually faster…
James Ko
  • 32,215
  • 30
  • 128
  • 239
7
votes
3 answers

Swift: how add offset to memcpy(...)

How to add offset for array for memcpy(...) invocation? I have array of String : var source = ["a","b","c","d"] var dest = [String](count:n, repeatedValue: "") memcpy(&dest, source, UInt(2 * sizeof(String)) This copy ["a","b"] to dest. I'ts…
Vitaliy L
  • 572
  • 4
  • 20
7
votes
1 answer

memcpy underlying data from std::vector of objects

Is this safe or does this just happen to work on my current compiler? Is there anything about this in the standard? The result in the floats vector is correct. class Color { public: Color(float r, float g, float b, float a) : mColor{r,g,b,a} {}; …
cboe
  • 469
  • 1
  • 9
  • 25
7
votes
4 answers

Linking error when building without CRT, memcpy and memset intrinsic functions

I'm trying to build an application as tiny as possible, and in doing so I'm trying to avoid use of the CRT by using Win API calls instead of standard C/C++ calls. Unfortunately, I'm still getting a single linker error: Error 2 error LNK2001:…
Knox
  • 1,150
  • 1
  • 14
  • 29
7
votes
7 answers

C++: Will structure be copied properly?

I have a pointer to a structure and I need to implement a method that will copy all of the memory contents of a structure. Generally speaking I need to perform a deep copy of a structure. Here's the structure: typedef struct { Size2f…
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
7
votes
6 answers

How is each byte in an integer stored in CPU / memory?

i have tried this char c[4]; int i=89; memcpy(&c[0],&i,4); cout<<(int)c[0]<
Pyjong
  • 3,095
  • 4
  • 32
  • 50
7
votes
4 answers

is it possible to do memcpy in bits instead of bytes?

I would like to know is it possible to do memcpy by bits instead of bytes? I am writing a C code for Ethernet frame with VLAN tagging, in which I need to fill different values for VLAN header attributes (PCP-3bits,DEI-1bit,VID-12bits). How can I do…
arr
  • 101
  • 2
  • 10
7
votes
1 answer

Copying data to "cufftComplex" data struct?

I have data stored as arrays of floats (single precision). I have one array for my real data, and one array for my complex data, which I use as the input to FFTs. I need to copy this data into the cufftComplex data type if I want to use the CUDA…
phasedweasel
  • 171
  • 2
  • 6
7
votes
2 answers

How can I do equivalent of memcpy from a raw array to a std::vector?

I have a class that has (amongst many other things) a pointer to unsigned char that gets deleted and reallocated to store some data from another array. This done with a function class MyClass { private: unsigned char* m_Buffer; int…
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
6
votes
3 answers

Assignment: create my own memcpy. Why cast the destination and source pointers to unsigned char* instead of char*?

I'm trying to create my own versions of C functions and when I got to memcpy and memset I assumed that I should cast the destination and sources pointers to char *. However, I've seen many examples where the pointers were casted to unsigned char *…
6
votes
0 answers

Can you memcpy between non-overlapping regions of the same object?

C17 says the following about memcpy [7.24.2.1p2]: 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. The common…
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
6
votes
4 answers

Does memcpy not throw exceptions?

Hopefully there is a simple answer to this as it seems a simple question, however I have not been able to find any information on this on the interwebs. In the following code snippet, Visual Studio complains of unreachable code at the line "delete…
mgray88
  • 376
  • 1
  • 5
  • 10
6
votes
3 answers

Assignment or memcpy? What is the preferred approach to setting an array member variable?

For this example, I am working with objective-c, but answers from the broader C/C++ community are welcome. @interface BSWidget : NSObject { float tre[3]; } @property(assign) float* tre; . - (void)assignToTre:(float*)triplet { tre[0] =…
bitcruncher
  • 800
  • 1
  • 7
  • 14
6
votes
4 answers

atomic memcpy suggestion

While testing a program for scalability, I came across the situation where I have to make my memcpy operation as atomic operation . I have to copy 64bytes of data from one location to other . I came across one solution, that is using spinning over…
peeyush
  • 2,841
  • 3
  • 24
  • 43