struct

How to create an array of structs containing large arrays?

戏子无情 提交于 2019-12-24 04:57:30
问题 How could I create an array of structs containing big arrays with a fixed size? I want to use arrays and not vectors. This code is an example but doesn't compile struct _Tmove { data1: usize, data2: u64, data3: bool, } struct _TmoveP { data4: Box<[_Tmove]>, data5: isize, } fn main() { let mut gen_list = Box::new([ _TmoveP { data5: 1, data4: Box::new([_Tmove { data1: 5, data2: 1, data3: true }; 300]), } ; 100000]); assert!(gen_list[0].data4[0].data1==5); } error[E0277]: the trait bound `_Tmove

Readng an array of structures from file

倖福魔咒の 提交于 2019-12-24 04:47:07
问题 I have the next task: I need to read an array of structures from file. There is no problem to read one structure: structFmt = "=64s 2L 3d" # char[ 64 ] long[ 2 ] double [ 3 ] structLen = struct.calcsize( structFmt ) f = open( "path/to/file", "rb" ) structBytes = f.read( structLen ) s = struct.unpack( structFmt, structBytes ) Also there is no problem to read an array of "simple" types: f = open( "path/to/file", "rb" ) a = array.array( 'i' ) a.fromfile( f, 1024 ) But there is a problem (for me,

Struct pointer casts

天涯浪子 提交于 2019-12-24 04:32:12
问题 I'm trying to implement a linked list like this: typedef struct SLnode { void* item; void* next; } SLnode; typedef struct DLnode { void* item; void* next; struct DLnode* prev; } DLnode; typedef struct LinkedList { void* head; /*SLnode if doubly_linked is false, otherwise DLnode*/ void* tail; /* here too */ bool doubly_linked; } LinkedList; And I want to access it like this: void* llnode_at(const LinkedList* ll, size_t index) { size_t i; SLnode* current; current = ll->head; for(i = 0; i <

Struct pointer casts

风格不统一 提交于 2019-12-24 04:32:01
问题 I'm trying to implement a linked list like this: typedef struct SLnode { void* item; void* next; } SLnode; typedef struct DLnode { void* item; void* next; struct DLnode* prev; } DLnode; typedef struct LinkedList { void* head; /*SLnode if doubly_linked is false, otherwise DLnode*/ void* tail; /* here too */ bool doubly_linked; } LinkedList; And I want to access it like this: void* llnode_at(const LinkedList* ll, size_t index) { size_t i; SLnode* current; current = ll->head; for(i = 0; i <

How do bit fields interplay with bits padding in C

北城余情 提交于 2019-12-24 04:09:39
问题 I have two questions concerning bit fields when there are padding bits. Say I have a struct defined as struct T { unsigned int x: 1; unsigned int y: 1; }; Struct T only has two bits actually used. Question 1: are these two bits always the least significant bits of the underlying unsigned int? Or it is platform dependent? Question 2: Are those unused 30 bits always initialized to 0? What does the C standard say about it? 回答1: Question 1: are these two bits always the least significant bits of

Struct padding for single member structs

懵懂的女人 提交于 2019-12-24 03:53:27
问题 I was looking to make a struct which was an arbitrary size, known at compile time. (for use in a macro). eg: /* assume sizeof(SomeStruct) could be an odd number, * if it is using GCC's 'packed' attribute for eg */ struct { unsigned char data[sizeof(SomeStruct)]; } a, *tmp; tmp = (void *)some_data a = *tmp; However I was concerned that struct padding may increase the size of the struct so it is larger than the member, I was assured that the size of a single member struct will always be the

How to create an array of dictionaries?

那年仲夏 提交于 2019-12-24 03:14:36
问题 new to programming! I'm trying to create an array of dictionaries inside a struct in Swift like so: var dictionaryA = [ "a": "1", "b": "2", "c": "3", ] var dictionaryB = [ "a": "4", "b": "5", "c": "6", ] var myArray = [[ : ]] myArray.append(dictionaryA) myArray.append(dictionaryB) This works fine in a playground, but when I put it into an Xcode project, inside a struct, the lines with the append function produce the error "Expected declaration". I've also tried using the += operator with the

How do arrays work inside a struct?

非 Y 不嫁゛ 提交于 2019-12-24 03:01:13
问题 If I have for example typedef struct node { int numbers[5]; } node; Whenever I create an instance of such a struct there's gonna be allocation of memory in the stack for the array itself, (in our case 20 bytes for 5 ints(considering ints as 32 bits)), and numbers is gonna be a pointer to the first byte of that buffer. So, I thought that since inside an instance of node, there's gonna be a 20 bytes buffer(for the 5 int s) and a 4 bytes pointer( numbers ), sizeof(node) should be 24 bytes. But

C++ static constexpr member redeclaration outside of class

那年仲夏 提交于 2019-12-24 02:58:08
问题 For the following code, why does the first case in main work fine without the redeclaration of Foo::bar, whereas the second case with the function requires it? struct Foo{ static constexpr int bar = 30; }; //Declaration of Foo::bar outside of struct constexpr int Foo::bar; int returnconstexpr(const int& x) { return x; } int main() { //Ok without declaration outside of struct std::cout << Foo::bar << std::endl; //Requires declaration outside of struct std::cout << returnconstexpr(Foo::bar) <<

allocating memory for a struct when it includes a string

大憨熊 提交于 2019-12-24 02:25:13
问题 Suppose if i have a struct like this struct node{ // note that i have changed the struct code according to my convenience char* lastname; char* employeeID; struct node* nextRecord; } ; typedef struct node myRecord; Now I am allocating memory for the sirst node as below headptr=malloc(sizeof(myRecord)); The struct inludes two strings. when I store something in headptr->lastname where does it get stored? shoul I allocate memory for those two strings explicitly? 回答1: when I store something in