Mem 3
Mem 3
'A' 16916
01000001 0100000101000100
Chapter 3, Slide 1
overflow = a code longer than the size of the container is
being stored at a data container:
variable i
01001001100101100000001011010100
X
Problems with overflows in run-time:
Chapter 3, Slide 3
01000001010001…
char i;
..
i = 123456789;
printf("%d\n",i);
Chapter 3, Slide 4
…01000101000001
char i;
..
i = 255;
..
i++;
printf("%d\n",i);
displays 0 on the screen
100000000
Chapter 3, Slide 5
innate data types: char, unsigned char --- 1 byte
short, unsigned short --- 2 bytes
int, unsigned int --- 4 bytes
long, unsigned long --- 4 bytes
float --- 4 bytes
double --- 8 bytes
only char and unsigned char do not depend on the platform.
The values shown are typical for a 32-bit architecture.
The size of a variable (or value) can be calculated (in
compile-time) by the operator sizeof exp
Complex “data containers” - structures, records
struct {
char a;
int b;
} x;
Chapter 3, Slide 6
memory of a structure is contiguous! However the “placement”
may differ:
variable x
x.a x.b
0100100110010110000000101101010001101101
Chapter 3, Slide 7
A proper placement (from the access point of view):
data variable x
completely
ignored, junk
padding
x.a x.b
01001001 10010110000000101101010001101101
Chapter 3, Slide 8
Who creates the padding? The compiler!
Who knows the size of a structure? The compiler?
So, if we need to know the actual size of a structure, we use the
sizeof operator!
From data point of view, objects are like structures and classes
are like struct constructs.
Memory addressing:
Chapter 3, Slide 10
Nameless “data containers”:
8090346
int* p
integer
"data container"
Chapter 3, Slide 11
...0101 010000010100001001000011010001001100...
Chapter 3, Slide 12
address
802340
address
802340
Chapter 3, Slide 13
address
802340
address
802340
Chapter 3, Slide 14
For the actual values of anything but char’s, the byte order is
important:
on a big endian machine a short with value 1 looks like this:
(Mac, JVM, TCP/IP NBO)
0000000000000001
0000000100000000
{returns 1
on a little endian machine
00000001000000000000000000000000
{
returns 0
Chapter 3, Slide 16
Setting pointers:
Indirection operator *
x = x + *p;
*p = x;
Chapter 3, Slide 17
End of slides for chapter 3
Chapter 3, Slide 18