Page 1
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
RAM AllocationChapter 3
Page 2
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
What you What you MUSTMUST know before we start: know before we start:
• What basic data types there are:
• Signed/Unsigned Characters• Signed/Unsigned Shorts/Integers/Longs
• The difference between characters & numbers
• How the sign-bit is used• How negative values are stored• What precision and magnitude are and how they
affect real numbers
(Remember: The topics in this course build on each other)
• float/double
• How many bits each data type requires
Page 3
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Storing Storing CharactersCharacters in RAM in RAMWe know that characters are really numbers:• signed char (or char, by default) on 8-bits:
8-bits = 1-byte = X X X X X X X X
Sign bitValue
• unsigned charValue
Regardless of whether signed or unsigned, the data type char requires 1-byte of storage per variable
Page 4
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Consider the following C DeclarationConsider the following C Declaration
charchar a, b == ‘f’, ‘f’, c c = 87= 87;;
What is the effect of the command ???What is the effect of the command ???
1. We are reserving 3-bytes of storage
2. We are associating each location with a variable name (LOCATIONS a, b, and c)
3. We are initializing location b with the Character ‘f’= ASCII 102= 11001102
4. We are placing the value 87 (= 10101112) into location c
Page 5
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Where in RAM will we find locations Where in RAM will we find locations aa, , bb, and , and cc ??? ???
We Don’t Know:We Don’t Know:• Address allocations are made at RUN-TIME and are based on available locations.
Assume that at run-time, we find that Addresses 5010, 5014, and 5015 are Available:• Variable a will be assigned to address 5010• Variable b will be assigned to address 5014• Variable c will be assigned to address 5015
How Will this appear in RAM ???How Will this appear in RAM ???
Page 6
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
5005
001010015006
110001105007
101110015008
001001115009
000001005010 (a)
111000015011
000000005012
011100115013
000000015014 (b)
011001105015 (c)
010101115016
110100115017
001000105018
100100105019
000000005020
00110110
Given: charchar a, b == ‘f’, ‘f’, c c = 87= 87;;
Where: Variable a => 5010: UnassignedVariable b => 5014: ‘f’ = 10210 = 011001102
Variable c => 5015: 8710 = 010101112
Page 7
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Why Does Location Why Does Location aa (address 5010) contain (address 5010) contain something?? We did NOT initialize the variable.something?? We did NOT initialize the variable.
Whatever was previouslypreviously stored at that location is still there.
If we were now (after variable allocation) to issue the command:
printf (“%c %d”, a, a);
We would produce the output:
-31
That Makes NO Sense at ALL !!That Makes NO Sense at ALL !!
Let’s Examine why this would occur
Page 8
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Location 5010 (variable a) contains: 11100001
Because location 5010 contains a (signed) char, the numeric value is:
1 1 1 0 0 0 0 1Left-most bit = ‘1’:Value is negative:COMPLIMENT
0 0 1 1 1 1 0Since we are using 2’s Compliment:ADD 1: + 1
1111100
= -(24 + 23 + 22 + 21 + 20) = -(16 + 8 + 4 + 2 + 1)
= -31-31
Page 9
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
That still doesn't explain why the ASCII character That still doesn't explain why the ASCII character is printed !!! is printed !!!
Even though the numeric value is -31, The value of location a is checked against the ASCII table as if it were an unsigned char:
1 1 1 0 0 0 0 1Evaluates to
27 + 26 + 25 + 20 = 128 + 64 + 32 + 1 = 225225
Which corresponds to the ASCII Character
Page 10
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
I don’t know if I believe – I know about your I don’t know if I believe – I know about your propensity to lie to us !!!propensity to lie to us !!!
OK – here’s a OK – here’s a program I wrote:program I wrote:
And here’s the output And here’s the output I received:I received:
Page 11
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
What if we try to store an illegal number, say What if we try to store an illegal number, say 837837, , into a character location in RAM ???into a character location in RAM ???
charchar illegal == 837;837;printf (“%c %d”, illegal, illegal);
If we were to issue the commands:
We would produce the output:
E 69
83710 = 11010001012
WHY ???WHY ???
BUTBUT requires 10-bits of storage
WE RESERVED ONLY 8-BITS !!!WE RESERVED ONLY 8-BITS !!!
Page 12
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Because we reserved only 1-byte, Because we reserved only 1-byte, ONLYONLY the the right-right-mostmost 8-bits will be stored: 8-bits will be stored:
1 1 0 1 0 0 0 1 0 1
= 26 + 22 + 20 = 64 + 4 + 1 = 6969
Which Corresponds to the ASCII Character ‘E’
Page 13
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Integers (c data type Integers (c data type intint))Integers require 2 CONTIGUOUS bytes (16-bits) of storage
Consider the c declaration:
intint x = ‘W’, y, z = 5487;
In Fact we are (once again):
(LOCATIONS x, y, and z)3. Initializing location x with ‘W’
(= ASCII 87 = 10101112 = 00000000010101112 on 16-bits)
4. Initializing location z with 5487(548710 = 10101011011112 = 00010101011011112 on 16-bits)
(of course we all know it is really 4 Contiguous bytes)
1. Requesting 6-bytes (2 per variable) of RAM be reserved2. Associating each variable name with a reserved location
Page 14
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Let’s Assume locations 7212, 7213, 7214, 7216, and 7219 through 7250 Are available:
• Variable x is assigned address 7212 (and 7213)• Variable y is assigned address 7219 (and 7220)• Variable z is assigned address 7221 (and 7222)
Why aren’t addresses Why aren’t addresses 72147214 and and 72167216 used??? used???
Because we need 2 contiguouscontiguous bytes of storage for integers:
• We can’t use location 7214 because location 7215 is NOT available
• We can’t use location 7216 because location 7217 is NOT available
Looking at RAM, We might see:
Page 15
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
intint x = ‘W’, y, z = 5487; x = 0000000001010111 y is unassigned z = 0001010101101111
• x stored at address 7212 (and 7213)• y stored at address 7219 (and 7220)• z stored at address 7221 (and 7222)
7211
001010017212 (x)
000000007213 (x)
010101117214
001001117215
000001007216
111000017217
000000007218
011100117219 (y)
000110017220 (y)
011001107221 (z)
0001010107222 (z)
011011117223
001000107224
100100107225
000000007226
00110110
Page 16
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Once again, Notice that there is ‘garbage’ in location y (Addresses 7219 & 7220):
00011001 01100110
7219 7220
= 212 + 211 + 28 + 26 + 25 + 22 + 21 = 4096 + 2048 + 256 + 64 + 32 + 4 + 2= 6,5026,502
Which is the output we would obtain if we issued the command:
printf (“%d”, y);
Page 17
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Lest we forget, since we know that integers are really stored on 32-bits:
intint x = ‘W’, y, z = 5487; x = 0000000001010111 y is unassigned z = 0001010101101111
• x stored at address 7212 (to 7215)• y stored at address 7219 (to 7222)• z stored at address 7223 (to 7226)
7211
001010017212 (x)
000000007213 (x)
000000007214 (x)
00000000 010101117215 (x) 7216
011000017217
000100107218
000000007219 (y)
000000007220 (y)
00000000 000110017221 (y) 7222 (y)
011001107223 (x)
000000007224 (x)
000000007225 (x)
000101017226 (x)
01101111 000100007227 7228
01111000
Page 18
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
What would happen if we tried to print an What would happen if we tried to print an integerinteger as as as an ASCII as an ASCII charactercharacter ??? ???
Depends:Depends: intint anumber == 104;104;printf (“%c”, anumber);• If we issue the commands:
The output will be: hh
Which is the ASCII character for decimal value 104
intint anumber == 6502;6502;printf (“%c”, anumber);• If we issue the commands:
The output will be: ff
WHAT ???WHAT ???
Page 19
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Don’t forget how the integer 6502 was stored (on 16-bits):
0 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0
When we try to print out a character according to the ASCII tables, we consider ONLY 8-bits (the right-mostright-most 8-bits)
= 26 + 25 + 22 + 21 = 64 + 32 + 4 + 2 = 102102
Which corresponds to the ASCII character ‘f’
Page 20
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
What if an illegal value is entered??What if an illegal value is entered??The same situation occurs as did when we entered an illegal value for the data type char:
If we make the declaration: intint badnumber == -52434;-52434;
Since: 52,43410 = 11001100110100102
Then: - 52,43410 = 00110011001011012 1’s Comp.+ 10011001100101110
= 213 + 212 + 29 + 28 + 25 + 23 + 22 + 21
= 8192 + 4096 + 512 + 256 + 32 + 8 + 4 + 2= 13,10213,102
Which is the output produced by the Statement: printf (“%d”, printf (“%d”, badnumberbadnumber));;
2’s Comp.
Page 21
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
I don’t know if I believe anymore -- About anything!!!I don’t know if I believe anymore -- About anything!!!
OK – let’s write another program – but instead of using the data type int, let’s use the data type short
WHY ????WHY ????
• Because, as we know, the data type short still requires 16-bits (2-bytes) of storage (As integers used to)
• We will NOT have to change our illustration
Page 22
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Once again, here is the Once again, here is the program I wrote: program I wrote:
And here is the And here is the output I received:output I received:
Page 23
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
One more quick example:One more quick example:Suppose we make the declaration: intint badnumber == 72925;72925;
Where: 7292510 = 100011100110111012
Requiring 17-bits
Since an integer is stored on 16-bits:
= 212 + 211 + 210 + 27 + 26 + 24 + 23 + 22 + 20
= 4096 + 2048 + 1024 + 128 + 64 + 16 + 8 + 4 + 1= 7,3897,389
Which is the output produced by the Statement: printf (“%d”, printf (“%d”, badnumberbadnumber));;
Page 24
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Other Integers:Other Integers:
Data Type
unsigned int
Bits Required
16 (2-bytes)
Legal Values
0 through 65,535(No sign bit)
longOr signed long
32 (4-bytes) -2,147,483,648 through 2,147,483,647
unsigned long 32 (4-bytes) 0 through 4,294,967,295
How would the data type How would the data type longlong appear in RAM ??? appear in RAM ???
Page 25
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Long Integers (c data type Long Integers (c data type longlong))Integers require 4 CONTIGUOUS bytes (32-bits) of storage
Consider the c declaration:
longlong l1 = ‘3’, l2;
In Fact we are (once again):
1. Requesting 8-bytes (4 per variable) of RAM be reserved2. Associating each variable name with a reserved location
(LOCATIONS l1, and l2)3. Initializing location l1 with ‘3’
( = ASCII 51 = 1100112 = 0000000000000000000000001100112 on 32-bits)
(REMEMBER: The data types int and long are now the same)
Page 26
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Let’s again assume locations 9011 through 9017, and 9021 through 9076 Are available:
• Variable l1 is assigned address 9011 (through 9014)• Variable l2 is assigned address 9021 (through 9024)
Why aren’t addresses Why aren’t addresses 90159015 through through 90179017 used??? used???
Because we need 4 contiguouscontiguous bytes of storage for longs:
• If we were to try and store variable l2 at location 9015, we would need addresses 9016 through 9018 to be available also
Looking at RAM, We might see:
Page 27
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
longlong l1 = ‘3’, l2; l1 = 0000000000000000 0000000000110011 l2 is unassigned
• l1 stored at address 9011 (through 9014)• l2 stored at address 9021 (through 9024)
9011
000000009012
000000009013
000000009014
001100119015
000001009016
111000019017
000000009018
011100119019
000110019020
011001109021
1111111119022
011011119023
001000109024
100100109025
000000009026
00110110
Page 28
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
What ‘Garbage’ will we find at location What ‘Garbage’ will we find at location l1l1 ??? ???At addresses 9021 through 9024, we find:
11111111011011110010001010010010 (On 32-bits)
Since the left-most bit = ‘1’ ( ==> the number is negative) we must compliment:
0000000100100001101110101101101+ 1
0000000100100001101110101101110
= -(223 + 220 + 215 + 214 + 212 + 211 + 210 + 28 + 26 + 25 + 23 + 22 + 21)= -(8,388,608 + 1,048,576 + 32,768 + 16,384 + 4,096 + 2,048 + 1,024 + 256 + 64 + 32 + 8 + 4 + 2)= -9,493,8709,493,870
(1’s Comp.)
(2’s Comp.)
Page 29
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
Floating-point numbers (real numbers)Floating-point numbers (real numbers)
data type float:• 4 CONTIGUOUSCONTIGUOUS bytes (32-bits) per variable• 7 decimals of precision• Sometimes also referred to as single precision reals
data type double:• 8 CONTIGUOUSCONTIGUOUS bytes (64-bits) per variable• ANSI: 10 decimals of precision
data type long double:• 16 CONTIGUOUSCONTIGUOUS bytes (128-bits) per variable• ANSI: 10 decimals of precision
Page 30
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
floatfloat f1 = -92.337, f2;
• f1 stored at address 8910 (through 8913)• f2 stored at address 8914 (through 8917)
8909
000000008910
101000108911
010011108912
001100118913
000001008914
101000108915
010111018916
001000108917
001000108918
011001108919
1011100118920
011011118921
001000108922
001000108924
000000008925
00110110
RAM Storage ???RAM Storage ???
Available: Addresses 8910 through 8993
Page 31
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation
So what do we need to do ??So what do we need to do ??• Make sure you THOUROGHLY understand ALL of the
concepts covered in these slides
• Answer ALL of the relevant questions on the Review Page
• Submit your References
• Submit your Question(s)
• Look at the Bits/Bytes/ASCII C/C++ Programming Assignment (it’s not due yet, but it can’t hurt to look at it)
??? Any Questions ??? (Please!!)??? Any Questions ??? (Please!!)
• Get ready for Quiz 1 --- It’s coming
Page 32
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
RAM Allocation