struct

Deserialize a byte array to a struct

I get a transmission over the network that's an array of chars/bytes. It contains a header and some data. I'd like to map the header onto a struct. Here's an example: #pragma pack(1) struct Header { unsigned short bodyLength; int msgID; unsigned short someOtherValue; unsigned short protocolVersion; }; int main() { ...

C# - How can I set the value of auto property backing fields in a struct constructor?

Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty) { this.StringProperty = stringProperty; this.IntProperty = intProperty; } public String StringProperty { get; set; } public Int32 IntProperty { get; set; } } Of course, a compiler error ...

Getting the size of an indiviual field from a c++ struct field

The short version is: How do I learn the size (in bits) of an individual field of a c++ field? To clarify, an example of the field I am talking about: struct Test { unsigned field1 : 4; // takes up 4 bits unsigned field2 : 8; // 8 bits unsigned field3 : 1; // 1 bit unsigned field4 : 3; // 3 bits unsigned field5 ...

Why isn't pass struct by reference a common optimization?

Up until today, I had always thought that decent compilers automatically convert struct pass-by-value to pass-by-reference if the struct is large enough that the latter would be faster. To the best of my knowledge, this seems like a no-brainer optimization. However, to satisfy my curiosity as to whether this actually happens, I created...

Clojure Structure Nested Within Another Structure

Is it possible to have a structure nested within a structure in Clojure? Consider the following code: (defstruct rect :height :width) (defstruct color-rect :color (struct rect)) (defn #^{:doc "Echoes the details of the rect passed to it"} echo-rect [r] (println (:color r)) (println (:height r)) (println (:width r))) (def first...

Can someone explain this definition of the 'dirent' struct in Solaris?

Recently I was looking at the 'dirent' structure (in dirent.h) and was a little puzzled by its definition. NOTE: This header file is from a Solaris machine at my school. typedef struct dirent { ino_t d_ino; off_t d_off; unsigned short d_reclen; char d_name[1]; } dirent_t; Particularly the d_name field. How would t...

Adding an int member to a C struct causes a segfault

I'm still learning C, and have started using it to generate images. I can't figure out why one of my programs is segfaulting. Here's the source code, cut down to 40 lines: #include <stdio.h> #include <stdlib.h> struct color { unsigned char r, g, b; }; struct image { int w, h/*, o*/; struct color **data; }; int ma...

self referential struct definition?

Hi I haven't been writing C for very long, and so I'm not sure about how I should go about doing these sorts of recursive things... I would like each cell to contain another cell, but I get an error along the lines of "field 'child' has incomplete type". What's up? typedef struct Cell { int isParent; Cell child; } Cell; Thanks, ...

When are structs the answer?

I'm doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you create millions of them, they don't live longer than a single method, they're lightweight. However, by simply changing 'struct' to 'class' on Vector and Ray, I got a v...

"Bus error" accessing a set<int> from a struct

Searched for a while, but I can't figure out why this would raise a bus error. Any help would be much appreciated. typedef struct { set<int> pages; } someStruct; ... void someFunction() { ... someStruct *a = createSomeStruct(); // just mallocs and returns a->pages.insert(5); ... } ...

What does this error mean: "error: expected specifier-qualifier-list before 'type_name'"?

I'm a bit new to working with c/c++, so sorry if this is a dumb question. I've been working on the Cell processor and I'm trying to create a struct that will hold an spe_context_ptr_t, which will be used within the thread to launch an spe context and will also hold a pointer to something else that will be passed to the spu context from w...

Struct vs Class for long lived objects

When you need to have very small objects, say that contains 2 float property, and you will have millions of them that aren't gonna be "destroyed" right away, are structs a better choice or classes? Like in xna as a library, there are point3s, etc as structs but if you need to hold onto those values for a long time, would it pose a perfo...

Immutability of structs

I read it in lots of places including here that it's better to make structs as immutable. What's the reason behind this? I see lots of Microsoft-created structs that are mutable, like the ones in xna. Probably there are many more in the BCL. What are the pros and cons of not following this guideline? ...

long long alignment problem (MSVC vs. GCC)

I'm writing C cross-platform library but eventually I've got error in my unittests, but only on Windows machines. I've tracked the problem and found it's related to alignment of structures (I'm using arrays of structures to hold data for multiple similar objects). The problem is: memset(sizeof(struct)) and setting structures members one ...

Difference between 'struct' and 'typedef struct' in C++?

In C++, is there any difference between: struct Foo { ... }; and typedef struct { ... } Foo; ...

What is the best way to initialize a bitfield struct in C++?

In C++, I have a class which contains an anonymous bitfield struct. I want to initialize it to zero without having to manually write out all fields. I can imagine putting the initialization in three places: Create a constructor in the bitfield Zero out in the initializer list of the constructor for the containing class Zero out in th...

struct c dynamically allocate memory

Hello, I am using a struct and I want to initialize a maximum of 10 ports. However, when the program is running it could be a lot less, we don't know until run-time. However, this will be the max. I have never done struct like this before, as I normally dynamically allocate using calloc and delcare like this *ports as the value type. ...

undefined C struct forward declaration

Hello, I have a header file port.h, port.c, and my main.c I get the following error: 'ports' uses undefined struct 'port_t' I thought as I have declared the struct in my .h file and having the actual structure in the .c file was ok. I need to have the forward declaration as I want to hide some data in my port.c file. In my port.h I ...

structure calloc c

Hello, C99 gcc I keep getting this error. I have a struct outside main. And inside main I am trying to allocate on the stack using calloc. I can't seem to find out what is wrong. Thanks for any advice, error: expected expression before ‘)’ token /* global */ struct port_data_t ...

Forward declare pointers-to-structs in C++

I am using a 3rd party library that has a declaration like this: typedef struct {} __INTERNAL_DATA, *HandleType; And I'd like to create a class that takes a HandleType in the constructor: class Foo { Foo(HandleType h); } without including the header that defines HandleType. Normally, I'd just forward-declare such a type, but I ...