Shared Flashcard Set

Details

Systems Terms: Structs
Terms from Lecture 10 of my Systems class
16
Computer Science
Graduate
03/04/2025

Additional Computer Science Flashcards

 


 

Cards

Term
What is a struct?
Definition
A grouping of data, usually of different types
Term
Size of a pointer
Definition
8 bytes
Term
Why can't a struct contain itself?
Definition
The compiler needs to know how big the struct is at compile-time, it can't just wait and deal with it at runtime
Term
Why can't you initialize a struct's pointer data member with dot notation?
Definition
It is telling the pointer to point at something else, then you lose the thing it was pointing at
Term
How to initialize a struct's character array?
Definition
strcpy(matthew.first_name, "matthew")
Term
Why do we use arrow notation with a struct pointer?
Definition
The struct pointer must be dereferenced (to a struct) before its data members can be accessed
Term
Are structs passed by value or by reference?
Definition
By value
Term
Why is it faster to pass a struct pointer into a function than a struct?
Definition
Structs are passed by value, so a copy of the struct is made. Returning a struct also makes a copy.
Term
When does it make sense to forward declare a struct?
Definition
When declaring a pointer to the struct or functions that take the struct as a parameter
Term
What is a bitfield?
Definition
A struct that allocates a certain predetermined number of bits to each data member
Term
Use case for bitfields
Definition
Hardware devices that use bits as on/off switches, like a smart thermostat
Term
Will sizeof() work for bitfields?
Definition
No, sizeof() is for bytes, not bits
Term
What is a union?
Definition
Like a struct, but it only stores a single member at a time. When you set one data member, it overwrites the one you last set.
Term
Size of a union
Definition
The size of its largest data member
Term
When to use a union
Definition
You want to store something, but it could be any different type
Term
How are structs stored in memory?
Definition
The data members are stored in memory as they are written, the same as if you had just declared the members outside of the struct. But the compiler can rearrange them!
Supporting users have an ad free experience!