struct

Why are public fields faster than properties?

I was poking around in XNA and saw that the Vector3 class in it was using public fields instead of properties. I tried a quick benchmark and found that, for a struct the difference is quite dramatic (adding two Vectors together a 100 million times took 2.0s with properties and 1.4s with fields). For a reference type, the difference doesn...

How much functionality is "acceptable" for a C++ struct?

My first post so please go easy on me! I know that there's no real difference between structs and classes in C++, but a lot of people including me use a struct or class to show intent - structs for grouping "plain old data" and classes for encapsulated data that has meaningful operations. Now, that's fine but at what point do you start...

C# P/Invoke structure problem

I am trying to write a C# P/Invoke wrapper for a C API (a native Win dll), and generally this is working fine. The only exception is a specific method which takes a struct as a parameter in the C code. The function is invoked without any exceptions, but it returns false indicating that something failed in the execution. In the API heade...

Is it a best practice to wrap arrays and their length variable in a struct in C?

Hi, I will begin to use C for an Operating Systems course soon and I'm reading up on best practices on using C so that headaches are reduced later on. This has always been one my first questions regarding arrays as they are easy to screw up. Is it a common practice out there to bundle an array and its associated variable containing...

Use reflection to set the value of a field in a struct which is part of an array of structs

At the moment my code successfully sets the value of fields/properties/arrays of an object using reflection given a path to the field/property from the root object. e.g. //MyObject.MySubProperty.MyProperty SetValue('MySubProperty/MyProperty', 'new value', MyObject); The above example would set 'MyProperty' property of the 'MyObject' ...

Calling C++ dll function from C#: Of structs, strings and wchar_t arrays.

Here's a simple problem I need to solve, but it makes me feel my hair turning gray as all my attempts are returning me the same error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I have a sample app written in C++ which makes a call to the dll. Here is the relevant code: ...

Expression Is a value and therefore cannot be the target of an assignment

I ran into an issue using a struct today that caught me off guard, and I was hoping someone could shed some light on it for me. I have a struct defined like this: public struct PaymentDetail { public Decimal Amount{get;set;} public string CheckNumber{get;set;} public PaymentType PaymentType{get;set;} } I have a class that...

Variable Sized Struct C++

Is this the best way to make a variable sized struct in C++? I don't want to use vector because the length doesn't change after initialization. struct Packet { unsigned int bytelength; unsigned int data[]; }; Packet* CreatePacket(unsigned int length) { Packet *output = (Packet*) malloc((length+1)*sizeof(unsigned int)); ...

C#: Problem with DynamicCastHelper aka moving an array into a struct

Hello, Awhile ago I asked you another question(Click here to view the question). This is my problem when using that source file you gave me: public unsafe struct tPacket_5000_E { public Int16 size; public Int16 opcode; public byte securityCount; public byte securityCRC; public byte flag; fixed byte blowfish[8];...

incorrect members order in a C# structure

Hello, I have a TCP Client,which puts a packet in a structure using System.Runtime.InteropServices; [StructLayoutAttribute(LayoutKind.Sequential)] public struct tPacket_5000_E { public Int16 size; public Int16 opcode; public byte securityCount; public byte securityCRC; public byte flag; [MarshalAsAttribute(Unma...

P/Invoke function call problem

I am working on a system that requires interaction with a native C API using P/Invoke. Now I've (yet again) stumbled upon a problem which I cannot seem to solve in any way. The original function is designed to return 2 kinds of structures, based on a parameter that specifies which structure to use. The C header file defines the structur...

Rewriting C++ methods in C

How can I make equivalents to these methods in C? I read somewhere that they could be "replaced with functions that take a structure pointer as the first parameter," but I'm not sure how to do this, if that is the right thing to do. struct SCustomKeys { struct SCustomKey Save[10]; struct SCustomKey Load[10]; struct SCustomK...

How would I go about reading/writing a Structs contents to a file in C++ ?

I'm working on a homework project and i'm trying to store inventory data into a file. The inventory data size shouldn't be too large cause technically no one is going to really use it. I need to write these contents to a file: • Item Description • Quantity on Hand • Wholesale Cost • Retail Cost • Date Added to Inventory I am g...

I'm trying to read/write the size of multiple structs to a .dat file. I am getting compile errors, what am I doing wrong? (C++)

I've already asked 2 questions kind of related to this project, and i've reached this conclusion. Writing the size of the Struct to the file , and then reading it back is the best way to do this. I'm creating a program for a homework assignment that will allow me to maintain inventory. I need to read / write multiple structs of the same...

I'm getting a C++ compiler error with the '<<' operator. Anyone know why?

I've been working on getting this program complete where it saves multiple structs to a file, can read them back and edit them, then save them all back to a file. I've been working on the logic of this not to mention lots of help from others and a ton of googling hours... now I am getting a compile error. Any help would be very appreciat...

Why is the size of my vector full of structs so large when I load it's serialization in from a file?

As most of you may be following my line of questions already know, i'm trying to create a program that can serialize multiple structs to a .dat file, read them back in via loading it's serialization, edit the contents, and then re-write them to the file and so on. It's a inventory program I am trying to do and I can't get it to work for ...

Storing a COM pointer in a struct

My program is crashing every time I try to store a COM pointer into a struct, and then later try to use the original pointer. I don't have debug access to tell exactly what's wrong. pRend->cp = cpRT; ID2D1SolidColorBrush *scBrush; ERF(cpRT->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::CornflowerBlue), &scBrush)); It crashes on Cr...

Why Must I Initialize All Fields in my C# struct with a Non-Default Constructor?

I would like to try this code: public struct Direction { private int _azimuth; public int Azimuth { get { return _azimuth; } set { _azimuth = value; } } public Direction(int azimuth) { Azimuth = azimuth } } But it fails on compilation, I understand that struct need to init all its fields...

How do you specify what a default state of a derived struct should be in C#?

I've seen different questions on SO about not being able to use parameterless constructors or not setting field initializers, but I think my question kind of goes a step beyond that. What I would like to know is, how would I go about setting up the "default" value of a struct I need to define? Say I'm making a TaxID struct, and the d...

Can Someone explain the Marshal.StructureToPtr

I'm having some trouble with this code: //Creating a new ImageElement Struct ImageElement oElement = new UM0516.ImageElement(); //Create a pointer and allocate enough room for the struct type IntPtr pElement = Marshal.AllocHGlobal(Marshal.SizeOf(new UM0516.ImageElement())); //Copy the contents of the struct into the allocated memory spa...