memcpy

C++中的内存操作

徘徊边缘 提交于 2019-12-02 05:44:40
1. memcpy # include <string.h> void * memcpy ( void * dest , void * src , unsigned int count ) 将源地址src指向内存区域的count个字节赋值到dest为起始地址的内存区域 src和dest所指内存区域不能重叠,函数返回指向dest的指针 注意,对象不能是简单的内存拷贝,例如C++中STL的字符串类型string,因为memcpy执行的是浅拷贝,只是简单的把第二个内存指向第一个内存的引用,而程序结束时,会对一块内存进行两次内存释放(析构函数)。 C++ String 使用注意: memcpy 2. memset # include <string.h> void * memset ( void * buffer , char c , int count ) 把buffer所指内存区域的前count个字节设置成字符c 返回指向buffer的指针 也适用于整数数组初始化为0或者-1,因为memset按照字节操作,有符号数的0为全0,有符号数的-1为全1,例如 int a [ 100 ] ; memset ( a , 0 , sizeof ( a ) ) ; int b [ 1000 ] ; memset ( b , - 1 , sizeof ( b ) ) ; 3. strcpy #

C++ vector容器和sort函数的学习

五迷三道 提交于 2019-12-02 04:58:53
Vector注意事项: 声明形如vector<> vec;的不是new出来而是作为普通变量的那么不需要delete, 在变量超出作用域时会自动回收 如果是用*vec = new vector<>()这种方式动态创建的vector那么需要delete vec vec里存放的元素如果不是指针那么不用delete, 这些元素在vec被释放时会被一起释放 vec里存放的元素是指针并且这些指针都是指向自己new的对象的话, 那么需要自己一个个delete c++中内存拷贝函数(C++ memcpy)详解 头文件:C++: #include<cstring> 原型: void * memcpy ( void *dest, const void *src, unsigned int count); //参数是void 功能:由src所指内存区域复制count个字节到dest所指内存区域。 说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。 函数原型: void * memcpy ( void *dest, const void *src, size_t n); 功能: 由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始地址的空间内。 头文件: #include<string.h> 返回值:   函数返回一个指向dest的指针。 说明:    1

how memcpy is handled by DMA in linux

折月煮酒 提交于 2019-12-02 02:31:35
问题 I am using memcpy() in my program. as I increase the number of variables, unfortunately the CPU usage increases. it is as if memcpy is run by using for loop iteration. is there a fast memcpy function in linux too? shall I use a patch and compile the kernel? 回答1: There are architectures where the bus between the CPU and memory is rather weak; some of those architectures add a DMA engine to allow big blocks of memory to be copied without having a loop running on the CPU. In Linux, you would be

how memcpy is handled by DMA in linux

旧街凉风 提交于 2019-12-02 01:03:24
I am using memcpy() in my program. as I increase the number of variables, unfortunately the CPU usage increases. it is as if memcpy is run by using for loop iteration. is there a fast memcpy function in linux too? shall I use a patch and compile the kernel? There are architectures where the bus between the CPU and memory is rather weak; some of those architectures add a DMA engine to allow big blocks of memory to be copied without having a loop running on the CPU. In Linux, you would be able to access the DMA engine with the dmaengine subsystem, but it is very hardware-dependent whether such

why cant I use const arguments in memcpy?

泄露秘密 提交于 2019-12-01 22:11:27
I have a struct cVector3d and I am memcpy 'ing it into a char array like this: void insert_into_stream(std::ostream& stream, cVector3d vector) { int length = sizeof(double)*3; char insert_buffer[sizeof(double)*3]; memcpy(insert_buffer, &vector[0], length); stream.write(insert_buffer, length); } If I use const cVector3d vector in the parameter list, than I get a " & requires l-value " error. The reason is in the documentation you linked: double & operator[] (unsigned int index) double operator[] (unsigned int index) const When you use the non-const version you get a l-value reference and you

Why does wmemcpy exist when memcpy should suffice?

三世轮回 提交于 2019-12-01 21:32:09
问题 wmemcpy appears to perform the same operations as memcpy but accepts wchar_t* instead of void* . How is its existence justified if these two code snippets should have the same behaviour? Does it have a useful purpose? memcpy(dest, wchar_array, sizeof(wchar_array)); And wmemcpy(dest, wchar_array, sizeof(wchar_array) / sizeof(wchar_t)); 回答1: In addition to davmac's answer about API symmetry and having the size of an array not always granted, it should be emphasized that the third argument of

Why does wmemcpy exist when memcpy should suffice?

依然范特西╮ 提交于 2019-12-01 19:06:01
wmemcpy appears to perform the same operations as memcpy but accepts wchar_t* instead of void* . How is its existence justified if these two code snippets should have the same behaviour? Does it have a useful purpose? memcpy(dest, wchar_array, sizeof(wchar_array)); And wmemcpy(dest, wchar_array, sizeof(wchar_array) / sizeof(wchar_t)); In addition to davmac's answer about API symmetry and having the size of an array not always granted, it should be emphasized that the third argument of wmemcpy refers to number of elements to copy (rather than bytes). If you work with wchar_t objects and handle

CUDA : How to copy a 3D array from host to device?

风流意气都作罢 提交于 2019-12-01 14:20:15
I want to learn how can i copy a 3 dimensional array from host memory to device memory. Lets say i have a 3d array which contains data. For example int host_data[256][256][256]; I want to copy that data to dev_data (a device array) in such a way so host_data[x][y][z]=dev_data[x][y][z]; How can i do it? and how am i supposed to access the dev_data array in the device? A simple example would be very helpfull. Ixanezis The common way is to flatten an array (make it one-dimensional). Then you'll have to make some calculations to map from (x,y,z) triple to one number - a position in a flattened one

CUDA : How to copy a 3D array from host to device?

坚强是说给别人听的谎言 提交于 2019-12-01 13:06:00
问题 I want to learn how can i copy a 3 dimensional array from host memory to device memory. Lets say i have a 3d array which contains data. For example int host_data[256][256][256]; I want to copy that data to dev_data (a device array) in such a way so host_data[x][y][z]=dev_data[x][y][z]; How can i do it? and how am i supposed to access the dev_data array in the device? A simple example would be very helpfull. 回答1: The common way is to flatten an array (make it one-dimensional). Then you'll have

atoi on a character array with lots of integers

China☆狼群 提交于 2019-12-01 11:57:47
I have a code in which the character array is populated by integers (converted to char arrays), and read by another function which reconverts it back to integers. I have used the following function to get the conversion to char array: char data[64]; int a = 10; std::string str = boost::lexical_cast<std::string>(a); memcpy(data + 8*k,str.c_str(),sizeof(str.c_str())); //k varies from 0 to 7 and the reconversion back to characters is done using: char temp[8]; memcpy(temp,data+8*k,8); int a = atoi(temp); This works fine in general, but when I try to do it as part of a project involving qt (ver 4.7