Title: CS 1312
1CS 1312
- Introduction to
- Object Oriented Programming
- Lecture 25
- C Pointers
- Structs
- Preprocessing
2Bits, Bytes and Words
- A bit is a binary digit which can have a value of
0 or 1 - A byte today is normally considered to be 8 bits
but was originally designated as the smallest
addressable unit of memory - Thus one could instruct the computer to store or
retrieve a given byte but not a given bit. - Bytes may be grouped into words (e.g. 4 bytes)
- (plus all kinds of zany combinations thereof like
half-words, double words, etc.) - So words are addressed in multiples of 4
- Since the bytes are addressed by one.
3Conceptual Memory
- Each memory cell thus has two associated numbers
- Its address
- Its contents
- When using any symbolic language we can also
associate a symbol or identifier - Example
- Memory Address Contents Symbol
- or Identifier
- 2000 7 someInt
We write int someInt someInt 7
4Conceptual Memory
Technically this only exists up through
compilation.
- Memory Address Contents Symbol
- or Identifier
- 2000 7 someInt
5Strictly Speaking
- The computer has no idea what were actually
doing (exception see HAL in 2001) - So a given memory location such as a byte only
has a limited number of possible values - Byte 0 - 255 (00 - FF16)
- It is the programmer using a programming language
that can assign this byte a meaning - Unsigned numbers from 0 to 255
- Signed numbers from -128 to 127
- Characters from a typical encoding such as ASCII
where for example the letter A is represented
by the number 65 - Thus we could also store in memory the address of
any memory location. This is known as a pointer.
6More Memory
- Notes
- The SYMB column isnt actually stored in memory.
- In fact, neither is the column marked ADDR!
- Assume that we are showing words (32 bits)
- Assume ints take a word
2000
7
ix
2004
42
iy
2008
-17
iz
7Initializing It
2000
7
ix
Note Where the actual values get stored depends
on several factors. Since we deal with the
symbols, we dont worry about this most of the
time.
2004
2008
8Assigning to it
- The code
- int ix 7
- int iy, iz
-
2000
7
ix
2004
?
iy
2008
?
iz
9Assigning to it
- The code
- int ix 7
- int iy, iz
- iy 42
-
2000
7
ix
2004
42
iy
2008
?
iz
10Assigning to it
- The code
- int ix 7
- int iy, iz
- iy 42
- iz iy - 59
2000
7
ix
2004
42
iy
2008
-17
iz
11A Pointer at last!
1000
pint
2000
7
ix
Possible Points of Confusion 1. The pointer is
named pint not pint 2. It is not necessarily the
size of an int rather it is pointing to an int.
Important in a minute.
2004
42
iy
2008
-17
iz
Can also be written as... int pint
12Make it point!
- The code
- int pint
- pint ix
1000
2000
pint
2000
7
ix
2004
42
iy
2008
-17
iz
13Dereferencing
- The code
- int pint
- pint ix
- pint 8
-
1000
2000
pint
2000
8
ix
Syntax Note int pint Means that when pint
is dereferenced it will be pointing to an int
2004
42
iy
2008
-17
iz
14Dereferencing
- The code
- int pint
- pint ix
- pint 8
- pint iy
-
1000
2000
pint
2000
50
ix
2004
42
iy
2008
-17
iz
15Fasten Your Seat Belt!
was 2000
- The code
- int pint
- pint ix
- pint 8
- pint iy
- pint 1
-
1000
????
pint
2000
50
ix
2004
42
iy
2008
-17
iz
16Fasten Your Seat Belt!
- The code
- int pint
- pint ix
- pint 8
- pint iy
- pint 1
-
1000
2004
pint
2000
50
ix
2004
42
iy
Why do they call it notorious pointer arithmetic?
2008
-17
iz
17Pop Quiz
- int x, y
- int px x
- int py y
- x 7
- py 5
- py px
- px px y py
- printf(d d\n, px, py)
- What prints?
18Pop Quiz
- int x, y
- int px x
- int py y
- x 7
- py 5
- py px
- px px y py
- printf(d d\n, px, py)
- 19 19
19Hilarious Implications
- Remember we only have in parameters in C
- So we can fake it!
- Lets say we want to write a function (procedure)
to swap two numbers - void swap(int a, int b)
-
- int t
- t a
- a b
- b t
-
- Doesnt work since parameters are in
- Pass by Value
20But we can fool the computer!
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
-
- Called like this
- int x, y
- x 3
- y 4
- swap(x, y)
Notice 1. We didnt actually create any pointer
variables in our calling sequence. 2. We dont
change a and b inside of swap just what they are
pointing to...
21Pop Quiz
- include ltstdio.hgt
- void print_num (int x)
-
- printf("Number d\n", x)
-
- int main (void)
-
- int a 3
- int b
- b a
- print_num(??)
- return 0
-
Which one goes here to print 3? b b b
22Pop Answer
- include ltstdio.hgt
- void print_num (int x)
-
- printf("Number d\n", x)
-
- int main (void)
-
- int a 3
- int b
- b a
- print_num(b)
- return 0
-
23Pop Quiz
- include ltstdio.hgt
- void print_num (int x)
-
- printf("Number d\n", x)
-
- int main (void)
-
- int a 3
- int b
- b a
- print_num(b)
- return 0
-
b Pointer to an int b What b is pointing
at b Address where b is stored
24Now it gets...
- C allows us to have arrays which appear to be
like other languages - int a100
- creates an array of 100 elements which can be
referenced as a0 through a99. - Note that an array created in this manner is
truly an array - However, we can also do strange and wonderful
things now like - int b
- b a
- Note This is the same as b a0
25Strange and Wonderful Thing I
- int main(void)
- int a10
- int b
- int i
- for (i 0 i lt 10 i)
- ai i
- b a / What happens here, recall b a0
/ - for (i0 i lt 10 i)
-
- (bi) (bi) 2 / And here? /
- printf("d d\n", i, ai)
-
- return 0
26Strange and Wonderful Thing II
- int main(void)
- int a10
- int b
- int i
- for (i 0 i lt 10 i)
- ai i
- b a
- for (i0 i lt 10 i)
-
- bi bi 2 / Say what (kareoke)?
/ - printf("d d\n", i, ai)
-
- return 0
27Strange and Wonderful Thing III
- int main(void)
- int a10
- int b
- int i
- for (i 0 i lt 10 i)
- ai i
- b a
- for (i0 i lt 10 i)
-
- ib ib 2 / Aarrrgggghhhhhhhhh!!!
/ - printf("d d\n", i, ai)
-
- return 0
b4
4b
28Caution
- Many people will say that arrays in C are just
pointer manipulations but an actual array is a
special thing - So while it was okay to say
- b a
- It would have been wrong to say
- a b
- Once you create a true array
- int a10
- You shouldnt try and assign something else to a
29Actual Array
- In C, we can dynamically allocate memory and
were going to just show you enough to make you
incredibly dangerous. - DONT CUT GREENLEES LECTURES ON THIS SUBJECT
- It could be a huge mistake!
- We are only telling you part of the
story!!!!!!!!!!!!!!!!!
30Allocating Memory
- There is a function in a C library which allows
you to ask for some memory. You tell it how much.
It tells you that - a. You got it and where its located
- OR
- b. You didnt get it
- The feared and dreaded
- void malloc(size_t n)
31Pseudomemories
- Remember the new function in Pseudocode?
- You said current lt- new(Node)
- So the idea was that the size of the Node was
looked up somewhere and then enough memory was
allocated to hold one Node. - Malloc works sort of like that only you can ask
for as much memory as you want. - Note that both return pointers.
32Warning, Danger, Will Robinson
- This is not a complete treatment of dynamic
memory management in C. - There is no automatic garbage collection in C
- There is no automatic garbage collection in C
- There is no automatic garbage collection in C
- There is no automatic garbage collection in C
- You must eventually learn
- Memory allocation
- Memory reallocation
- Memory deallocation
- From Mr. Greenlee - Youll love it, honest! -)
33Simple Malloc
- Lets say you want space for 100 ints.
- Here is what you do
- int pint
- pint (int )malloc(100 sizeof(int))
- if (null pint)
-
- / You did not get the space! /
- / Handle error here -- very important /
-
- / Now you have effectively an array of ints /
- for (i 0 i lt 100 i)
-
- pinti 0
-
34Going crazy?
- Did we mention pointers to pointers?
- We did this before
- int x
- int px
- px x
- We can also go one (or more) step(s) further
- int ppx
- ppx px
- Which would allow us to do things like
- ppx 42 / Puts 42 into x /
Ready to run back to Java yet?
35Visually
1000
??
x
2000
??
3000
??
36Visually
1000
??
x
2000
??
px
3000
??
37Visually
1000
??
x
2000
??
px
3000
??
ppx
38Visually
- int x
- int px
- int ppx
- px x
1000
??
x
2000
1000
px
3000
??
ppx
39Visually
- int x
- int px
- int ppx
- px x
- ppx px
1000
??
x
2000
1000
px
3000
2000
ppx
40Visually
- int x
- int px
- int ppx
- px x
- ppx px
- ppx 42
1000
42
x
2000
1000
px
3000
2000
ppx
41Stringing You Along
- Since were talking about arrays and pointers and
pointers to pointers we should briefly mention
strings - Strings in C are implemented as arrays of
characters - So just as you can say
- int a 0, 1, 3, 5
- and C will correctly dimension a to be a4
- You can also say
- char b t, e, s, t, \0
- And you can even say
- char c test
42Watch this closely
- char aLine Hi, there!
- char pLine Hi, there!
- These are similar but not the same!!!
- The first actually creates space for an array of
characters (11) which can be subsequently
modified - aLine4 T
- The second creates a pointer which is pointing to
a string constant so that - pLine4 T would be undefined while
- pLine aLine
- pLine4 T / is okay /
43Over the top!
- It is not only possible to have pointers to
pointers but one can even use arrays of pointers! - char name Illegal, Jan, Feb, Mar,
Apr, May, Jun, Jul, Aug, Sep, Oct,
Nov, Dec - Now for example name2 is a pointer to the
character string F, e, b, \0 - What would we do with an array of pointers?
44Imagine
- We have some names like
- Smith, David
- Leahy, Bill
- Dagon, David
- Morales IV, Ricky Martin
- Suppose we want to sort the names but we note
that they are of different lengths. - Instead of running a sort algorithm which swaps
the character strings around why not create an
array of pointers
45We have an array of pointers
Smith, David
name0
Leahy, Bill
name1
Dagon, David
name2
Morales IV, Ricky Martin
name3
46And we just move the pointers in the array to sort
Smith, David
name0
Leahy, Bill
name1
Dagon, David
name2
Morales IV, Ricky Martin
name3
47More fun
names
0
- If we define
- char names30
- we get
- The following are all legal
-
- char z
- names4 z
- names5 fred
- names6 (char)malloc(20sizeof(char))
1
...
29
Pointers to chars
48Running with the big dogs
- The classic data structure in C which is
equivalent to - Records in Pseudocode (Pascal)
- Classes (without methods) in Java
- Whatever in Scheme (Like a list sort of...)
- is called a struct
- They look like this
- struct ordPair
-
- int x
- int y
-
49More structs
- So we could declare
- struct ordPair opAlpha, opBeta
- and access them like this
- opAlpha.x 7
- opAlpha.y 8
- opBeta opAlpha
- Structs are what we would use for the classic
linked list node - struct LLnode
-
- int data
- struct LLNode next
-
50Putting it all together
- Here is how we could declare a head pointer
- struct LLnode head
- here is how we could get a new node dynamically
- head (struct LLnode )malloc(sizeof(struct
LLnode))
51One last thing
- C allows you to make up your own type identifiers
using typedef - This is used as follows with struct definitions
- typedef struct Node
-
- int data
- struct Node next
- Node
- Now we can say
- Node head
52Questions?
53Preprocessing
Compilation begins by moving the C source files
through a preprocessor--a simple text
substitution device. One can designate strings
for substitution with the expression
define ltexpressiongt ltsubstitutegt
54A Tale of Two Expansions
The C preprocessor has many features--and
pitfalls. define g(y) get_index(y) g(z) defin
e g (y) get_index (y) g(z)
get_index(z) (y) get_index (y) (z)
55The Power of Parens
Inputs
Results
define DIVIDE(x, y) x/y DIVIDE(3, 34)
3/34
define DIVIDE(x, y) (x)/(y) DIVIDE (3, 34)
(3)/(34)
56More Substitution
Whats wrong with this?
define square(x) x x
And not (z4) (z4)
57Common Sense Preprocessing
1
Watch extra white space, as shown in the previous
examples.
2
Liberally use parentheses to protect order of
precedence
58Questions?
59(No Transcript)