memcpy

use of memcpy to store data from buffer into struct

寵の児 提交于 2019-12-20 06:54:58
问题 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

why cant I use const arguments in memcpy?

天涯浪子 提交于 2019-12-20 02:27:26
问题 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. 回答1: The reason is in the documentation you linked: double & operator[] (unsigned int index) double

Memcpy of native array to managed array in C++ CLI

自古美人都是妖i 提交于 2019-12-19 16:35:07
问题 Am I doing this right? I get a pointer to a native array and need to copy to a managed array. Use memcpy() with a pin_ptr. unsigned char* pArray; unsigned int arrayCount; // get pArray & arrayCount (from a COM method) ManagedClass->ByteArray = gcnew array<Byte,1>(arrayCount) pin_ptr<System::Byte> pinPtrArray = &ManagedClass->ByteArray[0]; memcpy_s(pinPtrArray, arrayCount, pArray, arrayCount); arrayCount is the actual length of pArray, so not really worried about that aspect. Looked at the

GCC with -fno-builtin does not seem to work

笑着哭i 提交于 2019-12-19 02:14:33
问题 I would like to compare the GCC builtin function memcpy versus the one one from libc. However, all iterations of -fno-builtin or -fno-builtin-memcpy seem to be ignored. //g++ -O3 foo.cpp -S or //g++ -O3 -fno-builtin foo.cpp -S #include <string.h> int main() { volatile int n = 1000; //int n = 1000; float *x = new float[1000]; float *y = new float[1000]; memcpy(y,x,sizeof(float)*n); //__builtin_memcpy(y,x,sizeof(float)*n); } What I have found is that if n in the source code above is not

How to memcpy a part of a two dimensional array in C?

為{幸葍}努か 提交于 2019-12-18 19:42:45
问题 How to memcpy the two dimensional array in C: I have a two dimensional array: int a[100][100]; int c[10][10]; I want to use memcpy to copy the all the values in array c to array a, how to do this using memcpy? int i; for(i = 0; i<10; i++) { memcpy(&a[i][10], c, sizeof(c)); } is this correct? 回答1: That should work : int i; for(i = 0; i<10; i++) { memcpy(&a[i], &c[i], sizeof(c[0])); } 回答2: I don't think it's correct, no. There's no way for memcpy() to know about the in-memory layout of a and

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

瘦欲@ 提交于 2019-12-18 15:31:24
问题 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

How does the internal implementation of memcpy work?

霸气de小男生 提交于 2019-12-18 14:08:14
问题 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

What are real significant cases when memcpy() is faster than memmove()?

自闭症网瘾萝莉.ら 提交于 2019-12-18 12:54:15
问题 The key difference between memcpy() and memmove() is that memmove() will work fine when source and destination overlap. When buffers surely don't overlap memcpy() is preferable since it's potentially faster. What bothers me is this potentially . Is it a microoptimization or are there real significant examples when memcpy() is faster so that we really need to use memcpy() and not stick to memmove() everywhere? 回答1: At best, calling memcpy rather than memmove will save a pointer comparison and

C strcpy() - evil?

£可爱£侵袭症+ 提交于 2019-12-18 10:13:25
问题 Some people seem to think that C's strcpy() function is bad or evil. While I admit that it's usually better to use strncpy() in order to avoid buffer overflows, the following (an implementation of the strdup() function for those not lucky enough to have it) safely uses strcpy() and should never overflow: char *strdup(const char *s1) { char *s2 = malloc(strlen(s1)+1); if(s2 == NULL) { return NULL; } strcpy(s2, s1); return s2; } *s2 is guaranteed to have enough space to store *s1 , and using

Copying (using assignment) a structure to a structure inside a union causing seg fault

耗尽温柔 提交于 2019-12-18 09:34:52
问题 I wrote the following code: #include <iostream> #include <string> #include <cstring> struct bar { std::string s3; std::string s4; }Bar; union foo { char * s1; char * s2; bar b1; foo(){}; ~foo(){}; }Foo; int main () { foo f1; bar b2; std::string temp("s3"); b2.s3 = temp; b2.s4 = temp; //f1.b1 = b2; //-- This Fails (Seg faults) /* #0 0x00002b9fede74d25 in std::string::_Rep::_M_dispose(std::allocator<char> const&) [clone .part.12] () from /usr/local/lib64/libstdc++.so.6 #1 0x00002b9fede75f09 in