Questions tagged [struct]

A keyword in various programming languages whose syntax is similar to or derived from C (C++, C#, Swift, Go, Rust, etc.). Use a specific programming language tag to tag questions involving use of a `struct` as syntax and semantics can be language dependent. Keyword defines or declares a data type composed of other data types. Each member of a struct has its own area of memory (as opposed to a `union` whose members share a single area of memory).

A struct consists of a sequence of field names and their types (struct members), for example:

struct s {
    int   *i;                // pointer to an int
    char  *s;                // pointer to a char
    double d;                // a double
    int (*pFunc)(char *, int);  // pointer to a function
};

A struct can also contain bit fields to allow bit-level memory addressing:

struct bits {
    unsigned int b1 : 1;
    unsigned int b2 : 1;
    unsigned int b3 : 1;
    unsigned int b4 : 1;
    unsigned int b5 : 1;
    unsigned int b6 : 1;
    unsigned int b7 : 1;
    unsigned int b8 : 1;
};

Each member of a struct has its own area of memory as opposed to a union in which the members share the same area of memory.

The syntax for defining/declaring a struct as well as what it is possible to include in a struct definition/declaration varies between the various C style languages that use the keyword (e.g. member functions not allowed in C but are in C++ though both allow a pointer to a function).

The syntax for specifying and using a struct to define/declare variables may vary slightly between various C style programming languages ( s myVar; versus struct s myVar;)

Dynamic languages generally use some form of associative array in place of structs. The Pascal family of languages refer to these date types as records.

References

  1. Struct in Swift
  2. Struct in C#
  3. Struct in C++
  4. Struct in C
  5. Struct in Go
  6. Struct in Rust
  7. Classes and structs on MSDN
  8. Struct in Python
30239 questions
7
votes
1 answer

Usage of Structures in C

I am reading the script on the implementation of malloc (first-fit), and I am a little confused about the value assignment of metadata structure. Could anyone give some explanations why the malloc returns flag_block->ptr (as a pointer to the…
cyan
  • 101
  • 4
7
votes
2 answers

Declaring an array inside a Perl 6 NativeCall CStruct

Is there any way to declare an array of objects inside a CStruct? struct my_struct { int foo; int bar; char somestring[80]; }; class My::Struct is repr('CStruct') { has int32 $.foo; has int32 $.bar; ??? } A CArray[uint8]…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
7
votes
2 answers

Is it safe to memcpy to a dynamic storage struct?

Context: I was reviewing some code that receives data from an IO descriptor into a character buffer, does some control on it and then use part of the received buffer to populate a struct, and suddenly wondered whether a strict aliasing rule…
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
7
votes
2 answers

Initialize values of a struct pointer

I am starting to learn binary trees in cpp and dynamically allocated memories; thus to initialize a struct I do this struct node{ int val; node* left; node* right; }; //Initialize: node* root = new node; root->val = 7; root->left =…
polmonroig
  • 937
  • 4
  • 12
  • 22
7
votes
10 answers

Do I kill a kitten each time I use struct everywhere instead of class?

struct is public by default while class is private by default. Lets take Ogre3D for example; if I change all class occurences with struct, it compiles (I guess), and the engine works just as before. If I'm right, the compiled code is exactly the…
jokoon
  • 6,207
  • 11
  • 48
  • 85
7
votes
1 answer

Does inheritance via unwinding violate strict aliasing rule?

I have a struct X which inherits from struct Base. However, in my current setup, due to alignment, size of X is 24B: typedef struct { double_t a; int8_t b; } Base; typedef struct { Base base; int8_t c; } X; In order to save the…
Marcin Kolny
  • 633
  • 7
  • 14
7
votes
1 answer

condition_variable, mutex and flag in one struct

Is to ok to combine all three variables into one struct? struct lock_struct { std::mutex mutex; std::conditional_variable cv; bool flag; }; Are there any hidden synchronization problems with this approach? I do not intend to modify…
CorellianAle
  • 645
  • 8
  • 16
7
votes
2 answers

Is it guaranteed that there's no padding between values of the same type?

Consider this code: // T is *any* type struct str_T{ T a, b; }; I know that there's (almost always) padding between objects with different alignments because both members are of type T. But this time there's no different alignments. Can this…
iBug
  • 35,554
  • 7
  • 89
  • 134
7
votes
1 answer

Python 3 - Writing data from struct.unpack into json without individual recasting

I have a large object that is read from a binary file using struct.unpack and some of the values are character arrays which are read as bytes. Since the character arrays in Python 3 are read as bytes instead of string (like in Python 2) they cannot…
rovyko
  • 4,068
  • 5
  • 32
  • 44
7
votes
4 answers

Understanding C++ struct size

Consider the following code: struct CExample { int a; } int main(int argc, char* argv[]) { CExample ce1; CExample ce2; cout << "Size:" << sizeof(ce1) << " Address: " << &ce1 << endl; cout << "Size:" <<…
kiokko89
  • 211
  • 2
  • 7
7
votes
6 answers

C++ struct inheritance

As far as I know the main difference (and may be the unique one) between classes and structs in C++ is that classes have their members private by default and structs have theirs public by default. However, and probably because I was a C-developer…
M. Yousfi
  • 578
  • 5
  • 24
7
votes
1 answer

Struct Properties vs Generics in Racket

Racket seems to have two mechanisms for adding per-type information to structs: generics and properties. Unfortunately, the documentation doesn't seem to indicate when one is preferred over the other. The docs do say: Generic Interfaces provide a…
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
7
votes
1 answer

How are structs actually implemented in the C programming language?

Structs are a composite data structure in the C programming language; they consist of primitives such as ints and pointers all placed in memory in an adjacent fashion, such as an array. My question is, what are structs themselves made out of? Are…
the_endian
  • 2,259
  • 1
  • 24
  • 49
7
votes
2 answers

Adding method to a golang struct in a different file

How would one go about adding a method to a struct that is a different file? This is what I've tried so far but it doesn't seem to be working. // ./src package routes type App struct{ } func (a *App) initializeRoutes() { …
Joseph Palacio
  • 191
  • 1
  • 1
  • 5
7
votes
3 answers

struct pack return is too long

I'm trying to use the struct.pack function import struct values = (0, 44) s = struct.Struct('HI') b = s.pack(*values) print(b) print(str(len(b))) and it gives me this output: b'\x00\x00\x00\x00,\x00\x00\x00' 8 while the python docs say: Format -…
Dela
  • 146
  • 3
  • 15
1 2 3
99
100