struct

malloc and free with a dynamically changing structure

a 夏天 提交于 2020-01-07 02:25:28
问题 I am having trouble with moving my pointer in a dynamically changing structure. I have created my code where you can malloc more memory and this seems to be working. The problems that I am running into is how to add to the structure, how to free memory and how to move from structure to structure and print all items. I am trying to test add and print (the delete function that is there does not seem to work, segfaults) When I add to the struct and then print the struct I get a segfault from the

Convert ctypes code to cython

风格不统一 提交于 2020-01-07 02:25:26
问题 I'd like to convert some ctypes code to use cython instead, but I'm struggling. Essentially, the ctypes code: copies the contents (floats) of two lists into C-compatible structs sends the structs to my binary via FFI receives the structs back (the length is never modified) copies the contents to two new lists sends the structs back across the FFI boundary so their memory can be freed My ctypes code looks like this so far: rlib.h #ifndef RLIB_H #define RLIB_H typedef struct _FFIArray { void*

Using pointers or references to struct

霸气de小男生 提交于 2020-01-07 02:25:13
问题 Me and my friend is using structs in our code (our code is separate from each other). Lets take the following example: struct Book { char title; int pages; } void setBook(struct Book *tempBook) { tempBook->title = "NiceTitle"; tempBook->pages = 50; } The above code is pretty straight forward. The thing is, is there any difference in having these two mains: int main() { struct book obj1; setBook(&obj); } Or int main() { struct Book *obj2; setBook(obj2); } EDIT: I was not clear in my statement.

Missing data while copying buffer to struct

时间秒杀一切 提交于 2020-01-06 20:16:52
问题 I have a TCP server socket which receives a 16-byte request message. The request message will have several field and based on the field values i need to proceed with different actions. When I tried to copy the buffer to struct, I could see missing data. I tried all possible methods but couldn't able to figure out whether I need to do structure padding or not. My structure looks like, struct stRequestMsg { uint16_t startTag; uint32_t messageSize; uint16_t messageID; uint16_t sequenceNumber;

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

Writing complex structure to memory-mapped-file

有些话、适合烂在心里 提交于 2020-01-06 20:00:05
问题 I try to write following struct to a memory mapped file, but I still have problem with the array (writing throws exception that the struct can not contain reference) [StructLayout(LayoutKind.Explicit)] struct IndexEntry { [FieldOffset(0)] public byte key; [FieldOffset(4)] public int lastValueIdx; [FieldOffset(8)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PART_ENTRY_SIZE)] public long[] values; } I use this calling for writing: UnmanagedMemoryAccessor.Write<IndexEntry>(0, ref

Why can't I dynamically allocate memory of this string of a struct?

佐手、 提交于 2020-01-06 19:49:13
问题 Let's say for example, I have a struct: typedef struct person { int id; char *name; } Person; Why can't I do the following: void function(const char *new_name) { Person *human; human->name = malloc(strlen(new_name) + 1); } 回答1: You need to allocate space for human first: Person *human = malloc(sizeof *human); human->name = malloc(strlen(new_name) + 1); strcpy(human->name, new_name); 回答2: You have to allocate memory for the structure Person . The pointer should point to the memory allocated

C# calling C++ 3rd party DLL (no source) raises exception - not PInvoke Compatible

岁酱吖の 提交于 2020-01-06 16:22:26
问题 I am using VS 2015 Community with an ASP.NET MVC Web Application that uses a 3rd party C++ DLL I do not have source code for. Documentation is very scarce as is any helpful communication with the authors of the 3rd party DLL. I've asked a related SO Question and received a good answer from @Steven. I've modified my code according to his answer and am trying to make a successful call to the 3rd party C++ DLL. The code: // Call DLL MyDLLInput _DLLInput = new MyDLLInput(); { SomeList = new int

fread into struct reads data incorrectly

随声附和 提交于 2020-01-06 15:52:50
问题 I am trying to read a bitmap (.bmp) image header into a struct in c. typedef unsigned short WORD; typedef unsigned long DWORD; typedef struct _BITMAPFILEHEADER { WORD Type; DWORD Size; WORD Reserved1; WORD Reserved2; DWORD OffBits; } BITMAPFILEHEADER; My code to read the bitmap file FILE *fp; BITMAPFILEHEADER header; fp = fopen(file,"rb"); if (fp == NULL) { printf("cannot open file!\n"); return 1; } fread(&header, sizeof(BITMAPFILEHEADER), 1, fp); printf("Type: %02x\n", header.Type); printf(