Title: Variables and Objects, pointers and addresses:
1Variables and Objects, pointers and addresses
- variables and data objects are data containers
with names - the value of the variable is the code stored in
the container - to evaluate a variable is to fetch the code from
the container and interpret it properly - to store a value in a variable is to code the
value and store the code in the container - size of a variable is the size of its container
'A'
16916
Chapter 3, Slide 1
2overflow a code longer than the size of the
container is being stored at a data container
Problems with overflows in run-time (1) if the
whole part X of the memory belongs to the running
program, then (a) if X does not
contain any data important for the rest of the
execution of the program, then the
program runs fine and there is no
apparent problem
Chapter 3, Slide 2
3 (b) if X does contain important data which
get overridden by the
100101100000001011010100 tail of the binary code,
but by pure chance, it does not change
anything (as the data stored therein
just happened to be the same), then the program
runs fine and there is no apparent
problem (c) if X does contain important
data which get overridden and thus
changed, then (i) incorrect results
may be produced or (ii) the program
may crash with all kinds of possible error
messages. (2) if all or part of X
belongs to some other process, then the
program is terminated by the operating system for
a memory access violation (the infamous
UNIX segmentation fault error).
Chapter 3, Slide 3
401000001
010001
C/c Compilers take care of right overflow for
variables by truncating the code (of course
warnings should be produced by the compiler!)
char i..i 123456789printf("d\n",i)
displays 21 on the screen
Chapter 3, Slide 4
501000001
010001
C/C compilers take care of left overflow for
variables by truncating the code
char i..i 255..iprintf("d\n",i)
displays 0 on the screen
00000000
1
Chapter 3, Slide 5
6innate 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
7memory of a structure is contiguous! However the
placement may differimproper placement (from
the access point of view)
Chapter 3, Slide 7
8A proper placement (from the access point of
view)
Chapter 3, Slide 8
9Who 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
byte 0
byte 1
byte 2
byte 3
byte 4
byte 5
Pointers a pointer value is in essence an
address, a pointer variable is in essence a data
container to hold an address.
Chapter 3, Slide 9
10However, pointers know data type of whatever
they reference,with the exception of void
which is just a plain address. A pointer knows
what kind of object is at the end of the arrow!
Chapter 3, Slide 10
11Nameless data containers
But how a pointer can know what is at a
particular address?
Chapter 3, Slide 11
12What is stored in the four bytes at addresses
802340 .. 802343 ?(a) four characters 'A' 'B'
'C' 'D' (b) two shorts 16961, 17475(c) long
1145258561(d) float 781.035217(e) nobody can
tell Of course, (e) is the right answer.
Chapter 3, Slide 12
13Chapter 3, Slide 13
14Chapter 3, Slide 14
15For the actual values of anything but chars, the
byte order is important on a big endian machine
a short with value 1 looks like this(Mac, JVM,
TCP/IP NBO)
00000000
00000001
while in a little endian machine a short with
value 1 looks like this (Intel, most
communication hardware)
00000001
00000000
Normally (with variables), we do not have to
worry about it, the compiler knows it, and thus
storing and fetching of values is done
appropriately. We need to be aware of it when
messing up with pointers and storing/fetching
values through indirection.
Chapter 3, Slide 15
16int AmBigEndian() long x 1 return
!(((char )(x)))
on a big endian machine
00000000
00000001
00000000
00000000
returns 1
on a little endian machine
00000000
00000001
00000000
00000000
returns 0
Chapter 3, Slide 16
17Setting pointers(1) through dynamic allocation
(malloc, new) (2) through the address operator
(3) through calculation -- e.g. traversing
linked data structures (4) through assignment
with a special value -- e.g. memory mapped
I/Os etc. Indirection operator p means
evaluate the data container p points to or if
on the left-hand side of an assignment statement
(as an l-value), it means store at the data
container p points to. x
x p p x
Chapter 3, Slide 17
18End of slides for chapter 3
Chapter 3, Slide 18