ECEC 201 · Week 2
Modern Computer: Von Neumann
A C type is a size in memory, and the size belongs to the system, not the language.
Week 2 of 10 · Das Frite
ECEC 201 · Week 2 · In class
A C type is a size in memory, and the size belongs to the system, not the language.
- This board is a 64-bit Arm Linux system.
- Print
sizeoffor each type and keep the table: you will use it every week after this one.
ECEC 201 · Week 2 · In class
Signed and unsigned values wrap differently at their limits.
- Two's complement is why a signed value turns negative.
- Print both results in decimal and in hexadecimal, then let the undefined-behaviour sanitizer show you which of the two the language does not define.
ECEC 201 · Week 2 · Lecture
A type is a size the system chooses
- C fixes only minimums and an order:
char≤short≤int≤long≤long long. - The exact sizes belong to the system's ABI, so a 32-bit and a 64-bit system can disagree.
sizeofanswers in bytes, at compile time, for this compiler on this system.- When a width must not change, use
<stdint.h>:int32_t,uint8_t.
ECEC 201 · Week 2 · Lecture
Printing the right way
#include <stdio.h>
int main(void)
{
printf("short: %zu bytes\n", sizeof(short));
return 0;
}
| Value | Format |
|---|---|
sizeof result (size_t) | %zu |
int / unsigned | %d / %u, hex %x |
long long | %lld |
ECEC 201 · Week 2 · Lecture
Two's complement, in 8 bits
| Bits | Hex | As unsigned | As signed |
|---|---|---|---|
| 0111 1111 | 0x7F | 127 | 127 |
| 1000 0000 | 0x80 | 128 | -128 |
| 1111 1111 | 0xFF | 255 | -1 |
Same bits, two readings. The top bit is the sign in the signed reading, so one step past the largest positive pattern lands on the most negative one.
ECEC 201 · Week 2 · Lecture
Defined wrap, undefined overflow
- The standard defines what one of the two overflows does and leaves the other undefined.
- Undefined means the compiler may assume it never happens, and optimise on that assumption.
gcc -fsanitize=undefinedadds checks at run time and prints the file and line of an undefined operation.- A program that prints the "expected" number is not proof the operation is defined.
ECEC 201 · Week 2 · Lab
Lab
- Write a program that prints `sizeof` for char, short, int, long, long long, float, double, and a pointer.
- Add one to a signed and to an unsigned 32-bit value at their limits, and print both results in decimal and hexadecimal. Second pass: compile with `-fsanitize=undefined`, run again, and record the line it reports.
ECEC 201 · Week 2 · Lab
Lab (continued)
- Assemble the lab hand-in: The program, its `sizeof` table, the two overflow results, and the sanitizer line.
ECEC 201 · Week 2 · Lab
Hand in
The program, its `sizeof` table, the two overflow results, and the sanitizer line.
ECEC 201 · Week 2 · Homework
Homework
- Before running anything, predict the eight `sizeof` values and the two overflow results on paper.
ECEC 201 · Week 2 · Homework
Turn in
The predictions, marked where the board disagreed.
ECEC 201 · Week 2 · Read
Read
On the lab site: Module 1, What a board is, names the SoC. This week grades C type sizes measured on that SoC.
ECEC 201 · Week 2 · Next
Week 3
A pointer is an address.