structs

When do structs not live on the stack?

I'm reading through Jon Skeet's book reviews and he is going over the numerous inaccuracies of Head First C#. One of them caught my eye: [Under Errors Section] Claiming that structs always live on the stack. In what situations would structs not live on the stack? This goes contrary to what I thought I knew about structs. ...

hide parameterless constructor on struct

Is it possible to hide the parameterless constructor from a user in c# I want to force them to always use the constructor with parameters e.g. this Position class public struct Position { private readonly int _xposn; private readonly int _yposn; public int Xposn { get { return _xposn; } } public int Y...

Accessing struct members directly

I have a testing struct definition as follows: struct test{ int a, b, c; bool d, e; int f; long g, h; }; And somewhere I use it this way: test* t = new test; // create the testing struct int* ptr = (int*) t; ptr[2] = 15; // directly manipulate the third word cout << t->c; // look if it really affect...

Is it reasonable to have more than 65536 User Defined Types in large projects?

I'm thinking about some stuff related to runtime type info, and I'd like some feedback from programmers who work on much larger projects than I do. Is it at all reasonable to expect any program to ever have more than 65536 (2^16) user-defined types (classes and structs) in a single project? This does not mean 65536 instances, it means ...

Structs to Byte Arrays to send over sockets

What is the best way to get a byte array from a struct to send over TCP sockets? I'm using .Net (VB or C#). ...

Questions about Structs

MSDN says that a class that would be 16 bytes or less would be better handled as a struct [citation]. Why is that? Does that mean that if a struct is over 16 bytes it's less efficient than a class or is it the same? How do you determine if your class is under 16 bytes? What restricts a struct from acting like a class? (besides disallowin...

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

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 structs 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...

Arrays of structs or native data types in Objective C

I've run into this problem a couple times now. Last time, I wanted to create an array of arrays (matrix) of BOOL's. I ended up encapsulating them in NSStrings, because apparently NSArray only has arrays of objects. This time, want an array of arrays again, but of CGPoints. I'm going to be using these to draw images to the screen...

Coldfusion - What's an efficient way to search an array of structs?

I have a semi-large (hundreds of records) 1-dimensional array in Coldfusion. Each item in the array is a struct with several properties. I want to search the array for a struct that has a specific "name" property. I know that for an array of string values I could use Java methods like so: <cfset arrayIndex = myArray.indexOf("WhatImLooki...

A deep struct-like Equals() for .NET classes?

Given two objected that do not contain reference loops within them, do you know a method that tests their equality in a "generic" way (through reflection)? I basically want the same semantics as struct equivalence, only on classes. ...

Use of structs

can anyone provide a realworld example of when a struct can be used? ...

Best Way of Implementing these 3 classes in C#: Vector, Direction (unit vector), Point

All Points are Vectors, and all Vectors are Points. All Directions are Vectors, NOT all Vectors are Directions (this shouldn't mean both way conversion shouldn't be allowed). I want to have the operators overridden once for all preferably since they're all completely identical. In C++ I can just define class Vector { float x,y,z; }, a...

SubSonic error: db.tables with a ~ in the name result in a "Character is not valid" error

I renamed a database table and added a ~ character to the table name (my way of notifying it is a backup of something). After generating the SubSonic files, my Visual Studio gives a "Character not valid" error on file "AllStructs.vb" When looking into this file the following line of code was generated; Public Shared ReadOnly DbTable1~ol...

Compare two structs' values in C#

I'm not looking for a comparison of two structs that returns bool, I am wondering if there is a way to get which fields of two structs (the same structure, but maybe different values) are different. Basically I want a simpler way to do the following: public class Diff { public String VarName; public objec...

C Structs and arrays

I can't get the code below to compile (see errors). Advice on correction would be appreciated. #include <stdio.h> typedef struct { char *fldName; unsigned fldLen; } Field; typedef struct { char *fldPrompt; unsigned startRow; unsigned startCol; } Prompt; typedef struct { Field *fields[]; Prompt *prompts[]; uns...

(.net) Is there any intrinsic difference between .net Explicit structs and c++ unions?

That is the question. Is there anything you can do with c++ unions that you can't with c# Explicit structs? ...

Pointer to struct within the nested structs

Hello all, I'm trying to run the following code(in gcc 4.3 on fedora 11 i586 ): #include #include #include struct s_smallstruct{ int smallstruct; }; struct s_test2{ char * test2; struct s_smallstruct* smallstruct; }; struc...

How do I get the FieldInfo of an array field?

I'm trying to get the field info of an array value from within a struct. So far I have the following, but I dont see how to get the infomration I want. [StructLayout(LayoutKind.Sequential)] public struct Test { public byte Byte1; [MarshalAs(UnmanagedType.ByValArray, SizeConst=3)] public Test2[] Test1...

Reading data from file into array of structs C++

I have a sample txt file and want to read the contents of the file into an array of structs. My persons.txt file contains 5 arbitrary nos one on each line. 7 6 4 3 2 My program looks like this: #include <iostream> #include <fstream> using namespace std; struct PersonId { typedef PersonId* ptr; PersonId(); int fId; }; ...

How do I pass a struct from an unmanaged C++ program to a C# program?

This is my second post. Here is what I am trying to do: Call an unmanaged c++ program from c#, passing in a array of structs from the c# program and return a updated version of the array of structs from the c++ program. Here is the calling c# program: using System; using System.Runtime.InteropServices; namespace TestCallingC { c...