memcpy

c - Segmentation fault when trying to write a modified structure

↘锁芯ラ 提交于 2019-12-25 16:43:47
问题 This is in a program meant to work with ppm image files. I'm getting a compilation error when trying to modify a struct and then writing it to a new file. This is the global struct (declared in ppmIO.c and ppmIO.h): ppmIO.c: struct Image *instance; ppmIO.h: struct Image { int width; int height; unsigned char *data; }; extern struct Image *instance; This is my imageManip.h file: #include <ppmIO.h> void ImageInvert(struct Image **toInvert); void ImageSwap(struct Image **toSwap); These are the

How to use memcpy correctly with different types of arrays

删除回忆录丶 提交于 2019-12-25 08:19:26
问题 I have a templated class named Stack that has a private method resize . Whenever I use the int or double template version of this class, it works as desired. But, whenever I use the float or string versions of the template, it crashes. I believe that it's a problem with my memcpy function call. How am I supposed to use it correctly? template <class Type> void Stack<Type>::resize(int capacity) { if(capacity >= MAX_SIZE) capacity = MAX_SIZE; Type* copy = new Type[capacity]; for (int i = 0; i <

Plain Old Data and `std::memcpy` alignment issues

依然范特西╮ 提交于 2019-12-25 07:17:34
问题 Trying to respond to another question, I've proposed a solution that use std::memcpy() to store generic types in a buffer of char s. My doubt is about possible memory alignment issues storing POD (I know that with not-POD type, as std::string , is very very dangerous). In short: there are memory alignment issues with the following program? And if they are, it's possible to write something similar (that store POD values in a char buffer) that is safe? And how? #include <cstring> #include

Can you deserialize bytes with memcpy?

混江龙づ霸主 提交于 2019-12-24 09:09:25
问题 If I have a class that has primitives in it, a bunch of ints or chars for example, is it possible to do deserialize and serialize it with memcpy? MyClass toSerialize; unsigned char byteDump[sizeof(toSerialize)]; memcpy(&byteDump, &toSerialize, sizeof(toSerialize)); WriteToFile(byteDump); Then on another program or computer, do this: MyClass toDeserialize; unsigned char byteDump[sizeof(toSerialize)]; LoadFile(byteDump); memcpy(&toDeserialize, &byteDump, sizeof(byteDump)); I have cases where

memcpy of a part of a struct

人走茶凉 提交于 2019-12-24 00:46:44
问题 I have a struct/class which is partiall Plain Old Data (POD). struct S { // plain-old-data structs with only arrays and members of basic types (no pointers); Pod1 pod1; Pod2 pod2; Pod3 pod3; Pod4 pod4; vector<int> more; }; I copy objects of class S a lot. I would like to copy it with memcpy, but S::more prevents it. I would like to avoid call to 4 memcpy's and make it all with one for an extra bit of performance. Should I do someting like this? memcpy(s1, s2, sizeof(Pod1) + sizeof(Pod2) +

Cannot properly memcpy a char array to struct

…衆ロ難τιáo~ 提交于 2019-12-23 20:31:11
问题 So I have a construct called packet struct Packet { unsigned int packet_type; wchar_t packet_length[128]; wchar_t file_name[256]; wchar_t template_name[256]; wchar_t file_name_list[1024]; wchar_t file_data[1024]; void serialize(char * dat) { memcpy(dat, this, sizeof(Packet)); } void deserialize(const char * dat) { memcpy(this, dat, sizeof(Packet)); } }; I'm trying to desieralize from this data {byte[2692]} [0] 0 unsigned int packet_type; (4 bytes) [1] 0 [2] 0 [3] 0 [4] 50 '2' wchar_t packet

Does memcpy not throw exceptions?

半腔热情 提交于 2019-12-23 19:54:36
问题 Hopefully there is a simple answer to this as it seems a simple question, however I have not been able to find any information on this on the interwebs. In the following code snippet, Visual Studio complains of unreachable code at the line "delete bytes;" try { memcpy(bytes, other.bytes, count); } catch (...) { delete[] bytes; throw; } Does memcpy not throw exceptions? 回答1: No. memcpy is a C function. It doesn't know about C++ features such as exceptions. (Of course, it's perfectly legal to

memcpy leads to EXC_BAD_ACCESS

柔情痞子 提交于 2019-12-23 19:15:08
问题 I'm converting someone else's C++ to (Objective) C, but I'm having a problem with memcpy . I use it as follows: memcpy((void *)virtualFlash[virtualFlashAddress], data, dataLength); The variables are defined as follows: unsigned char virtualFlash[5 * 1024 * 1024]; // 5MB NSUInteger virtualFlashAddress; // set to 8 later on unsigned char *data = (unsigned char *)[recordData bytes]; // recordData is an NSData object NSUInteger dataLength = [recordData length]; // same NSData object I get an EXC

How to deduce contiguous memory from iterator

偶尔善良 提交于 2019-12-23 16:30:22
问题 Somehow, the native stl::copy() algorithm on VC++ (Dinkumware) figures out that it can use memcpy() on data that is trivially copy-able. Is it possible for a mere mortal to do that? - assuming each element is_trivially_copyable. Does random_access_iterator imply contiguous memory? The standard is not clear to me. So, if all you have in a template is an iterator or two, is it possible to deduce at compile-time that the underlying array can be copied with memcpy() , and if so how? EDIT - Here's

memcpy adds ff ff ff to the beginning of a byte

孤人 提交于 2019-12-23 09:20:04
问题 I have an array that is like this: unsigned char array[] = {'\xc0', '\x3f', '\x0e', '\x54', '\xe5', '\x20'}; unsigned char array2[6]; When I use memcpy: memcpy(array2, array, 6); And print both of them: printf("%x %x %x %x %x %x", array[0], // ... etc printf("%x %x %x %x %x %x", array2[0], // ... etc one prints like: c0 3f e 54 e5 20 but the other one prints ffffffc0 3f e 54 ffffffe5 20 what happened? 回答1: I've turned your code into a complete compilable example. I also added a third array of