It is difficult to parse binary info… hexadecimal makes it easier. We prefix an hexadecimal with “0x”.
So to represent 1111, we do 0xF. Or to represent 0110 we do 0x6.
Memory addresses are often in hexadecimal.
Data types occupy different space in memory.
Pointers & Dynamic Memory allocation
A pointer is an address. If I don’t wanna directly assign a pointer to something immediately, then I should assign it the value NULL.
Every time thatI use malloc I need to check if the return is NULL or not. If it is NULL, then I must end my program!
Stack and HEAP:
To statically obtain an ineger:
int x; (lives on the stack)
And dynamically?
int* px = malloc(4);
int*px = malloc(sizeof(int));
This gets us the space on the heap, and the pointer we get back is that *px.
Dynamically allocated memory is not automatically returned to the system for later use when the function in which it’s created finished execution.
To get memory back after using, we use free().
Very important 3 rules for dynamic memory allocation:
Structs
A lot of different characteristics inside of car. Remember to end with the semicolon.
We usually declare them in a separate .h file. (Useful so that they can be used in different files).
So what if I want to make structs in dynamic memory? Well, I need to save up enough space to hold all car variables.
I can write this simpler by using arrow operator:
Defining Custom Data Types
You can use this to shorten “struct car” for example, to car.
Can actually do it in a shorter way like this:
Hence we can know write: