memcpy

Go conversion between struct and byte array

醉酒当歌 提交于 2019-11-30 14:00:56
I am writing a client - server application in Go. I want to perform C-like type casting in Go. E.g. in Go type packet struct { opcode uint16 data [1024]byte } var pkt1 packet ... n, raddr, err := conn.ReadFromUDP(pkt1) // error here Also I want to perform C-like memcpy(), which will allow me to directly map the network byte stream received to a struct. e.g. with above received pkt1 type file_info struct { file_size uint32 // 4 bytes file_name [1020]byte } var file file_info if (pkt1.opcode == WRITE) { memcpy(&file, pkt1.data, 1024) } unsafe.Pointer is, well, unsafe, and you don't actually need

copy_to_user vs memcpy

别等时光非礼了梦想. 提交于 2019-11-30 13:57:50
问题 I have always been told(In books and tutorials) that while copying data from kernel space to user space, we should use copy_to_user() and using memcpy() would cause problems to the system. Recently by mistake i have used memcpy() and it worked perfectly fine with out any problems. Why is that we should use copy_to_user instead of memcpy() My test code(Kernel module) is something like this: static ssize_t test_read(struct file *file, char __user * buf, size_t len, loff_t * offset) { char ani

Fully optimized memcpy/memmove for Core 2 or Core i7 architecture?

风流意气都作罢 提交于 2019-11-30 12:43:59
The theoretical maximum of memory bandwidth for a Core 2 processor with DDR3 dual channel memory is impressive: According to the Wikipedia article on the architecture, 10+ or 20+ gigabytes per second. However, stock memcpy() calls do not attain this. (3 GB/s is the highest I've seen on such systems.) Likely, this is due to the OS vendor requirement that memcpy() be tuned for every processor line based on the processor's characteristics, so a stock memcpy() implementation should be reasonable on a wide number of brands and lines. My question: Is there a freely available, highly tuned version

C memcpy in reverse

邮差的信 提交于 2019-11-30 12:21:17
I am working with audio data. I'd like to play the sample file in reverse. The data is stored as unsigned ints and packed nice and tight. Is there a way to call memcpy that will copy in reverse order. i.e. if I had 1,2,3,4 stored in an array, could I call memcpy and magically reverse them so I get 4,3,2,1. This works for copying int s in reverse: void reverse_intcpy(int *restrict dst, const int *restrict src, size_t n) { size_t i; for (i=0; i < n; ++i) dst[n-1-i] = src[i]; } Just like memcpy() , the regions pointed-to by dst and src must not overlap. If you want to reverse in-place: void

How does the internal implementation of memcpy work?

前提是你 提交于 2019-11-30 11:05:16
How does the standard C function 'memcpy' work? It has to copy a (large) chunk of RAM to another area in the RAM. Since I know you cannot move straight from RAM to RAM in assembly (with the mov instruction) so I am guessing it uses a CPU register as the intermediate memory when copying? But how does it copy? By blocks (how would it copy by blocks?), by individual bytes (char) or the largest data type they have (copy in long long double's - which is 12 bytes on my system). EDIT: Ok apparently you can move data from RAM to RAM directly , I am not an assembly expert and all I have learnt about

Memcpy implementation, strict aliasing

送分小仙女□ 提交于 2019-11-30 10:20:54
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 implementation would be this since you can use a char: void memcpy4( void* dst , void* src , int size ) { for ( int

copy_to_user vs memcpy

老子叫甜甜 提交于 2019-11-30 09:03:16
I have always been told(In books and tutorials) that while copying data from kernel space to user space, we should use copy_to_user() and using memcpy() would cause problems to the system. Recently by mistake i have used memcpy() and it worked perfectly fine with out any problems. Why is that we should use copy_to_user instead of memcpy() My test code(Kernel module) is something like this: static ssize_t test_read(struct file *file, char __user * buf, size_t len, loff_t * offset) { char ani[100]; if (!*offset) { memset(ani, 'A', 100); if (memcpy(buf, ani, 100)) return -EFAULT; *offset = 100;

Is memcpy of a trivially-copyable type construction or assignment?

守給你的承諾、 提交于 2019-11-30 04:34:57
Let's say you have an object of type T and a suitably-aligned memory buffer alignas(T) unsigned char[sizeof(T)] . If you use std::memcpy to copy from the object of type T to the unsigned char array, is that considered copy construction or copy-assignment? If a type is trivially-copyable but not standard-layout, it is conceivable that a class such as this: struct Meow { int x; protected: // different access-specifier means not standard-layout int y; }; could be implemented like this, because the compiler isn't forced into using standard-layout: struct Meow_internal { private: ptrdiff_t x_offset

memcpy函数的实现

被刻印的时光 ゝ 提交于 2019-11-30 04:28:41
1.按1个字节拷贝 (1)不要直接使用形参,要转换成char* (2)目标地址要实现保存 (3)要考虑源和目标内存重叠的情况 void * mymemcpy(void *dest, const void *src, size_t count) { if (dest == NULL || src == NULL) return NULL; char *pdest = static_cast <char*>(dest); const char *psrc = static_cast <const char*>(psrc); int n = count; if (pdest > psrc && pdest < psrc+count) { for (size_t i=n-1; i != -1; --i) { pdest[i] = psrc[i]; } } else { for (size_t i= 0; i < n; i++) { pdest[i] = psrc[i]; } } return dest; } 2.按4个字节拷贝 (1)转成int*,按照每次4个字节来拷贝,不足四个字节的按一个字节拷贝 void *mymemcpy(void *dst,const void *src,size_t num) { assert((dst!=NULL)&&(src!=NULL)); int

Acwing-169-数独2(搜索, 剪枝)

删除回忆录丶 提交于 2019-11-30 03:34:18
链接: https://www.acwing.com/problem/content/171/ 题意: 请你将一个16x16的数独填写完整,使得每行、每列、每个4x4十六宫格内字母A~P均恰好出现一次。 保证每个输入只有唯一解决方案。 思路: 每个坐标维护一个16位的数, 用来记录某个值是否使用. 对每个位置, 如果只能填一个,则直接填, 对空格如果不能填, 则返回. 对每一行, 只能在一个位置使用的值直接填, 同时一行不能覆盖a-p,则返回, 列, 块同理. 再从所有可行位置,找到一个可填值最少的开始枚举. 还要多开数组记录状态, 方便复原. 代码: #include <bits/stdc++.h> using namespace std; const int N = 16; char Map[N][N+1]; char TmpMap[N*N+1][N][N+1]; int State[N][N]; int TmpS1[N*N+1][N][N], TmpS2[N*N+1][N][N]; int Num[1<<N], Cnt[1<<N]; int cnt; int Lowbit(int x) { return x&(-x); } void Change(int x, int y, int p) { Map[x][y] = 'A'+p; for (int i = 0;i < N;i+