struct

How does a compiled definition of struct look like in C?

Possible Duplicate: How does a compiled C++ class look like? Hi all, bash$cat struct.c struct test { int i; float f; }; bash$gcc -c struct.c The object file struct.o is of elf format. I am trying to understand what does this object file contain. The source code is just a definition of a struct. There is nothing executable...

Help with 2 C macros

I've defined 2 macros: #define HCL_CLASS(TYPE) typedef struct TYPE { \ HCLUInt rc; \ void (*dealloc)(TYPE*); #define HCL_CLASS_END(TYPE) } TYPE; \ TYPE * TYPE##Alloc() { TYPE *ptr = (TYPE *)malloc(sizeof(TYPE)); if (ptr != NULL) ptr->rc = 1; return ptr; } The purpose of these macros...

Anonymous union within struct not in c99?

Hi, here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node int_n; struct double_node double_n; }; }; int main(void) { struct int_no...

How to initialize last item automatically in a struct array?

I am passing an array to a function, and i am initializing it globally with some values. I am using empty string in end of array to determine the array length. Now, Is there some way to automatically initialize the array to have extra empty item in the end of it, so i have no chances to forget it from there? Just like the char[] works, ...

Strange C# struct in collection behaviour

Any ideas as to why this public Collection<Point> data = new Collection<Point>(){ new Point{X=10,Y=20}, new Point{X=20,Y=30}, new Point{X=40,Y=20}, new Point{X=10,Y=20} }; (notice the identical first and last elements) gives the error An item with the same key has alrea...

How to marshal a struct into a UInt16 Array

I know that you can use code like this to marshal a structure into a byte array: public static byte[] StructureToByteArray(object obj) { int len = Marshal.SizeOf(obj); byte[] arr = new byte[len]; IntPtr ptr = Marshal.AllocHGlobal(len); Marshal.StructureToPtr(obj, ptr, true); Marshal.Copy(ptr, arr, 0, len); Marsha...

How do I allocate memory and determine array sizes dynamically in C?

I am trying to teach myself C from a python background. My current mini-problem is trying to do less hard-coding of things like array lengths and allocate memory dynamically based on input. I've written the following program. I was hoping for suggestions from the community for modifying it in the following ways: 1.) Make first and ...

Why doesn't the CLR always call value type constructors

Hi, I have a question concerning type constructors within a Value type. This question was inspired by something that Jeffrey Richter wrote in CLR via C# 3rd ed, he says (on page 195 - chapter 8) that you should never actually define a type constructor within a value type as there are times when the CLR will not call it. So, for example...

Reading bytes into a structure in C#

Hello again, time for another off the wall question. I am writing an MD2 loader for my small 3D engine project. In my old language (C) I could define a structure and then read() from an open file directly into the structure. I have a structure to hold the header information from the MD2 file, as follows: [StructLayout(LayoutKind.Sequent...

Accessing variables in a union inside a struct

Can anyone please explain why the 1st method of accessing a nested struct element inside an union in a struct works and the 2nd does not? typedef struct element Node; struct element { int type; union { int value; Node *child[2]; } u; }; int main() { Node n; Node *p; n.type = 0; p = n.u...

Array of structs, multithreading, can I write at the same time to different indexes?

Hi I have an huge array which contains a struct "Tile". The program im writing is a 2D game, and I don't want different players (handled by different threads) to write their position to the same tile at the same time, and I wondered two things. Can two threads write to two different places in the array at the same time safely, and is th...

Is it possible to "embed" a structure of variable type into another structure? (GCC)

Is it possible to embed a structure of varying type inside another structure in C? Basically I want to do something like this. struct A { int n; void *config; } struct AConfig { int a; char *b; } struct BConfig { int a; float b; } const struct A table[] = { { 103, (void*)(struct AConfig){ 1932, "hello" } }, { 438, (void*)(str...

binary16 in Python

The struct module is useful when you're trying to convert data to and from binary formats. However, recently I came across a file format specification that uses the binary16 floating point format. I looked through the Python documentation, but can't find anything that can convert to and from it. What would be the best way to convert this...

Saving a C struct with a char* string into a file

I'm trying to save a struct with a char* string into a file. struct d_object { int flags; int time; int offset; char *filename; }; The problem is that when doing that I will obviously only save the address of that pointer rather than the string. So what I've done is simply use a character array and but I'm forced to ...

How to implement a 2-dimensional array of struct in C

Hey *, I'm currently trying to understand how to implement a 2-dimensional array of struct in C. My code is crashing all the time and I'm really about to let it end like all my approaches getting firm to C: garbage. This is what I got: typedef struct { int i; } test; test* t[20][20]; *t = (test*) malloc(sizeof(test) * 20 * 20); M...

When to use Struct instead of Hash in Ruby?

I don't have much programming experience. But to me, Struct seems somewhat similar to Hash. What tasks does Struct do very good? Is there anything Struct can do, but Hash cannot do? After googling, the concept of Struct is important in C, but I don't know much about C. ...

How to get rid of padding bytes between data members of a struct

Hi colleagues I have a binary file with "messages" and I am trying to fit the bytes inside the right variable using structs. In my example I used two types of messages: Tmessage and Amessage. #include <iostream> #include <fstream> #include <stdlib.h> #include <string> #include <iomanip> using namespace std; struct Tmessage { un...

How to Convert Structure to byte array in C# ?

Hi, How to Convert Structure to byte array in C# ? I have defined a structure like this : public struct CIFSPacket { public uint protocolIdentifier;//the value must be "0xFF+'SMB'" public byte command; public byte errorClass; public byte reserved; public usho...

Never defined structure

Is there any benefit in having never-defined structures in C ? Example in SQLite source code : /* struct sqlite3_stmt is never defined */ typedef struct sqlite3_stmt sqlite3_stmt; And the object is manipulated like so : typedef struct Vdbe Vdbe; struct Vdbe { /* lots of members */ }; int sqlite3_step(sqlite3_stmt *pStmt) { ...

Oddity with structs defined in files other than main.cpp

Hi everyone, I have found that a struct (with an array of doubles and one integer) defined in a separate Cpp-file, but called from main sends unreasonable values to cout for the array. Below what I hope to be a minimum example, along with the console output. My apologies should my code be scrambled -- I have been struggling a bit with ...