memcpy

Using memcpy to copy a range of elements from an array

情到浓时终转凉″ 提交于 2019-11-29 03:47:51
Say we have two arrays: double *matrix=new double[100]; double *array=new double[10]; And we want to copy 10 elements from matrix[80:89] to array using memcpy . Any quick solutions? It's simpler to use std::copy : std::copy(matrix + 80, matrix + 90, array); This is cleaner because you only have to specify the range of elements to be copied, not the number of bytes. In addition, it works for all types that can be copied, not just POD types. memcpy(array, &matrix[80], 10*sizeof(double)); But (since you say C++) you'll have better type safety using a C++ function rather than old C memcpy :

optimized memcpy

*爱你&永不变心* 提交于 2019-11-29 03:22:01
Are there faster alternatives to memcpy() in C++? Unlikely. Your compiler/standard library will likely have a very efficient and tailored implementation of memcpy. And memcpy is basically the lowest api there is for copying one part of memory to another. If you want further speedups, find a way to not need any memory copying. First, a word of advice. Assume that the people who wrote your standard library are not stupid. If there was a faster way to implement a general memcpy, they'd have done it. Second, yes, there are better alternatives. In C++, use the std::copy function. It does the same

When __builtin_memcpy is replaced with libc's memcpy

旧城冷巷雨未停 提交于 2019-11-29 03:18:45
There is a version of C99/posix memcpy function in GCC: __builtin_memcpy . Sometimes it can be replaced by GCC to inline version of memcpy and in other cases it is replaced by call to libc's memcpy. E.g. it was noted here : Finally, on a compiler note, __builtin_memcpy can fall back to emitting a memcpy function call. What is the logic in this selection? Is it logic the same in other gcc-compatible compilers, like clang/llvm, intel c++ compiler, PCC, suncc (oracle studio)? When I should prefer of using __builtin_memcpy over plain memcpy? C2H5OH I had been experimenting with the builtin

memcpy与大小端

試著忘記壹切 提交于 2019-11-29 02:41:58
搬运自我的CSDN: https://blog.csdn.net/u013213111/article/details/100149145 参考: 大端 小端 与memcpy 网络字节序和大小端字节序 来看这样一段代码: 本意是想把uint8_t a[2]中的内容合成一个uint16_t b #include <stdlib.h> #include <stdio.h> #include <stdint.h> int main() { uint8_t a[2] = { 0x11, 0x22 }; uint16_t b; memcpy(&b, a, sizeof(b)); printf("a[0]:%x,a[1]:%x\n", a[0], a[1]); printf("b:%x\n", b); return 0; } 在intel的电脑上跑出来的结果如何呢: a[0]:11,a[1]:22 b:2211 也就是说,假如a[1]中存储的是高位byte,a[0]中存储的是低位byte,那么最终合成的uint16_t是正确的;而假如a[0]中存储的是高位byte,a[1]中存储的是低位byte,那么结果就是错误的了。 然而并不是所有的计算机系统都如此,而是要看系统是大端存储模式还是小端存储模式。 小端存储模式(小端字节序),指的是数据的高字节位置保存在内存的高地址处

faster alternative to memcpy?

