Addendum to Video 3
- To use hexadecimal code in the debugger windows, you have to prefix it with 0x
Endianness
- But what happens if we ask for more than 8 bits?
- For example, the number 500 in binary is...
- 0000 0001 1111 0100
- The first byte is just one (higher order byte)
- The second byte is 244 (lower order byte)
- But why in memory, does 244 appear first, then the 1?
- This is because of Endianness
- Little Endian (low order byte comes first): x86, ARM, x64
- Big Endian (high order byte comes first): PowerPC (PS3)
- This matters in file formats!
- For example, Photoshop made on Mac, which used Motorola 68k, and it was Big Endian based
- File formats were Big Endian of their source architecture
Structs
- When programming for reals, we don't want to have to constantly declare everything as parts of little bytes and those sorts of things.
- C allows us to declare composites of many different types, all of which may have different byte counts
- A struct is a collection of variables that go together.
- You're referring to a memory layout that's sequentially ordered and usually stored back to back (but sizes can be different due to padding and alignment)
- struct is the backbone of C programming
- Struct Example:
- And then we can access the members of the struct like this...
But why is my struct not the right size? (11 bytes vs 16 bytes)
- In this struct, it should be a total of 11 bytes.
- Compiler is not under any constraint to lay out a struct as compactly as possible
- Although it adds up to 11 bytes (1+4+4+2) in reality, compiler has boundaries and will (in this case) assign 4 bytes to each
- You can use sizeof() to get the size of a variable
Hexadecimal
0 - 01 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 6
7 - 7
8 - 8
9 - 9
A - 10
B - 11
C - 12
D - 13
E - 14
F - 15
- 0xA = 10
- 0xAA 16 * 10 + 10 = 170
- 0xAAA = 16*16*10 + 16*10 + 10
- Hex is used because it more concise than binary, and it lines up nicely byte-wise
Casting
- Treat Test as if it were a bunch of shorts
- Look at memory occupied by Test as if it were a bunch of 16 bit values.
- C is statically typed.
- This means that C keeps track of what you said the type of a variable/struct/etc was
- Compiler spits out error if you try to use something differently than the way it was declared.
- For example...
In the Memory debugger, why are byte values listed at 204?
- In hex, 204 is 0x000000cc
- 0xcccccccc would be 204 204 204 204
- When you compile in debug mode in VS, something says"clear all values to write cc (204)"
- This is because a lot of errors originate from uninitialized values
- So 0xcc means "uninitialized"
- In a release build, this doesn't happen since it's inefficient!
Array indexing
- In C, arrays are just a memory layout
- Array is a collection of objects that is indexed by a value (usually a number).
- They are placed sequentially in memory
- They also arranged in memory reliably.
- It's spaced using a known number.
- Example:
More on C and Arrays
- C is a little wonky because of...
- Projectiles is just a pointer to ProjectTilePointer.
- C knows this as well, BUT it understands that it is a much bigger thing!
- They are NOT the same type.
In other words....
- So all three of these ARE THE SAME THING!!!!
Reference vs. Dereference
- To put it simply -> (Dereference). is used for pointers
- . (Reference), is used to access the actual variable
- Error is C2228 left of '' must have class/struct/union...
- Solution is almost always to switch . to -> or -> to .
- Functionally, there is no difference between these 2.
- Same thing, only difference is that one's isn't a pointer and the other isn't
#pragma
- __attribute__
- You can use these to wrap any struct in a pay to tell the compilers that is has to be packed.
- This will force C to pack it tightly, and not auto-assumed to a certain size.
No comments:
Post a Comment