memcpy

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

别来无恙 提交于 2019-11-29 21:18:51
问题 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', etc.) using System; namespace CsByteCopy { class Program { class ExampleClass { public UInt64 field1; public UInt32 field2; public UInt16 field3; public byte[] field4 = new byte[18]; } static void Main(string[] args) { byte[] exampleData = { // These

How to split array into two arrays in C

我是研究僧i 提交于 2019-11-29 20:18:34
问题 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 回答1: Sure. The straightforward solution is to allocate two new arrays using malloc and then using memcpy to copy the data into the two arrays. int array[6] = {1,2,3,4,5,6} int *firstHalf = malloc(3 * sizeof(int)); if (!firstHalf) { /* handle error */ } int *secondHalf = malloc(3 * sizeof(int)); if (!secondHalf) { /* handle error */ }

C strcpy() - evil?

余生长醉 提交于 2019-11-29 20:12:17
Some people seem to think that C's strcpy() function is bad or evil. While I admit that it's usually better to use strncpy() in order to avoid buffer overflows, the following (an implementation of the strdup() function for those not lucky enough to have it) safely uses strcpy() and should never overflow: char *strdup(const char *s1) { char *s2 = malloc(strlen(s1)+1); if(s2 == NULL) { return NULL; } strcpy(s2, s1); return s2; } *s2 is guaranteed to have enough space to store *s1 , and using strcpy() saves us from having to store the strlen() result in another function to use later as the

Copy a std::vector to a repeated field from protobuf with memcpy

。_饼干妹妹 提交于 2019-11-29 18:56:45
问题 At first I have this simple protobuf file message messagetest { ... repeated float samples = 6; .... } Which creates a headerfile with this methods //repeated float samples = 6; inline int samples_size() const; inline void clear_samples(); static const int kSamplesFieldNumber = 6; inline float samples(int index) const; inline void set_samples(int index, float value); inline void add_samples(float value); inline const ::google::protobuf::RepeatedField< float >& samples() const; inline ::google

Please look into this inexplicable behavior and output of memcpy() for overlapping memory blocks

有些话、适合烂在心里 提交于 2019-11-29 18:52:06
After reading the following about memcpy() , I proceeded to read about memmove() : To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach). (LINK) And after checking the program used to illustrate the working of memmove() I decided to tweak it by using memcpy() instead to see how different is the output.To my surprise,they are the same even if it's a case of overlapping memory blocks.Here is the program and the output,and I have proceeded to

Fastest de-interleave operation in C?

霸气de小男生 提交于 2019-11-29 17:47:12
问题 I have a pointer to an array of bytes mixed that contains the interleaved bytes of two distinct arrays array1 and array2 . Say mixed looks something like this: a1b2c3d4... What I need to do is de-interleave the bytes so I get array1 = abcd... and array2 = 1234... . I know the length of mixed ahead of time, and the lengths of array1 and array2 are equivalent, both equal to mixed / 2 . Here is my current implementation ( array1 and array2 are already allocated): int i, j; int mixedLength_2 =

Copying (using assignment) a structure to a structure inside a union causing seg fault

依然范特西╮ 提交于 2019-11-29 16:47:51
I wrote the following code: #include <iostream> #include <string> #include <cstring> struct bar { std::string s3; std::string s4; }Bar; union foo { char * s1; char * s2; bar b1; foo(){}; ~foo(){}; }Foo; int main () { foo f1; bar b2; std::string temp("s3"); b2.s3 = temp; b2.s4 = temp; //f1.b1 = b2; //-- This Fails (Seg faults) /* #0 0x00002b9fede74d25 in std::string::_Rep::_M_dispose(std::allocator<char> const&) [clone .part.12] () from /usr/local/lib64/libstdc++.so.6 #1 0x00002b9fede75f09 in std::string::assign(std::string const&) () from /usr/local/lib64/libstdc++.so.6 #2 0x0000000000400ed1

Memcpy implementation, strict aliasing

随声附和 提交于 2019-11-29 15:38:29
问题 While learning c I have implemented my own memcpy functions. I have used a wider type( uint32_t ) in the function. (For simplicity the function is restricted to types that are multiples of 4 and the data is properly aligned ) void memcpy4( void* dst , void* src , int size ) { size /= 4; for ( int i = 0 ; i < size ; i++ ) ((uint32_t*)dst)[i] = ((uint32_t*)src)[i]; } I did some reading on type punning and strict aliasing and I believe the function above breaks the rule. The correct

Is it undefined behaviour to memcpy from an uninitialized variable?

若如初见. 提交于 2019-11-29 13:22:37
Is using an uninitialized variable as the src for memcpy undefined behaviour in C? void foo(int *to) { int from; memcpy(to, &from, sizeof(from)); } Shafik Yaghmour The C committee proposed response to defect report 451: instability of uninitialized automatic variables is: The answer to question 3 is that library functions will exhibit undefined behavior when used on indeterminate values. The question in the defect had sought an exemption for memcpy and fwrite if this was indeed the case saying: [...] The fact that one wants to be able to copy uninitialized padding bytes in structs using memcpy

Understanding the implementation of memcpy()

隐身守侯 提交于 2019-11-29 09:34:26
I was looking the implementation of memcpy.c, I found a different memcpy code. I couldnt understand why do they do (((ADDRESS) s) | ((ADDRESS) d) | c) & (sizeof(UINT) - 1) #if !defined(__MACHDEP_MEMFUNC) #ifdef _MSC_VER #pragma function(memcpy) #undef __MEMFUNC_ARE_INLINED #endif #if !defined(__MEMFUNC_ARE_INLINED) /* Copy C bytes from S to D. * Only works if non-overlapping, or if D < S. */ EXTERN_C void * __cdecl memcpy(void *d, const void *s, size_t c) { if ((((ADDRESS) s) | ((ADDRESS) d) | c) & (sizeof(UINT) - 1)) { BYTE *pS = (BYTE *) s; BYTE *pD = (BYTE *) d; BYTE *pE = (BYTE *) ((