两盒软妹~` 提交于 2019-11-28 16:59:48
I have a function that is doing memcpy, but it's taking up an enormous amount of cycles. Is there a faster alternative/approach than using memcpy to move a piece of memory? memcpy is likely to be the fastest way you can copy bytes around in memory. If you need something faster - try figuring out a way of not copying things around, e.g. swap pointers only, not the data itself. This is an answer for x86_64 with AVX2 instruction set present. Though something similar may apply for ARM/AArch64 with SIMD. On Ryzen 1800X with single memory channel filled completely (2 slots, 16 GB DDR4 in each), the

strcpy vs. memcpy

北战南征 提交于 2019-11-28 15:12:28
What is the difference between memcpy() and strcpy() ? I tried to find it with the help of a program but both are giving the same output. int main() { char s[5]={'s','a','\0','c','h'}; char p[5]; char t[5]; strcpy(p,s); memcpy(t,s,5); printf("sachin p is [%s], t is [%s]",p,t); return 0; } Output sachin p is [sa], t is [sa] what could be done to see this effect Compile and run this code: void dump5(char *str); int main() { char s[5]={'s','a','\0','c','h'}; char membuff[5]; char strbuff[5]; memset(membuff, 0, 5); // init both buffers to nulls memset(strbuff, 0, 5); strcpy(strbuff,s); memcpy

Overwriting an object with an object of same type

只谈情不闲聊 提交于 2019-11-28 13:51:27
Is the following well defined? #include <iostream> #include <string.h> using namespace std; struct Const { const int i; Const (int i) : i(i) {} int get0() { return 0; } // best accessor ever! }; int main() { Const *q,*p = new Const(1); new (p) Const(2); memcpy (&q, &p, sizeof p); cout << q->i; return 0; } Note that after construction of second Const , p doesn't semantically (intentionally?) points to new object, and the first is gone, so it is usable "as a void* ". But the second object is constructed at the exact same address, so the bit pattern of p represents the address of the new object.

MBW内存测试

家住魔仙堡 提交于 2019-11-28 13:29:29
MBW内存测试 https://www.cnblogs.com/dongdongwq/p/5431561.html 在测试前,理应了解本机所具备的特点,比如CPU频率、内存频率、内存大小,等等信息。 查看CPU用如下命令(多少个核,频率,特性等): cat /proc/cpuinfo 查看内存用如下命令:(只有当前内存大小,已用空间等等,不能看到内存频率) cat /proc/meminfo 要查看内存型号和频率等信息,用如下的命令: sudo dmidecode -t memory 查看cpu内核频率命令: cat /proc/cpuinfo |grep MHz|uniq 内存带宽的计算公式是:带宽=内存核心频率×内存总线位数×倍增系数。简化公式为:标称频率*位数。比如一条DDR3 1333MHz 64bit的内存,理论带宽为: 1333*64/8=10664MiB/s = 10.6GiB/s 。 常用命令: mbw -q -n 10 256 -n 10表示运行10次,256表示测试所用的内存大小,单位为MB。 mbw测试了MEMCPY、DUMB、MCBLOCK等方式的内存带宽。从测试结果看,前2都差不多,最后一种测试得到的带宽值比较高。 下面是使用taskset指定CPU核心和运行mbw次数的脚本。真正使用到的是taskset命令,它可以指定程序在哪个CPU核上跑

What header should I include for memcpy and realloc?

本秂侑毒 提交于 2019-11-28 13:15:30
I am porting a project to the iPhone and it uses realloc and memcpy which are not found. What is the header to include? It's a project mixing Objective C and C++ and I am starting to be lost. Thanks in advance for your help! In C: #include <string.h> // memcpy #include <stdlib.h> //realloc In C++, remove the .h and prefix with a c . In C++, they will be placed in the std namespace, but are also global. In C++ it's more idiomatic to use std::copy than C's memcpy , although the latter does work just as well. To get std::copy , you need to #include <algorithm> . There's not a direct C++

飞腾与龙芯的内存性能简单对比

蹲街弑〆低调 提交于 2019-11-28 13:14:50
1. 使用的工具 mbw 1. 安装方法: git clone http://github.com/raas/mbw cd mbw make 2.执行命令 ./mbw 16 -b 4096 2.1 龙芯机器 [root@NeoKylin mbw]# ./mbw -b 4096 16 Long uses 8 bytes. Allocating 2*2097152 elements = 33554432 bytes of memory. Using 4096 bytes as blocks for memcpy block copy test. Getting down to business... Doing 10 runs per test. 0 Method: MEMCPY Elapsed: 0.00831 MiB: 16.00000 Copy: 1925.855 MiB/s 1 Method: MEMCPY Elapsed: 0.00743 MiB: 16.00000 Copy: 2151.984 MiB/s 2 Method: MEMCPY Elapsed: 0.00725 MiB: 16.00000 Copy: 2207.810 MiB/s 3 Method: MEMCPY Elapsed: 0.00756 MiB: 16.00000 Copy: 2117.803 MiB