unions

How to fix System.TypeLoadException when declaring structs with union?

浪尽此生 提交于 2021-02-11 14:17:16
问题 Receiving System.TypeLoadException in runtime. I use code from third-party source to define structs and p-invoke external methods. I found the full code online in this link. In the code there are some structs with union, using FieldOffsetAttribute(0) on multiple fields. when I try to use some of the structs I get an exception: Could not load type 'NET_DVR_SLAVE_CHANNEL_UNION' from assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object

How union is helpful over a variable? [duplicate]

梦想与她 提交于 2021-02-11 06:13:24
问题 This question already has answers here : Purpose of Unions in C and C++ (16 answers) Closed 7 months ago . I have looked into this post and explored the use cases of union. Every source is saying that it is memory efficient over structures, I understand this. But confusion arises when you say: "Variables of Union share same memory, when you change the value of one variable, then other's value gets changed, and only one variable is accessible at a time" So why is there a need to declare union

How union is helpful over a variable? [duplicate]

淺唱寂寞╮ 提交于 2021-02-11 06:13:05
问题 This question already has answers here : Purpose of Unions in C and C++ (16 answers) Closed 7 months ago . I have looked into this post and explored the use cases of union. Every source is saying that it is memory efficient over structures, I understand this. But confusion arises when you say: "Variables of Union share same memory, when you change the value of one variable, then other's value gets changed, and only one variable is accessible at a time" So why is there a need to declare union

C++ Union usage

橙三吉。 提交于 2021-02-08 13:16:19
问题 So far I have just used unions to store either member A or member B. I do now find myself in the case where I want to change the used member during runtime . union NextGen { std::shared_ptr<TreeRecord> Child = nullptr; std::vector<std::shared_ptr<TreeRecord>> Children; }; My current usage: void TreeRecord::AddChild(const std::shared_ptr<TreeRecord>& NewChild) { if(_childCount == 0) { _nextGeneration.Child = NewChild; _childCount++; } else if(_childCount == 1) { //This is not clear to me: //Do

Resolve union structure in Rust FFI

谁说我不能喝 提交于 2021-02-07 14:23:20
问题 I have problem with resolving c-union structure XEvent. I'm experimenting with Xlib and X Record Extension in Rust. I'm generate ffi-bindings with rust-bindgen. All code hosted on github alxkolm/rust-xlib-record. Trouble happen on line src/main.rs:106 when I try extract data from XEvent structure. let key_event: *mut xlib::XKeyEvent = event.xkey(); println!("KeyPress {}", (*key_event).keycode); // this always print 128 on any key My program listen key events and print out keycode . But it is

Does C++11 allow non-anonymous unions to contain static data members?

試著忘記壹切 提交于 2021-02-07 13:09:02
问题 In C++11 I declare the following union: union U4 { char c; int i; static int si; }; When I compile this code with g++ 4.7.0 using -std=c++11 -pedantic-errors, I get the following errors (with minor editing): error: local class ‘union U4’ shall not have static data member ‘int U4::si’ [-fpermissive] error: ‘U4::si’ may not be static because it is a member of a union The FDIS (N3242) does not explicitly allow static data members of named unions, as far as I can see. But I also don't see where

How to know which variable value is set for union?

半城伤御伤魂 提交于 2021-02-07 06:24:27
问题 I am working on optimization of a project. It contains a struct of an options in which user can select a single option at a time. In addition to the option, we also use a flag variable to check which option value is set for this record. In order to make it memory efficient I want to convert struct into a union. But How do I know that which variable value is set in union. Because there is no restriction in union to fetch a value of a variable even which is not set. struct options{ int basicPay

Allowing multiple differently shaped interfaces as TypeScript return types

守給你的承諾、 提交于 2021-02-05 06:43:25
问题 I have a function that takes a few parameters and generates objects that will be passed into an external process. Sine I have no control over the shapes that need to be ultimately created, I have to be able to take some varying parameters to my function and assemble them into the appropriate objects. Here's a really basic example that exhibits the issue I'm having: interface T1A { type: 'type1'; index: number; } interface T1B { type: 'type1'; name: string; } interface T2A { type: 'type2';

unrestricted union members lifetime during parent object construction

纵饮孤独 提交于 2021-01-28 11:00:39
问题 Normally you are responsible for lifetime of your unrestricted union members -- and typically you do it via in-place ctor/dtor calls. But, apparently, there is at least one case when compiler helps you -- in the code below, if object construction fails it's (previously constructed) union member gets automatically destroyed (at least in MSVC 2015), i.e. we never leak. #include <string> struct CanThrow { CanThrow() { throw 0; } }; struct A { A() : str{} {} // note that we don't explicitly call

Is there a safer way to use unions to convert between integer and floating-point numbers?

对着背影说爱祢 提交于 2021-01-28 03:43:33
问题 I´m writing a VM in Rust and I have a C and C++ background. I need union-like functionality because on the VM stack I can either store an int or a float . In C I had a union: union stack_record_t { int i; float f; }; I can use the record as int or as float with zero runtime overhead. I have a static bytecode analyzer which will find type errors before the bytecode executes, so I don't have to store a flag alongside the record. I don´t know if it is a good idea to use unions in Rust because