memcpy

Why doesn't memcpy work when copying a char array into a struct?

ⅰ亾dé卋堺 提交于 2020-01-06 20:01:29
问题 #define buffer 128 int main(){ char buf[buffer]=""; ifstream infile("/home/kevin/Music/test.mp3",ios::binary); infile.seekg(-buffer,ios::end); if(!infile || !infile.read(buf,buffer)){ cout<<"fail!"<<endl; } ID3v1 id3; cout<<sizeof(id3)<<endl; memcpy(&id3,buf,128); cout<<id3.header<<endl; } struct ID3v1{ char header[3]; char title[30]; char artist[30]; char album[30]; char year[4]; char comment[28]; bool zerobyte; bool track; bool genre; }; When I do the memcpy, it seems to be pushing too much

Why doesn't memcpy work when copying a char array into a struct?

左心房为你撑大大i 提交于 2020-01-06 20:00:11
问题 #define buffer 128 int main(){ char buf[buffer]=""; ifstream infile("/home/kevin/Music/test.mp3",ios::binary); infile.seekg(-buffer,ios::end); if(!infile || !infile.read(buf,buffer)){ cout<<"fail!"<<endl; } ID3v1 id3; cout<<sizeof(id3)<<endl; memcpy(&id3,buf,128); cout<<id3.header<<endl; } struct ID3v1{ char header[3]; char title[30]; char artist[30]; char album[30]; char year[4]; char comment[28]; bool zerobyte; bool track; bool genre; }; When I do the memcpy, it seems to be pushing too much

一些常用函数:memset、memcpy、strlen、strcpy

风格不统一 提交于 2020-01-06 14:48:39
memset一般用作初始化函数。作用是将某一块内存中的内容全部设置为某一值。 格式:起始指针、设定的初始值、长度 memset ( buffer , 0 , sizeof ( int ) * 10 ) ; //在buffer处初始化10个0 memcpy用于从源内存地址的起始位置开始拷贝若干个字节到目标内存地址。 格式:返回指针,源指针、长度 memcpy ( dst , src , strlen ( src ) + 1 ) ; strlen是计算字符串长度的函数,从内存任意位置开始到’\0’为止的字符数量,一般比sizeof得到的少1。 格式:字符串指针 strlen ( str ) ; strcpy是字符串拷贝函数,把含有’\0’结束符的字符串复制到另一个地址空间。 格式:返回指针,源指针 strcpy ( dst , src ) ; 来源: CSDN 作者: 海洋之心。 链接: https://blog.csdn.net/weixin_42979679/article/details/103846996

Copying bytes without memcpy

人走茶凉 提交于 2020-01-04 14:58:10
问题 I have several variables of different types stored in a char array. Normally I would write them to the array this way: int a = 5; memcpy(offset, (char*)&a, sizeof(int)) However, memcpy doesn't work in OpenCL kernels. What would be the easiest way to do the same without this function? 回答1: You can easily enough provide mymemcpy void mymemcpy(unsigned char *dest, const unsigned char *src, size_t N) { size_t i; for(i=0;i<N;i++) dest[i] = src[i]; } However it's not very efficient because most

Can I implicitly create a trivially copiable type

Deadly 提交于 2020-01-04 14:13:41
问题 My question is this: Suppose type T is trivially copyable....can "create" an instance of this type without calling a constructor....like so: #include <type_traits> #include <cstring> using T = int; // T can be any trivially copyable type T create(const T& other) { std::aligned_storage_t<sizeof(T),alignof(T)> my_T; std::memcpy(&my_T,&other,sizeof(T)); return *reinterpret_cast<T*>(&my_T); } Is this defined behavior, or can I only copy into an existing object of type T? 回答1: The rule, from

Does memcpy preserve data between different types?

ⅰ亾dé卋堺 提交于 2020-01-04 01:55:31
问题 Does calling memcpy on two different structures preserve the original data if the buffer size is sufficient? And is it defined to retrieve values of another data type with data of previous data type if their respective data types overlap? This should be similar for both c/cpp languages but I'm providing an example in cpp - #include <iostream> #include <cstring> using namespace std; struct A{ int a; char b[10]; }; struct B{ int ba; int bb; }; int main(){ B tmp; tmp.ba = 50; tmp.bb = 24; cout <

Understanding memcpy

好久不见. 提交于 2020-01-03 11:29:09
问题 int a = 10; int* pA = &a; long long b = 200; long long* pB = &b; memcpy (pB,pA,4); memcpy (pB+1,pA,4); cout<<"I'm a memcpy!: "<<*(pB)<<endl; I'm doing some tests with memcpy to teach myself how memory works. What I am trying to do is make b = to "1010". I can copy the value from a to b, but then I try to offset the memory by 1 byte and write another 10 but it doesn't work it only outputs "10". What would I need to do to get a value of 1010? 回答1: A few problems with your code as it stands: You

'memcpy'-like function that supports offsets by individual bits?

独自空忆成欢 提交于 2020-01-02 05:28:13
问题 I was thinking about solving this, but it's looking to be quite a task. If I take this one by myself, I'll likely write it several different ways and pick the best, so I thought I'd ask this question to see if there's a good library that solves this already or if anyone has thoughts/advice. void OffsetMemCpy(u8* pDest, u8* pSrc, u8 srcBitOffset, size size) { // Or something along these lines. srcBitOffset is 0-7, so the pSrc buffer // needs to be up to one byte longer than it would need to be

memcpy a buffer and an array not working

£可爱£侵袭症+ 提交于 2020-01-02 04:47:10
问题 I have a requirement in which i need to pass an empty array as a parameter to a function. And in this called function, i should be memcpy some data into the passed array. So i have written a small example which is same as my requirement. Below is its code: #include <stdio.h> #include <stdlib.h> void printArr(int *a) { int i; int *b=(int*)malloc(sizeof(int)*10); printf("\n\nEnter 10 lements:\n"); for(i=0;i<10;i++) scanf("%d",&b[i]); printf("\nContents of array b:\n"); for(i=0;i<10;i++) printf(

Difference between memcpy and copy by assignment

旧时模样 提交于 2020-01-01 06:19:06
问题 Struct A { uint16_t len; uint8_t cnt; uint8_t unit; uint32_t seq; }; This struct A is serialized into a char * buf. If I want to deserialize the individual values eg: uint16_t len = 0; memcpy(&len, buf, sizeof(len)); or I can just do uint16_t len = (uint16_t) buf; Which one is better or are both the same? Also to deserialize the whole struct, if I just do A tmp; memcpy(&tmp, buf, sizeof(A)); Would this work fine or should I be worried about padding etc from the compiler? 回答1: When the data is