memcpy

Difference between strncpy and memcpy?

China☆狼群 提交于 2019-11-27 19:07:32
How can i access s[7] in s ? I didn't observe any difference between strncpy and memcpy . If I want to print the output s , along with s[7] (like qwertyA ), what are the changes I have to made in the following code: #include <stdio.h> #include <stdlib.h> int main() { char s[10] = "qwerty", str[10], str1[10]; s[7] = 'A'; printf("%s\n",s); strncpy(str,s,8); printf("%s\n",str); memcpy(str1,s,8); printf("%s\n",str1); return 0; } /* O/P qwerty qwerty qwerty */ Others have pointed out your null-termination problems. You need to understand null-termination before you understand the difference between

How to increase performance of memcpy

北城余情 提交于 2019-11-27 17:03:26
Summary: memcpy seems unable to transfer over 2GB/sec on my system in a real or test application. What can I do to get faster memory-to-memory copies? Full details: As part of a data capture application (using some specialized hardware), I need to copy about 3 GB/sec from temporary buffers into main memory. To acquire data, I provide the hardware driver with a series of buffers (2MB each). The hardware DMAs data to each buffer, and then notifies my program when each buffer is full. My program empties the buffer (memcpy to another, larger block of RAM), and reposts the processed buffer to the

Why does the speed of memcpy() drop dramatically every 4KB?

孤人 提交于 2019-11-27 17:03:17
I tested the speed of memcpy() noticing the speed drops dramatically at i*4KB. The result is as follow: the Y-axis is the speed(MB/second) and the X-axis is the size of buffer for memcpy() , increasing from 1KB to 2MB. Subfigure 2 and Subfigure 3 detail the part of 1KB-150KB and 1KB-32KB. Environment: CPU : Intel(R) Xeon(R) CPU E5620 @ 2.40GHz OS : 2.6.35-22-generic #33-Ubuntu GCC compiler flags : -O3 -msse4 -DINTEL_SSE4 -Wall -std=c99 I guess it must be related to caches, but I can't find a reason from the following cache-unfriendly cases: Why is my program slow when looping over exactly 8192

Can I call memcpy() and memmove() with “number of bytes” set to zero?

半腔热情 提交于 2019-11-27 12:31:59
Do I need to treat cases when I actully have nothing to move/copy with memmove() / memcpy() as edge cases int numberOfBytes = ... if( numberOfBytes != 0 ) { memmove( dest, source, numberOfBytes ); } or should I just call the function without checking int numberOfBytes = ... memmove( dest, source, numberOfBytes ); Is the check in the former snippet necessary? From the C99 standard (7.21.1/2): Where an argument declared as size_t n specifies the length of the array for a function, n can have the value zero on a call to that function. Unless explicitly stated otherwise in the description of a

Copying data to “cufftComplex” data struct?

岁酱吖の 提交于 2019-11-27 09:31:14
I have data stored as arrays of floats (single precision). I have one array for my real data, and one array for my complex data, which I use as the input to FFTs. I need to copy this data into the cufftComplex data type if I want to use the CUDA cufft library. From nVidia: " cufftComplex is a single‐precision, floating‐point complex data type that consists of interleaved real and imaginary components." Data to be operated on by cufft is stored in arrays of cufftComplex . How do I quickly copy my data from a normal C array into an array of cufftComplex ? I don't want to use a for loop because

strcpy vs. memcpy

天大地大妈咪最大 提交于 2019-11-27 09:03:22
问题 What is the difference between memcpy() and strcpy() ? I tried to find it with the help of a program but both are giving the same output. int main() { char s[5]={'s','a','\0','c','h'}; char p[5]; char t[5]; strcpy(p,s); memcpy(t,s,5); printf("sachin p is [%s], t is [%s]",p,t); return 0; } Output sachin p is [sa], t is [sa] 回答1: what could be done to see this effect Compile and run this code: void dump5(char *str); int main() { char s[5]={'s','a','\0','c','h'}; char membuff[5]; char strbuff[5]

memcpy vs assignment in C

白昼怎懂夜的黑 提交于 2019-11-27 08:17:13
Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware? I am using GCC 4.2.x on a 32 bit Intel platform (but am interested in 64 bit as well). You should never expect them outperform assignments. The reason is, the compiler will use memcpy anyway when it thinks it would be faster (if you use optimize flags). If not and if the structure is reasonable small that it fits into registers, direct register manipulation could be used which wouldn't require any memory access at all. GCC has special block-move patterns internally that figure out when to

Copy 2D array using memcpy?

前提是你 提交于 2019-11-27 08:10:10
So I want to copy the contents of a 2D array to another array of the exact same type. Here is how the array is created: GridUnit** newGrid; newGrid = new GridUnit*[width]; for (int i = 0; i < width; i++) newGrid[i] = new GridUnit[height]; GridUnit is size 16 (4 floats). So that's all initialised fine, no problems with using it as it is after I have ran the for loops to actually fill the values with some data. Now what I want to do is copy the contents of another array into this one (without for loops if possible). This is what I have been trying to do so far: memcpy(&newGrid, &grid, height *

Overwriting an object with an object of same type

南楼画角 提交于 2019-11-27 07:58:51
问题 Is the following well defined? #include <iostream> #include <string.h> using namespace std; struct Const { const int i; Const (int i) : i(i) {} int get0() { return 0; } // best accessor ever! }; int main() { Const *q,*p = new Const(1); new (p) Const(2); memcpy (&q, &p, sizeof p); cout << q->i; return 0; } Note that after construction of second Const , p doesn't semantically (intentionally?) points to new object, and the first is gone, so it is usable "as a void* ". But the second object is

What header should I include for memcpy and realloc?

早过忘川 提交于 2019-11-27 07:42:59
问题 I am porting a project to the iPhone and it uses realloc and memcpy which are not found. What is the header to include? It's a project mixing Objective C and C++ and I am starting to be lost. Thanks in advance for your help! 回答1: In C: #include <string.h> // memcpy #include <stdlib.h> //realloc In C++, remove the .h and prefix with a c . In C++, they will be placed in the std namespace, but are also global. 回答2: In C++ it's more idiomatic to use std::copy than C's memcpy , although the latter