strcpy()/strncpy() crashes on structure member with extra space when optimization is turned on on Unix?

What you are doing is undefined behavior. The compiler is allowed to assume that you will never use more than sizeof int64_t for the variable member int64_t c. So if you try to write more than sizeof int64_t(aka sizeof c) on c, you will have an out-of-bounds problem in your code. This is the case … Read more

Why is Go json.Marshal rejecting these struct tags? What is proper syntax for json tags? [duplicate]

Oh my goodness! I just figured it out. There is no space allowed between json: and the field name “name”. The “go vet” error message (“bad syntax”) is remarkably unhelpful. The following code works. Can you see the difference? package main import ( “encoding/json” “fmt” ) type Person struct { Name string `json:”name”` Age int … Read more

Is there a data type in Python similar to structs in C++?

Why not? Classes are fine for that. If you want to save some memory, you might also want to use __slots__ so the objects don’t have a __dict__. See http://docs.python.org/reference/datamodel.html#slots for details and Usage of __slots__? for some useful information. For example, a class holding only two values (a and b) could looks like this: … Read more

Is it possible to dynamically define a struct in C

It isn’t possible to dynamically define a struct that is identical to a compile-time struct. It is possible, but difficult, to create dynamic structures that can contain the information equivalent to a struct. The access to the data is less convenient than what is available at compile-time. All else apart, you cannot access a member … Read more

JSON field set to null vs field not there

Use json.RawMessage to “delay” the unmarshaling process to determine the raw byte before deciding to do something: var data = []byte(`{ “somefield1″:”somevalue1”, “somefield2”: null }`) type Data struct { SomeField1 string SomeField2 json.RawMessage } func main() { d := &Data{} _ = json.Unmarshal(data, &d) fmt.Println(d.SomeField1) if len(d.SomeField2) > 0 { if string(d.SomeField2) == “null” { … Read more