memcpy

Using memcpy in C++

感情迁移 提交于 2019-12-23 07:30:15
问题 I am little confused on the parameters for the memcpy function. If I have int* arr = new int[5]; int* newarr = new int[6]; and I want to copy the elements in arr into newarr using memcopy , memcpy(parameter, parameter, parameter) How do I do this? 回答1: So the order is memcpy(destination, source, number_of_bytes) . Therefore, you can place the old data at the beginning of newarr with memcpy(newarr, arr, 5 * sizeof *arr); /* sizeof *arr == sizeof arr[0] == sizeof (int) */ or at the end with

Understanding how to memcpy with TheUnsafe

孤者浪人 提交于 2019-12-23 04:44:32
问题 I read stuff about TheUnsafe, but I get confused by the fact that, unlike C/C++ we have to work out the offset of stuff, and there's also the 32bits VM vs the 64bits VM, which may or may not have different pointers sizes depending on a particular VM setting being turned on or off (also, I'm assuming all offsets to data are actually based on pointer arithmetic this would influence them to). Unfortunately, it seems all the stuff ever written about how to use TheUnsafe stems from one article

convert vector<bool> to int

大城市里の小女人 提交于 2019-12-22 10:59:44
问题 I have a vector of bool which I want to copy in a int container of bigger size. Is there a fast way to do this? To clarify, is there a smarter way to achieve this? #include <vector> #include <cstdint> #include <iostream> #include <climits> #include <cassert> inline size_t bool2size_t(std::vector<bool> in) { assert(sizeof(size_t)*CHAR_BIT >= in.size()); size_t out(0); for (size_t vecPos = 0; vecPos < in.size(); vecPos++) { if (in[vecPos]) { out += 1 << vecPos; } } return out; } int main () {

When a union object is copied, is a member subobject created?

瘦欲@ 提交于 2019-12-22 08:40:08
问题 When another member of a union is accessed, the C++ standard used to be silent on what happens, but that was fixed to explain that member access to a union object was allowed for the purpose of assigning to that yet no existent object, which would magically create the object, by assignment to it or one of its members. Essentially the member access operator returns a promise of a future object and you have to use it with an assignment. Given union U { int i; long l; }; We can create a i or l

Make compiler copy characters using movsd

梦想与她 提交于 2019-12-21 11:01:35
问题 I would like to copy a relatively short sequence of memory (less than 1 KB, typically 2-200 bytes) in a time critical function. The best code for this on CPU side seems to be rep movsd . However I somehow cannot make my compiler to generate this code. I hoped (and I vaguely remember seeing so) using memcpy would do this using compiler built-in intrinsics, but based on disassembly and debugging it seems compiler is using call to memcpy/memmove library implementation instead. I also hoped the

C memcpy() a function

断了今生、忘了曾经 提交于 2019-12-21 05:02:05
问题 Is there any method to calculate size of a function? I have a pointer to a function and I have to copy entire function using memcpy. I have to malloc some space and know 3rd parameter of memcpy - size. I know that sizeof(function) doesn't work. Do you have any suggestions? 回答1: Functions are not first class objects in C. Which means they can't be passed to another function, they can't be returned from a function, and they can't be copied into another part of memory. A function pointer though

Which type trait would indicate that type is memcpy assignable? (tuple, pair)

情到浓时终转凉″ 提交于 2019-12-20 20:31:34
问题 I would like to know what type introspection I can do to detect types that assignable by simply raw memory copy? For example, as far I understand, built-in types tuples of built-in types and tuple of such tuples, would fall in this category. The motivation is that I want to transport raw bytes if possible. T t1(...); // not necessarely default constructible T t2(...); t1 = t2; // should be equivalent to std::memcpy(&t1, &t2, sizeof(T)); // t1 is now an (independent) copy of the value of t2,

c++ memcpy return value

橙三吉。 提交于 2019-12-20 16:19:37
问题 according to http://en.cppreference.com/w/cpp/string/byte/memcpy c++'s memcpy takes three parameters: destination, source and size/bytes. it also returns a pointer. why is that so? aren't the parameters enough to input and copy data. or am i misunderstanding something? the examples don't use the return value 回答1: If a function has nothing specific to return, it is often customary to return one of the input parameters (the one that is seen as the primary one). Doing this allows you to use

Cuda object copy

邮差的信 提交于 2019-12-20 07:29:29
问题 I'm trying to use CUDA with objects, this is a little test code i put together to try out things, but i ran into a problem. When i'm doing anything to the device version of the variable, the copy back to the host fails with "cuda Error Ilegal Address", but if i just copy the code to the device and back it works. If i comment out the printf... line, it the works. class A { public: int s; }; __device__ A *d_a; __global__ void MethodA() { printf("%d\n", d_a->s); } int main() { A *a = new A(); a-

use of memcpy to store data from buffer into struct

╄→尐↘猪︶ㄣ 提交于 2019-12-20 06:57:44
问题 I have sflow packet capture code in which I need to print the sflow data information from buffer. I have defined the structs for the required information and trying to use memcpy to copy the buffer information into the struct. when I print the fields, I get some big value which isn't the correct one. The have attached the struct code below: typedef unsigned char mac[6]; typedef unsigned char ip_v4[4]; typedef unsigned char ip_v6[16]; typedef unsigned int header_protocol; /* Packet header data