struct

Accessing Win32 C/C++ struct members from C#

I am intercepting Win32 API calls a native dll or exe is doing from C# using some kind of hooking. In this particular case I am interested in DrawText() in user32.dll. It is declared like this in Win32 API: INT WINAPI DrawTextW(HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags) The LPRECT struct has the following signature (als...

Structure Vs Class in C#

When you create an instance of a class with the new operator, memory gets allocated on the heap. When you create an instance of a struct with the new operator where does the memory get allocated, on the heap or on the stack ? ...

C++: ctors for structs?

C++: Since a struct is a class with everything "public", are default -ctors created and called? The reason I ask is to understand the overhead, if any, that C++ may have over C, when structs are used. An opinion I have heard is that classes have some overhead that structs don't, in C++, but I question this. ...

Why should we typedef a struct so often in C?

I have seen many programs consisting of structures like the one below typedef struct { int i; char k; } elem; elem user; I have seen this many times. Why is it needed so often? Any specific reason or applicable area? ...

Passing pointers/references to structs into functions

This is going to sound like a silly question, but I'm still learning C, so please bear with me. :) I'm working on chapter 6 of K&R (structs), and thus far through the book have seen great success. I decided to work with structs pretty heavily, and therefore did a lot of work early in the chapter with the point and rect examples. One of ...

Why is it necessary to call :this() on a struct to use automatic properties in c#?

If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; State = state; Zip = zip; } public string Line1 { get; protected ...

Passing a value with struct type into a function in C

typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void push(element e, queue s) { nat lt = s->length; if (lt == max_length - 1) { printf("Error in push: Queue is full.\n"); return; } else { s->c...

Handling large amount of structures in MATLAB

I need to handle massive (tens of millions) of MATLAB structs; I needed a dozen or so fields so I reckoned memory won't be an issue, until I discovered this ( explanation ) >> s=[]; >> s.first=1; >> whos Name Size Bytes Class Attributes s 1x1 132 struct >> s.second=2; >> wh...

Solution for "dereferencing `void *' pointer" warning in struct in C?

Hi, I was trying to create a pseudo super struct to print array of structs. My basic structures are as follows. /* Type 10 Count */ typedef struct _T10CNT { int _cnt[20]; } T10CNT; ... /* Type 20 Count */ typedef struct _T20CNT { long _cnt[20]; } T20CNT; ... I created the below struct to print the array of above mentioned ...

Why do I get this error creating & returning a new struct?

I get an error when I compile this code: using System; public struct Vector2 { public event EventHandler trigger; public float X; public float Y; public Vector2 func() { Vector2 vector; vector.X = 1; vector.Y = 2; return vector; // error CS0165: Use of unassigned local variable 've...

Initializing an Array of Structs in C#

How can I initialize an const / static array of structs as clearly as possible? class SomeClass { struct MyStruct { public string label; public int id; }; const MyStruct[] MyArray = { {"a", 1} {"b", 5} {"q", 29} }; }; ...

Why is using a class as a struct bad practice in Java?

We recently had a code review . One of my classes was used so that I could return/pass more than one type of data from/to methods . The only methods that the class had were getters/setters . One of the team's members ( whose opinion I respect ) said that having a class like that is bad practice ( and not very OOP ) . Why is that ? ...

memcpy vs assignment in C

Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware? I am using GCC 4.2.x on a 32 bit Intel platform (but am interested in 64 bit as well). ...

C++ CLI structure to byte array

I have a structure that represents a wire format packet. In this structure is an array of other structures. I have generic code that handles this very nicely for most cases but this array of structures case is throwing the marshaller for a loop. Unsafe code is a no go since I can't get a pointer to a struct with an array (argh!). I c...

How to initialize a struct in ANSI C?

Hi guys! I'm not a specialist for ANSI C (or regular C at all), so I stumbled about this stupid thing: I want to initialize a struct element, splitted in declaration and initialization. This is what I got: typedef struct MY_TYPE { boolean flag; short int value; double stuff; } MY_TYPE; void function(void) { MY_TYPE a; ... ...

Why can't I define a default constructor for a struct in .NET?

In .NET a value type (C# struct) can't have a constructor with no parameters. According to this post this is mandated by the CLI spec. What happes is that for every value-type a default constructor is created (by the compiler?) which initialized all members to zero (or null). Does anyone know why it is disallowed to define such a defaul...

How to compare two elements of the same but unconstrained generic type for equality?

I've got the following generic class and the compiler complains that "Operator '!=' cannot be applied to operands of type 'TValue' and 'TValue'" (see CS0019): public class Example<TValue> { private TValue _value; public TValue Value { get { return _value; } set { if (_value != value) // <<...

How do I enforce using a factory on a struct in C#

I have a c# struct where I need to forbid calling the no args constructor on it. MyStruct a; /// init a by members // OK MyStruct b = MyStruct.Fact(args); // OK, inits by memebers MyStruct s = new MyStruct(); // can't have that I'm doing this mostly to force explicet values for all members as there are no valid default values and a...

C Pointer confusion

Hi! I want to store a string in memory and read it later: $$->desc.constant->base.id = (char*)malloc(200); sprintf($$->desc.constant->base.id, "%f", $1); printf("->%s\n", $$->desc.constant->base.id); //LINE A printf("->%i\n", $$->desc.constant); //LINE B //SOME OTHER CODE //Then, later on in a function call: printf("%i", expr->desc...

Difference between a Structure and a Union in C

Is there any good example to give the difference between a 'struct' and a 'union'? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference? ...