memcpy

gcc optimising away compound statement

随声附和 提交于 2019-12-13 06:22:37
问题 I am having an issue with implementing a push_back operator for a generic resizable vector in c. For genericity I need to use a void pointer as an argument, but in practice I want to give it values directly. When I compile the below code with gcc -o t test.c -std=c99 , it prints 10 as I expect. When I add -O1 (or higher) to the compile options, the program prints 0 . I think the problem is in the smemcpy code, as when I replace it with memcpy I no longer have this problem. Simplified code:

Does moving values of one type with another type violate strict aliasing?

瘦欲@ 提交于 2019-12-12 19:03:43
问题 Does it violate strict aliasing rules to move items of any type around using uint32_t, then read them back? If so, does it also violate strict aliasing rules to memcpy from an array of uint32_ts to an array of any type, then read the elements back? The following code sample demonstrates both cases: #include <assert.h> #include <stdio.h> #include <stdint.h> #include <string.h> int main(void) { const char *strings[5] = { "zero", "one", "two", "three", "four" }; uint32_t buffer[5]; int i; assert

Avoid copying an array when using mexCallMATLAB

只谈情不闲聊 提交于 2019-12-12 14:43:34
问题 I have written a mex file for MATLAB. It calls MATLAB pinv function to calculate the Moore Penrose pseudoinverse. I named this function my_pinv . my_pinv gets an array and returns its pseudoinverse, exactly similar to pinv : A = magic(8); A = A(:,1:6) b = 260*ones(8,1) x = my_pinv(A)*b In the mex file, however, I have to copy the values of the input array to be able to use mexCallMATLAB . Here is the content of my_pinv.cpp : #include <matrix.h> #include <mex.h> #include <string.h> void

Sending C structs through TCP in C#

倖福魔咒の 提交于 2019-12-12 12:07:53
问题 I am writing a program to interact with the management interface for a piece of equipment over TCP. The problem is, the documentation for the equipment is written in C, while the program I am writing is in C#. My problem is, the documentation specifies The communication is based upon the C structure-based API buffer No amount of Googling can seem to point me to this API or how I send a raw structure across TCP. The documentation seems to imply that I should use memcpy to copy the struct to

Fast copy every second byte to new memory area

痞子三分冷 提交于 2019-12-12 11:45:12
问题 I need a fast way to copy every second byte to a new malloc'd memory area. I have a raw image with RGB data and 16 bits per channel (48 bit) and want to create an RGB image with 8 bits per channel (24 bit). Is there a faster method than copying bytewise? I don't know much about SSE2, but I suppose it's possible with SSE/SSE2. 回答1: Your RGB data is packed, so we don't actually have to care about pixel boundaries. The problem is just packing every other byte of an array. (At least within each

Concatenate two arrays using void pointer (C)

左心房为你撑大大i 提交于 2019-12-12 11:09:21
问题 I want to concatenate two arrays of the same type into a single new array with the same type. But the problem is I have to use void pointers, and somehow my code won't work from the third element on. I searched a bit on the internet but seems like noone is having this problem #include <stdio.h> #include <stdlib.h> #include <string.h> void array_float_fill(float *arr1, float *arr2, int n, int m){ for(int i = 0;i<n;i++){ *(arr1+i)=i+n; } for(int i = 0;i<m;i++){ *(arr2+i)=i+m; } } void array

Is `memcpy((void *)dest, src, n)` with a `volatile` array safe?

烈酒焚心 提交于 2019-12-12 07:26:47
问题 I have a buffer that I use for UART, which is declared this way: union Eusart_Buff { uint8_t b8[16]; uint16_t b9[16]; }; struct Eusart_Msg { uint8_t msg_posn; uint8_t msg_len; union Eusart_Buff buff; }; struct Eusart { struct Eusart_Msg tx; struct Eusart_Msg rx; }; extern volatile struct Eusart eusart; And here is the function that fills the buffer (which will be sent using interrupts): void eusart_msg_transmit (uint8_t n, void *msg) { if (!n) return; /* * The end of the previous transmission

Go conversion between struct and byte array

前提是你 提交于 2019-12-12 07:14:56
问题 I am writing a client - server application in Go. I want to perform C-like type casting in Go. E.g. in Go type packet struct { opcode uint16 data [1024]byte } var pkt1 packet ... n, raddr, err := conn.ReadFromUDP(pkt1) // error here Also I want to perform C-like memcpy(), which will allow me to directly map the network byte stream received to a struct. e.g. with above received pkt1 type file_info struct { file_size uint32 // 4 bytes file_name [1020]byte } var file file_info if (pkt1.opcode ==

Best way to add a char prefix to a char* in C++? [duplicate]

一笑奈何 提交于 2019-12-12 04:48:53
问题 This question already has answers here : Prepending to a string (8 answers) Closed 6 years ago . I need to add a prefix ('X') to the char* (" is cool"). What is the BEST way to do this? What is the easiest way? char a = 'X'; char* b= " is cool"; I need: char* c = "X is cool"; So far I tried strcpy-strcat, memcpy; I'm aware this sounds as a stupid, unresearched question. What I was wondering is whether there is a way to add the char to the array without turning the char into a string. 回答1: How

C++ - Buffer combining adding extra empty values

不羁的心 提交于 2019-12-12 04:08:47
问题 I am trying to fill two buffers, an index buffer object and a vertex buffer object in C++. // Create the IBO and VBO data GLushort* iboData = new GLushort[polyProcessed * 3]; Vertex* vboData = new Vertex[vertProcessed]; int iboPos = 0; int vboPos = 0; // Create the VBO and IBO for(int i = 0; i < fragMap[0x36]; i++) { // Copy the data to the IBO memcpy(iboData + iboPos, zmeshes[i].indices, zmeshes[i].numPoly * 3 * sizeof(GLushort));//sizeof(*zmeshes[i].indices)); // Advance the position iboPos