memcpy

Understanding the copy done by memcpy()

最后都变了- 提交于 2020-01-01 05:31:23
问题 I have to create an image which has two times the number of columns to that of the original image. Therefore, I have kept the width of the new image two times to that of the original image. Although this is a very simple task and I have already done it but I am wondering about the strange results obtained by doing this task using memcpy() . My code: int main() { Mat image = imread("pikachu.png", 1); int columns = image.cols; int rows = image.rows; Mat twoTimesImage(image.rows, 2 * image.cols,

memcpy vs assignment in C — should be memmove?

大城市里の小女人 提交于 2020-01-01 04:41:07
问题 As pointed out in an answer to this question, the compiler (in this case gcc-4.1.2, yes it's old, no I can't change it) can replace struct assignments with memcpy where it thinks it is appropriate. I'm running some code under valgrind and got a warning about memcpy source/destination overlap. When I look at the code, I see this (paraphrasing): struct outer { struct inner i; // lots of other stuff }; struct inner { int x; // lots of other stuff }; void frob(struct inner* i, struct outer* o) {

Why is it allowed to overwrite a const variable using a pointer to it using memcpy?

北城以北 提交于 2019-12-31 02:24:05
问题 Why is it allowed to change a const variable using a pointer to it with memcpy? This code: const int i=5; int j = 0; memcpy(&j, &i, sizeof(int)); printf("Source: i = %d, dest: j = %d\n", i,j); j = 100; memcpy(&i, &j, sizeof(int)); printf("Source: j = %d, dest: i = %d\n", j,i); return 0; compiled with just a warning: warning: passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target type [enabled by default] But did run just fine, and changed the value of a const variable.

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

青春壹個敷衍的年華 提交于 2019-12-30 00:51:09
问题 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

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

家住魔仙堡 提交于 2019-12-30 00:51:09
问题 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

Is memcpy process-safe?

六月ゝ 毕业季﹏ 提交于 2019-12-29 06:19:59
问题 Ive looked online and have not been able to satisfy myself with an answer. Is memcpy threadsafe? (in Windows) What I mean is if I write to an area of memory shared between processes (using boost::shared_memory_object) using a single memcpy and then try read that area from another process using a single memcpy then will one process be blocked automatically while that write is happening? Where can I read about this? 回答1: memcpy is typically coded for raw speed. It will not be thread safe. If

Using memcpy to copy a range of elements from an array

风流意气都作罢 提交于 2019-12-29 06:00:24
问题 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? 回答1: 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. 回答2: memcpy(array, &matrix[80], 10*sizeof(double

POJ 3567 Eight II (八数码问题+bfs+康托展开)

。_饼干妹妹 提交于 2019-12-27 11:04:16
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3567 解题思路 这道题是 POJ 1077 Eight 的升级版, 区别在于POJ1077的终点是确定的,那么其他情况都是可以由这一种情况推出. 这道题的起点和终点 似乎 都是不确定的,如果暴力搜索的话一定会超时,这就很难办. 可以从另一个角度来思考, 我们根据起点中'X'的位置来进行分类,而其他位置的棋子用其编号(1~8)来表示即可,这样把起点从 \(9!\) 中情况缩小到了9种情况,以这9种情况事先进行一个搜索就可以了. 输出路径要求是字典最小序,以d, l, r, u的顺序搜索即可 搜索思路 这道题的另一个难点----也是八数码问题的核心----在于如何标识每一种情况, 方法是利用康托展开算法进行哈希 康托展开是 一个全排列到一个自然数 的双射,常用于构建哈希表时的空间压缩。 康托展开的实质是 计算当前排列在所有由小到大全排列中的顺序 ,因此是可逆的。 康托展开的转换公式: \(X=a_n(n-1)!+a_{n-1}(n-2)!+\cdots+a_1\cdot0!\) ,其中 \(a_i\) 为整数,并且 \(0\leq a_i<i,1\leq i\leq n\) 对公式的解释: 设 \(m_i\) 是数组中 从后往前数 第i个数, 则 \(a_i\) 是 \(m_i\)

Unix/C/C++--数据类型转换、格式化、cpy、精度

扶醉桌前 提交于 2019-12-26 19:30:37
Unix/C/C++--数据类型转换、格式化、cpy、精度 1 简介 2 等号赋值与memcpy 3 各类转换 3.1 unsigned char 2 float 3.2 unsigned char 2 string 3.3 float 2 char 3.4 float 2 unsigned char 4 指针传递 4.1 数组指针 4.1.1 示例一 4.1.2 示例二 5 格式化 5.1 格式化输出 5.2 设置小数点后面的位数 6 精度 6.1 float 参考 1 简介 各类场景下,需要各种数据转化。 2 等号赋值与memcpy strcpy等函数的逐字节拷贝,memcpy是按照机器字长逐字进行拷贝的,一个字等于4(32位机)或8(64位机)个字节。CPU存取一个字节和存取一个字一样,都是在一条指令、一个内存周期内完成的。显然,按字拷贝效率更高。 原来两者都是通过逐字拷贝来实现的。但是“等号赋值”被编译器翻译成一连串的MOV指令,而memcpy则是一个循环。“等号赋值”比memcpy快,并不是快在拷贝方式上,而是快在程序流程上。测试发现,“等号赋值”的长度必须小于等于128,并且是机器字长的倍数,才会被编译成连续MOV形式,否则会被编译成调用memcpy。而同样的,如果memcpy复制的长度小于等于128且是机器字长的整数倍,会被编译成MOV形式。所以,无论你的代码中如何写

C++char[]数组前几位拷贝到其他类型

梦想与她 提交于 2019-12-26 11:28:20
1、首先有一个char[]型数组 char a[0XFFFF];里面是你的数据 unsigned short num = NULL; memcpy(&num, recv_buf, 2); 第一行是申请的一个short类型的对象,short占两个字节,而char占一个字节,所有memcpy的最后一个参数是2。其他的以此类推。 来源: CSDN 作者: hua_hengxin 链接: https://blog.csdn.net/hua_hengixn/article/details/103709562