memcpy

How do I copy bytes into a struct variable in C#?

我的未来我决定 提交于 2019-12-12 02:25:35
问题 I have a struct abc and I want to copy bytes into a struct variable. Similar to memcpy in C/C++. I receive the bytes over socket and they are the bytes of the same struct abc variable. [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct abc { public int a; public int b; public float c; public char[] d; //30 size } 回答1: You can convert the byte array to your structure as follows : int size = Marshal.SizeOf(typeof(abc)); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(arr, 0,

use htonl convert a int number, and memcpy to a char*, but nothing

一笑奈何 提交于 2019-12-11 13:55:28
问题 code like this: int totalLen = 50; int usTest = htons(totalLen); char* strBuf = new char[totalLen ]; memcpy(strBuf,&usTest,sizeof(int)); after this,there is nothing in strBuf, why? but if I put a big number, like 100000001 , it will be OK? what's the problem? 来源: https://stackoverflow.com/questions/19818704/use-htonl-convert-a-int-number-and-memcpy-to-a-char-but-nothing

Check for changes in POD variables

风流意气都作罢 提交于 2019-12-11 13:52:42
问题 I'm looking for an efficient way to check if a POD variable is altered between two cycles. I've come up with this solution: class Foo { public: template<typename T> bool isChanged(T& entry); void endCycle(); private: std::map<void*,size_t> entryMap; // <Address orig.,Size> std::map<void*,void*>oldVals; // <Address orig., Address cpy.> }; template<typename T> bool Foo::isChanged(T& entry) { entryMap[&entry] = sizeof(T); if(oldVals[&entry] == NULL) return false; if(memcmp(&entry, oldVals[&entry

A templated 'strdup()'?

可紊 提交于 2019-12-11 07:31:07
问题 template<typename T> static T *anydup(const T *src, size_t len) { T *ptr = malloc(len * sizeof(T)); memcpy(ptr, src, (len * sizeof(T))); return ptr; } Is this proper? Can I expect any errors from this when using an int, long, etc.? I'm very new to generic programming and am trying to learn more. 回答1: No this is not proper ! When you have a malloc() in C++ code, you should become very suspicious: malloc() allocates memory, but doesn't properly create objects. The only way to work with such

memcpy causing 'exc bad access'

a 夏天 提交于 2019-12-11 07:15:40
问题 I'm trying to loop through an array and copy across data, but after 1023 loops, I get an exc bad access message thrown and I have a feeling it might be to do with my memory. In my loop, I need to append data to my totalValues array, so I did this: memcpy(totalValues + totalCopied, tempBuffer, 600 * sizeof(float)); This is done inside a loop and totalCopied keeps track of how much data has been appended to totalValues so that I know where to write from when the loop hits memcpy again. I'm not

What Does Adding One to a Character Array in C Do?

久未见 提交于 2019-12-11 06:25:22
问题 I'm looking through some code for learning purposes. I'm working through this portion of code. // e.g. const unsigned char data={0x1,0x7C ... } unsigned char buf[40]; memset(buf,0,40); buf[0] = 0x52; memcpy(buf+1, data, length); // What does buf+1 do in this situation? On the last line where memcpy is called what does buf+1 do? buf is a character array, so what does +1 do to it? 回答1: In C, every array name is a pointer, so buf here also means the pointer which point to buf[0].Then "buf+1"

c - memcpy and pointers. Still work. Why?

我的未来我决定 提交于 2019-12-11 03:57:53
问题 EDIT: What should I do to have a correct code then ? EDIT2: Ok, I correct the code below Context Fiddling with memcpy. Linux, 64 bits. gcc 4.8.x Code #include <stdio.h> #include <string.h> #include <stdlib.h> void d(char ** restrict a, char ** restrict b){ char r[20]; memcpy(r,*a, strlen(*a)+1); // it is the same thing as before since *c is equivalent to &r (or put simply r). char *restrict c = malloc(20); memcpy(c,*a, strlen(*a)+1); // that one bugs me. why b alone and not *b ?? // EDIT :

memcpy on gcc code sourcery for ARM

有些话、适合烂在心里 提交于 2019-12-11 03:49:12
问题 I have my code compiled using arm code sourcery (arm-none-eabi-gcc) ( I think Lite Edition). I define a struct variable inside a function, and do a memcpy like typedef struct { char src[6]; char dst[6]; uint16_t a; uint16_t b; uint32_t c; uint16_t d; } Info_t; Info_t Info; memcpy(Info.src, src, sizeof(Info.src)); memcpy(Info.dst, dst, sizeof(Info.dst)); The first memcpy goes through, but the second one is causing a abort. I heard that the gcc optimizes memcpy and is resulting in an non-

Error when dealing with memory - mremap_chunk: Assertion

我与影子孤独终老i 提交于 2019-12-11 02:04:14
问题 It seems like my previous post but issue here is different .. This is the C structure for problem - typedef struct ip_esp_private { /* keep track of things privately */ u_int32_t type; u_int32_t ivlen; u_int32_t icvlen; u_int32_t keylen; /* length of "Encryption key */ u_int32_t akeylen; /*length of authn key */ u_int32_t key[0]; /* encryption key and authentication key both */ } esp_private; The values are provided to structure contents at run time as follows - case 'k': /* Key */ length =

C# copy array of structs to array of doubles

别等时光非礼了梦想. 提交于 2019-12-10 22:35:42
问题 [StructLayout(LayoutKind.Sequential)] public struct Demo { double X; double Y; } var data = new Demo[128]; FillWithMeaningfulValues(data); double[] doubles; Copy(data, out doubles); // ? How do I copy the demo array into the doubles array without having to for(...) through each element? In C++, I would use memcpy, but in C# I did not find what I need in Marshal.Copy. void MethodIDoNotWantToUse(Demo[] demo, out double[] doubles) { doubles = new double[demo.Length * 2]; for(int i = 0, j = 0; i