quartz/content/notes/11-struct-and-union.md
2022-11-03 15:03:39 +13:00

3.1 KiB

title aliases tags sr-due sr-interval sr-ease
11-struct-and-union
cosc204
lecture
2022-11-10 19 230

Struct

not a calss

  • class like composite data type called struct
  • POD plain old data

declare

struct <name>{
	<type><name>;
	<type><name>;
} <variable>;

struct point_2d {
	double x;e
	double y;
} point;

typically an anonymous struct is declared and then given a name with typedef

typedef struct {
	double x;
	double y;
} point_2d;

This is the common convention now.

initalize

point_2d location = {0.0, 1.0}; //initalizers
point_2d location = {.y = 1.0, .x = 0.0}; //designated initalizers

accessing

use the .

point_2d location;

location.x = 0;
location.y = 1;

shallow copy

copy the members including pointers

  • but not the things the pointers point to

shallow copy example 1|200 shallow copy example 2|400

both point to the same memory location. when you change one - they both change.

passing to routines

by value

need to copy the whole objcet, usually slower

void thing_print(thing object) {
	print("%f ",object.value)
	print("%f\n", object.string)	
	object.value = object.value -1; //does not affect first because it is a copy of object
}

thing_print(first)

by address

usually faster. less data coming off and onto the stack

void thing_print(thing *object) {
	print("%f ",object->value) //struct->field === (*struct).field
	print("%f\n", object->string)	
	object->value = object->value -1; //does not affect first because object is a pointer to first
}

thing_print(&first)

returning

  • return by value
  • never return a pointer to somthing on the stack

Queue

  • enqeue to tail
  • dequeue from head
  • FIFO

diagram|400

enqueue with pointers

queue is a linked list NULL as sentinel value at end

diagram linked list|400

  • go down list till we find null
  • create new object (malloc), value = 123, pointer = NULL
  • change null in final object to the new object
typedef struct q_item {
	int value;
	stuct q_item *next;
} queue_item;

//for convenience we have a struct for the queue
typedef struct {
	queue_item *head;
} queue;

constructor

queue *queue_new(void){
	queue *this = malloc(sizeof(queue));
	this->head = NULL;
	return this;
}

enqueue enqueue code|400

dequeue dequeue code|400

Structs as view

typedef struct q_item {
	int value;             //4-byte integer
	struct q_item *next;   //8-byte pointer
} queue_item;

struct is a view where the members are laid out consecutively

Union

multiple views of the same memory

  • each line in the declaration is a different view
typedef union {
	struct {
		uint8_t a, b, c, d;
	};
	uint32_t integer;
} two_views;

diagram|100

endianess

big-endian vs little-endian some computer store the most significatn byte first, other store the least significant byte first