struct

malloc an array of struct pointers

烂漫一生 提交于 2019-12-29 03:21:09
问题 I have the following struct: typedef struct _chess { int **array; int size; struct _chess *parent; } chess; and I have: typedef struct _chess *Chess; Now, I want to create an array of dynamic length to store pointers to the chess struct so I do the following: Chess array [] = malloc(size * sizeof(Chess)); This gives me an error: invalid initializer. And if I drop the [] and do this: Chess array = malloc(size * sizeof(Chess)); it compiles without error but when I try to set an element of this

When are structs the answer?

血红的双手。 提交于 2019-12-29 02:44:31
问题 I'm doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you create millions of them, they don't live longer than a single method, they're lightweight. However, by simply changing 'struct' to 'class' on Vector and Ray, I got a very significant performance gain. What gives? They're both small (3 floats for Vector, 2 Vectors for a Ray), don't get copied around excessively. I do pass

How to remove compiler error with struct: “Use of unassigned local variable”

只谈情不闲聊 提交于 2019-12-29 02:00:08
问题 The C# compiler is a bit ... old fashioned ... and won't do static analysis. So it breaks on seemingly correct code like this: MyStruct s; bool inited = false; foreach( Something foo in ACollection ) { if( foo.Test() ) continue; if( inited ) s.DoSomething(); else { s = foo.GetMeAnS(); inited = true; } } Note: the unusual problem is that "s" is a struct. If it were a class, I'd simply init it to null. This struct has no meaningful "uninited" state, and I don't want to pay the performance cost

Difference between golang pointers

天涯浪子 提交于 2019-12-29 01:56:05
问题 There are 2 kinds of variables that I have. Check for the Go playground, and I don't understand why this is happening. The problem: what I get from the Models it should be a struct to use it for GORM First() function. The code: package main import ( "fmt" ) type Test struct { Test string } var Models = map[string]interface{}{ "test": newTest(), } func main() { test1 := Test{} fmt.Println("Test 1: ") fmt.Printf("%v", test1) fmt.Println() fmt.Println("Test 1 as pointer: ") fmt.Printf("%v",

Swift struct initialization, making another struct like String

送分小仙女□ 提交于 2019-12-29 01:42:13
问题 on Swift String is a struct and you could just initialize it using var someString:String = "Hello" how would I make another Struct initializable like String? for example struct StringV2 { init() } class SomeClass { let someStringV2:StringV2 = "Hello" } Since that's how String 's code looks like. 回答1: This is (in my opinion) neat part of the language. Yes, this is possible, thanks to the ExpressibleByStringLiteral protocol. Unfortunately, there is some complexity to it.

How do I declare an instance of one of my Rust structs as static? [duplicate]

馋奶兔 提交于 2019-12-28 19:31:10
问题 This question already has answers here : How can you make a safe static singleton in Rust? (2 answers) How do I create a global, mutable singleton? (1 answer) Closed last year . How do I declare an instance of one of my own structs as static? This sample doesn't compile: static SERVER: Server<'static> = Server::new(); fn main() { SERVER.start("127.0.0.1", 23); } 回答1: You can’t call any non- const functions inside a global. Often you will be able to do something like struct literals, though

update struct via another struct in Matlab [duplicate]

本小妞迷上赌 提交于 2019-12-28 15:36:32
问题 This question already has answers here : What are some efficient ways to combine two structures in MATLAB? (5 answers) Closed 5 years ago . I'm wondering if there is a convenient way to update a struct with the values of another struct in Matlab. Here is the code, with the use of fieldnames , numel and a for loop, fn = fieldnames(new_values); for fi=1:numel(fn) old_struct.(fn{fi}) = new_values.(fn{fi}); end Of course, I don't want to loose the fields in old_struct that are not in new_values ,

update struct via another struct in Matlab [duplicate]

若如初见. 提交于 2019-12-28 15:35:10
问题 This question already has answers here : What are some efficient ways to combine two structures in MATLAB? (5 answers) Closed 5 years ago . I'm wondering if there is a convenient way to update a struct with the values of another struct in Matlab. Here is the code, with the use of fieldnames , numel and a for loop, fn = fieldnames(new_values); for fi=1:numel(fn) old_struct.(fn{fi}) = new_values.(fn{fi}); end Of course, I don't want to loose the fields in old_struct that are not in new_values ,

Using Struct Stat()

北慕城南 提交于 2019-12-28 15:22:13
问题 I'm trying to figure out how exactly to use stat() to capture information about a file. What I need is to be able to print several fields of information about a file. So.. #include <iostream> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> using namespace std; int main() { struct stat buf; stat("file",&buf); ... cout << st_dev << endl; cout << st_ino << endl; cout << st_mode << endl; cout << st_nlink << endl; cout << st_uid << endl; cout << st_gid << endl; cout << st_rdev <<

How does function ACTUALLY return struct variable in C?

流过昼夜 提交于 2019-12-28 08:09:11
问题 How does a function return value is clear to me, just to kick start: int f() { int a = 2; return a; } Now a gets the memory in stack and its life-span is within the f() in order to return the value it copies the value to a special register which is read by the caller as it knows that the callee have placed the value for him. (Since the size of return-value-holder special register size is limited that's why we cant return large objects therefore In case of advance languages when we want to