Computing Technology
pixel art
RGB
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 a b c d e f
hexadecimal
base-16
161 160
##
16 1
##
16 1
00
16 1
01
16 1
02
16 1
03
16 1
04
16 1
05
16 1
06
16 1
07
16 1
08
16 1
09
16 1
0A
16 1
0B
16 1
0C
16 1
0D
16 1
0E
16 1
0F
16 1
10
16 1
11
16 1
12
16 1
13
16 1
14
16 1
…
16 1
FF
16 1
FF
16 × F + 1×F
16 1
FF
16 × 15 + 1 × 15
16 1
FF
240 + 15
16 1
FF
255
FF
F F
1111 1111
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
0 1 2 3 4 5 6 7
8 9 A B C D E F
10 11 12 13 14 15 16 17
18 19 1A 1B 1C 1D 1E 1F
0 1 2 3 4 5 6 7
8 9 A B C D E F
10 11 12 13 14 15 16 17
18 19 1A 1B 1C 1D 1E 1F
0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7
0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17
0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F
int n = 50;
50
n
50
0x123
&
*
%p
pointers
int n = 50;
int *p = &n;
int n = 50;
int *p = &n;
int n = 50;
int* p = &n;
int n = 50;
int * p = &n;
int n = 50;
int *p = &n;
50
n
50
0x123
0x123
p
50
0x123
0x123
p
50
0x123
p
50
0x123
string
string s = "HI!";
H I ! \0
H I ! \0
s[0] s[1] s[2] s[3]
H I ! \0
0x123 0x124 0x125 0x126
s
H I ! \0
0x123 0x124 0x125 0x126
0x123
s
H I ! \0
0x123 0x124 0x125 0x126
s
H I ! \0
0x123 0x124 0x125 0x126 0x127
string s = "HI!";
string s = "HI!";
char *s = "HI!";
typedef struct
{
string name;
string number;
} person;
typedef struct
{
string name;
string number;
} person;
typedef struct
{
string name;
string number;
} person;
typedef struct
{
string name;
string number;
} person;
typedef
typedef int
typedef int integer;
typedef
typedef char *
typedef char * string;
cs50.h
pointer arithmetic
typedef char * string;
typedef char *string;
50 50
i j
strcmp
s
s
H I ! \0
s
H I ! \0
0x123 0x124 0x125 0x126
0x123
s
H I ! \0
0x123 0x124 0x125 0x126
0x123
s t
H I ! \0
0x123 0x124 0x125 0x126
0x123
s t
H I ! \0 H I ! \0
0x123 0x124 0x125 0x126
0x123
s t
H I ! \0 H I ! \0
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
0x123 0x456
s t
H I ! \0 H I ! \0
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s t
H I ! \0 H I ! \0
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s
s
h i ! \0
s
h i ! \0
0x123 0x124 0x125 0x126
0x123
s
h i ! \0
0x123 0x124 0x125 0x126
0x123
s t
h i ! \0
0x123 0x124 0x125 0x126
0x123 0x123
s t
h i ! \0
0x123 0x124 0x125 0x126
s t
h i ! \0
0x123 0x124 0x125 0x126
s t
H i ! \0
0x123 0x124 0x125 0x126
malloc
free
…
s
h i ! \0
0x123 0x124 0x125 0x126
s t
h i ! \0
0x123 0x124 0x125 0x126
s t
h i ! \0
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s t
h i ! \0
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s t
h i ! \0 h
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s t
h i ! \0 h i
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s t
h i ! \0 h i !
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s t
h i ! \0 h i ! \0
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
s t
h i ! \0 H i ! \0
0x123 0x124 0x125 0x126 0x456 0x457 0x458 0x459
NULL
valgrind
garbage values
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
void swap(int a, int b)
{
}
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
scope
passing by value
↑
stack
machine code
↑
stack
machine code
globals
heap
↓
↑
stack
machine code
globals
heap
↓
↑
stack
machine code
globals
heap
↓
↑
stack
machine code
globals
heap
↓
↑
stack
machine code
globals
heap
↓
↑
stack
↑
stack
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
main
swap
main
main
x y
1 2
x y
swap
1 2
x y
a b tmp
1 2
x y
1 2
a b tmp
1 2
x y
int tmp = a;
a = b;
b = tmp;
1 2
a b tmp
1 2
x y
int tmp = a;
a = b;
b = tmp;
1 2 1
a b tmp
1 2
x y
int tmp = a;
a = b;
b = tmp;
2 2 1
a b tmp
1 2
x y
int tmp = a;
a = b;
b = tmp;
2 1 1
a b tmp
1 2
x y
2 1 1
a b tmp
1 2
x y
1 2
x y
passing by reference
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
1 2
x y
a b tmp
1 2
x y
int tmp = *a;
*a = *b;
*b = tmp;
a b tmp
1 2
x y
int tmp = *a;
*a = *b;
*b = tmp;
1
a b tmp
1 2
x y
int tmp = *a;
*a = *b;
*b = tmp;
1
a b tmp
2 2
x y
int tmp = *a;
*a = *b;
*b = tmp;
1
a b tmp
2 1
x y
1
a b tmp
2 1
x y
2 1
x y
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
machine code
globals
heap
↓
↑
stack
heap
↓
↑
stack
heap overflow
stack overflow
buffer overflow
"Sensors that received the new version of Channel File
291 carrying the problematic content were exposed to a
latent out-of-bounds read issue in the Content Interpreter.
At the next IPC notification from the operating system, the
new IPC Template Instances were evaluated, specifying a
comparison against the 21st input value. The Content
Interpreter expected only 20 values. Therefore, the attempt
to access the 21st value produced an out-of-bounds
memory read beyond the end of the input data array and
resulted in a system crash."
"Sensors that received the new version of Channel File
291 carrying the problematic content were exposed to a
latent out-of-bounds read issue in the Content Interpreter.
At the next IPC notification from the operating system, the
new IPC Template Instances were evaluated, specifying a
comparison against the 21st input value. The Content
Interpreter expected only 20 values. Therefore, the attempt
to access the 21st value produced an out-of-bounds
memory read beyond the end of the input data array and
resulted in a system crash."
get_char
get_double
get_float
get_int
get_long
get_string
...
get_int
scanf
…
50
n
s
s
s
s
file I/O
fopen
fclose
fprintf
fscanf
fread
fwrite
fseek
…
BMP
Computing Technology