struct

Struct alignment and type reinterpretation

不羁的心 提交于 2019-12-23 11:59:43
问题 Lets say I have two types A and B. Then I make this type struct Pair{ A a; B b; }; Now I have a function such as this. void function(Pair& pair); And lets assume that function will only ever use the a part of the pair. Then is it undefined behavior to use and call the function in this way? A a; function(reinterpret_cast<Pair&>(a)); I know that a compiler may insert padding bytes after a member but can it also do it before the first member? 回答1: I think it's defined behavior, assuming Pair is

Default value for struct parameter

橙三吉。 提交于 2019-12-23 11:58:03
问题 Let's say I have the following struct: struct myStruct { int x; int y; int z; int w; }; I want to initialize this struct to a default value when calling the following function. If it helps I'm looking for a simple zero initialization. void myFunc(myStruct param={0,0,0,0}) { ... } This code however gives me compile error. I've tried VS2003 and VS2008. NOTE: I have looked at other answers mentioning the use of constructor. However I want the user to see what values I'm using for initialization.

How CLR can bypass throwing error when assigning a null value to the struct?

别等时光非礼了梦想. 提交于 2019-12-23 11:44:08
问题 I am trying to understand one thing in this code: Nullable<Int32> x = 5; Nullable<Int32> y = null; Console.WriteLine("x: HasValue={0}, Value={1}", x.HasValue, x.Value); Console.WriteLine("y: HasValue={0}, Value={1}", y.HasValue, y.GetValueOrDefault()); And Output is: x: HasValue=True, Value=5 y: HasValue=False, Value=0 And the thing that I don't understand when you pass null to y , I believe it calls public static implicit operator Nullable<T>(T value) but the definition of this method

Why can I not assign interchangeably with two structs that have identical contents?

余生颓废 提交于 2019-12-23 11:34:31
问题 I'm trying to learn C and I've come across something weird: struct { int i; double j; } x, y; struct { int i; double j; } z; Here, you can see I created two struct s that are identical in their elements. Why is it that when I try to assign x = z it will generate a compile error but x = y does not? They have the same contents, so why can't I assign them back and forth with each other, regardless? Is there any way I can make this so I can assign x = z ? Or do they simply have to be the same

How to make default value with a struct property?

隐身守侯 提交于 2019-12-23 10:58:10
问题 I would like to know how to apply [DefaultValue] attribute to a struct property. You can notice that Microsoft does it with Form's Size and many other properties. Their values' types are Size, Point, etc. I would want to do the same thing with my custom struct. 回答1: [DefaultValue(typeof(Point), "0, 0")] Would be an example. Using a string to initialize the value is a necessary evil, the kind of types you can use in an attribute constructor are very limited. Only the simple value types, string

How to make default value with a struct property?

末鹿安然 提交于 2019-12-23 10:58:08
问题 I would like to know how to apply [DefaultValue] attribute to a struct property. You can notice that Microsoft does it with Form's Size and many other properties. Their values' types are Size, Point, etc. I would want to do the same thing with my custom struct. 回答1: [DefaultValue(typeof(Point), "0, 0")] Would be an example. Using a string to initialize the value is a necessary evil, the kind of types you can use in an attribute constructor are very limited. Only the simple value types, string

Dynamically sized structs - Learn C The Hard Way Ex17

自闭症网瘾萝莉.ら 提交于 2019-12-23 10:57:06
问题 I'm having trouble with an exercise in Learn C The Hard Way. The exercise provides a simple database program which has a fixed size and number of rows. Below you can see the structs that form the database. #define MAX_DATA 512 #define MAX_ROWS 100 struct Address { int id; int set; char name[MAX_DATA]; char email[MAX_DATA]; }; struct Database { struct Address rows[MAX_ROWS]; }; struct Connection { FILE *file; struct Database *db; }; the task is to change code to accept parameters for MAX_DATA

sizeof operator gives extra size of a struct in C# [duplicate]

三世轮回 提交于 2019-12-23 10:43:11
问题 This question already has answers here : Structure padding and packing (8 answers) Closed 3 years ago . I am trying to check size of all of my variables (value types) using sizeof operator. I gone through one of the msdn article where it is written that For all other types, including structs, the sizeof operator can be used only in unsafe code blocks and also structs should not contain any fields or properties that are reference types For this, I enabled unsafe compilation in my project

Semantic versioning: minor or major change?

社会主义新天地 提交于 2019-12-23 10:06:50
问题 In semantic versioning the general rule is to increase the minor number only when backwards compatible functionalities are introduced, otherwise the major number must be increased instead. The same approach, but with a different arithmetic, is used by libtool . I have a question concerning what is considered a backwards compatible change and what not. Imagine I have written a library, and the public header of this library contains a typedef of a data type named foo . In version 1.0.0 this

C++ using struct arguments for functions instead of multiple arguments?

和自甴很熟 提交于 2019-12-23 09:59:08
问题 Anybody think there are advantages to using a class or struct to pass arguments ? Like instead of f(int,float,string) Have f(Args) Where Args is struct with int , float , string members. Advantage is easy to create multiple default parameters and not have to change function signature when new arguments added. 回答1: The obvious benefit would be to have logical grouping of semantically related data items. Once you do, add some (member) operations on the structure that will guarantee your