Title: Overview
1Overview
- Stack and the Heap
- malloc() and free()
- Pointers
- numbers in binary
2C Memory Management
- C requires knowing where objects are in memory,
otherwise don't work as expect - Java hides location of objects
- C has 3 pools of memory
- Static storage global variable storage,
basically permanent, entire program run - The Stack local variable storage, parameters,
return address(location of "activation records"
in Java or "stack frame" in C) - The Heap (dynamic storage) data lives until
deallocated by programmer
3Pointer Arithmetic
- Which of the following are valid? (and what do
they mean?) - pointer integer
- integer pointer
- pointer pointer
- pointer integer
- integer pointer
- pointer pointer
- compare pointer to pointer
- compare pointer to integer (to 0?)
4The Stack
- Stack frame includes
- Return address
- Parameters
- Space for other local variables
- Stack frames contiguous blocks of memory stack
pointer tells where top stack frame is - When procedure ends, stack frame is tossed off
the stack frees memory for future stack frames
SP
5Who cares about stack management?
- Pointers in C allow access to deallocated memory,
leading to hard to find bugs ! - int ptr () int y y 3 return
ymain () int stackAddr,content
stackAddr ptr() content
stackAddr printf("d", content) / 3
/ content stackAddr printf("d", content)
/13451514 /
6The Heap (Dynamic memory)
- Large pool of memory, not allocated in
contiguous order - back-to-back requests for heap memory could
result blocks very far apart - where Java new command allocates memory
- In C, specify number of bytes of memory
explicitly to allocate item - int ptrptr (int ) malloc(4) / malloc
returns type void , so need to cast to right
type / - malloc Allocates raw, uninitialized memory from
heap
7C Memory Allocation malloc()
- Instead of explicit number, for portability, use
sizeof() - int ptrptr (int ) malloc(sizeof(int))
- not a procedure will check type or a variable to
turn into a number - malloc() also for structure allocation
- struct DlistNode nodePtrnodePtr (struct
DlistNode ) malloc(sizeof(struct DlistNode))
- Note unlike Java, C never frees memory
programmer must explicitly free memory
8C Memory Allocation free()
- free() is opposite of malloc()
- Deallocates the memory so that it may be re-used
by malloc() - Danger may accidentally deallocate memory when
still have a pointer into it, causing the same
problem as with pointers to stack space
9C Memory Allocation
- Rule of thumb deallocate anything you're never
going to use again - If not too much, and program doesn't run a long
time, then allocate a lot at the beginning and
then let memory be freed when program ends - Otherwise, end up with "Memory Leaks", that is,
program gets bigger over time, and need to
restart computer - In a large program with shared data structures,
how do you know when something can be deallocated?
10Odds and Ends
- Structure declaration does not allocate memory
- Variable declaration does allocate memory
- If declare inside procedure, allocated on the
stack - If declare outside a procedure, allocated in
static storage
11Pointers to structures
- The C arrow operator (-gt) dereferences and
extracts a structure field with a single
operator. - The following are equivalent
struct point p printf(x is d\n,
(p).x) printf(x is d\n, p-gtx)
12p
- p difference
- increment by ??
- Char p, q
- While(p q)
13Comparison
- How do you tell if X gt Y ?
14Which base do we use?
- Decimal great for humans, especially when doing
arithmetic - Hex if human looking at long strings of binary
numbers, its much easier to convert to hex and
look 4 bits/symbol - Terrible for arithmetic on paper
- Binary what computers use you will learn how
computers do ,-,,/ - To a computer, numbers always binary
- Regardless of how number is written
- 3210 0x20 1000002
- Use subscripts ten, hex, two in book,
slides when might be confusing
15Limits of Computer Numbers
- Bits can represent anything!
- Characters?
- 26 letters ? 5 bits (25 32)
- upper/lower case punctuation ? 7 bits (in 8)
(ASCII) - standard code to cover all the worlds languages
? 16 bits (unicode) - Logical values?
- 0 ? False, 1 ? True
- colors ? Ex
- locations / addresses? commands?
- but N bits ? only 2N things
Red (00)
Green (01)
Blue (11)
16How to Represent Negative Numbers?
- So far, unsigned numbers
- Obvious solution define leftmost bit to be sign!
- 0 ? , 1 ? -
- Rest of bits can be numerical value of number
- Representation called sign and magnitude
- MIPS uses 32-bit integers. 1ten would be
- 0000 0000 0000 0000 0000 0000 0000 0001
- And - 1ten in sign and magnitude would be
- 1000 0000 0000 0000 0000 0000 0000 0001
17Shortcomings of sign and magnitude?
- Arithmetic circuit complicated
- Special steps depending whether signs are the
same or not - Also, Two zeros
- 0x00000000 0ten
- 0x80000000 -0ten
- What would 2 0s mean for programming?
- Therefore sign and magnitude abandoned
18Another try complement the bits
- Example 710 001112 -710 110002
- Called Ones Complement
- Note positive numbers have leading 0s, negative
numbers have leadings 1s.
- What is -00000 ? Answer 11111
- How many positive numbers in N bits?
- How many negative ones?
19Shortcomings of Ones complement?
- Arithmetic still a somewhat complicated.
- Still two zeros
- 0x00000000 0ten
- 0xFFFFFFFF -0ten
- Although used for awhile on some computer
products, ones complement was eventually
abandoned because another solution was better.
20Standard Negative Number Representation
- What is result for unsigned numbers if tried to
subtract large number from a small one? - Would try to borrow from string of leading 0s,
so result would have a string of leading 1s - 3 - 4 ? 000011 - 000100 111111
- With no obvious better alternative, pick
representation that made the hardware simple - As with sign and magnitude, leading 0s ?
positive, leading 1s ? negative - 000000...xxx is gt0, 111111...xxx is lt 0
- except 11111 is -1, not -0 (as in sign mag.)
- This representation is Twos Complement
212s Complement Number line N 5
00000
00001
- 2 N-1 non-negatives
- 2 N-1 negatives
- one zero
- how many positives?
11111
11110
00010
0
-1
1
2
11101
-2
-3
11100
-4
. . .
. . .
15
-15
-16
01111
10001
10000
22Twos Complement for N32
- 0000 ... 0000 0000 0000 0000two
0ten0000 ... 0000 0000 0000 0001two
1ten0000 ... 0000 0000 0000 0010two
2ten. . .0111 ... 1111 1111 1111 1101two
2,147,483,645ten0111 ... 1111 1111 1111
1110two 2,147,483,646ten0111 ... 1111 1111
1111 1111two 2,147,483,647ten1000 ... 0000
0000 0000 0000two 2,147,483,648ten1000 ...
0000 0000 0000 0001two 2,147,483,647ten100
0 ... 0000 0000 0000 0010two
2,147,483,646ten. . . 1111 ... 1111 1111
1111 1101two 3ten1111 ... 1111 1111 1111
1110two 2ten1111 ... 1111 1111 1111
1111two 1ten - One zero 1st bit called sign bit
- 1 extra negativeno positive 2,147,483,648ten
23Twos Complement Formula
- Can represent positive and negative numbers in
terms of the bit value times a power of 2 - d31 x -231 d30 x 230 ... d2 x 22 d1 x 21
d0 x 20 - Example 1111 1100two
- 1x-29 1x28 1x27... 1x220x210x20
- -29 28 27 ... 22 0 0
- -128 64 32 16 8 4
- -128 12
- -4ten
24Twos complement shortcut Negation
- Change every 0 to 1 and 1 to 0 (invert or
complement), then add 1 to the result - Proof Sum of number and its (ones) complement
must be 111...111two - However, 111...111two -1ten
- Let x ? ones complement representation of x
- Then x x -1 ? x x 1 0 ? x 1 -x
- Example -4 to 4 to -4x 1111 1111 1111 1111
1111 1111 1111 1100twox 0000 0000 0000 0000
0000 0000 0000 0011two1 0000 0000 0000 0000
0000 0000 0000 0100two() 1111 1111 1111 1111
1111 1111 1111 1011two1 1111 1111 1111 1111
1111 1111 1111 1100two
25Twos comp. shortcut Sign extension
- Convert 2s complement number rep. using n bits
to more than n bits - Simply replicate the most significant bit (sign
bit) of smaller to fill new bits - 2s comp. positive number has infinite 0s
- 2s comp. negative number has infinite 1s
- Binary representation hides leading bits sign
extension restores some of them - 16-bit -4ten to 32-bit
- 1111 1111 1111 1100two
- 1111 1111 1111 1111 1111 1111 1111 1100two
26Signed vs. Unsigned Variables
- Java just declares integers int
- Uses twos complement
- C has declaration int also
- Declares variable as a signed integer
- Uses twos complement
- Also, C declaration unsigned int
- Declares a unsigned integer
- Treats 32-bit number as unsigned integer, so most
significant bit is part of the number, not a sign
bit
27Numbers represented in memory
- Memory is a place to store bits
- A word is a fixed number of bits (eg, 32) at an
address - Addresses are naturally represented as unsigned
numbers in C
00000
01110
101101100110
11111 2k - 1
28Negative Numbers
- Sign Magnitude
- First bit is the sign, rest of the bits are the
value - Get two negatives and addition is hard (try it)
- 2s compliment
- Only one zero, arithmetic is easy
- Add a number to its inverse and get 0
- Just flip the bits and add one to negate
- Sign extend if needed
- Now we need to worry about overflow!
- Signed vs. unsigned int
29Signed v. Unsigned Comparisons
- X 1111 1111 1111 1111 1111 1111 1111 1100two
- Y 0011 1011 1001 1010 1000 1010 0000 0000two
- Is X gt Y?
unsigned YES signed NO
30What if too big?
- Binary bit patterns above are simply
representatives of numbers. Strictly speaking
they are called numerals. - Numbers really have an infinite number of digits
- with almost all being same (000 or 111) except
for a few of the rightmost digits - Just dont normally show leading digits
- If result of add (or -,,/) cannot be represented
by these rightmost HW bits, overflow is said to
have occurred.
31And in Conclusion...
- We represent things in computers as particular
bit patterns N bits ? 2N - numbers, characters, ...
- Decimal for human calculations, binary to
understand computers, hexadecimal to understand
binary - 2s complement universal in computing cannot
avoid, so learn - Computer operations on the representation
correspond to real operations on the real thing - Overflow numbers infinite but computers finite,
so errors occur