Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3
Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation What you MUST 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 Characters in RAM We 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 bit Value unsigned char Value 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 Declaration char = ‘f’, c = 87; char a, b = ‘f’, c = 87; 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 = We are placing the value 87 (= ) 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 a, b, and c ??? 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 ???
Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation (a) (b) (c) Given: char = ‘f’, c = 87; char a, b = ‘f’, c = 87; Where: Variable a => 5010: Unassigned Variable b => 5014: ‘f’ = = Variable c => 5015: =
Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Why Does Location a (address 5010) contain something?? We did NOT initialize the variable. previously Whatever was previously 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 !! 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: Because location 5010 contains a (signed) char, the numeric value is: Left-most bit = ‘1’: Value is negative: COMPLIMENT Since we are using 2’s Compliment: ADD 1: = -( ) = -( ) = -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 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: Evaluates to = = 225 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 propensity to lie to us !!! OK – here’s a program I wrote: And here’s the output 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 837, into a character location in RAM ??? char = 837; char illegal = 837; printf (“%c %d”, illegal, illegal); If we were to issue the commands: We would produce the output: E = WHY ??? BUT BUT requires 10-bits of storage 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, ONLY the right- most 8-bits will be stored: = = = 69 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 int) Integers require 2 CONTIGUOUS bytes (16-bits) of storage Consider the c declaration: int int x = ‘W’, y, z = 5487; In Fact we are (once again): (LOCATIONS x, y, and z) 3. Initializing location x with ‘W’ (= ASCII 87 = = on 16-bits) 4. Initializing location z with 5487 ( = = on 16-bits) (of course we all know it is really 4 Contiguous bytes) 1. Requesting 6-bytes (2 per variable) of RAM be reserved 2. 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 7214 and 7216 used??? contiguous Because we need 2 contiguous 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 int int x = ‘W’, y, z = 5487; x = y is unassigned z = x stored at address 7212 (and 7213) y stored at address 7219 (and 7220) z stored at address 7221 (and 7222) (x) (x) (y) (y) (z) (z)
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): = = = 6,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: int int x = ‘W’, y, z = 5487; x = y is unassigned z = x stored at address 7212 (to 7215) y stored at address 7219 (to 7222) z stored at address 7223 (to 7226) (x) (x) (x) (x) (y) (y) (y)7222 (y) (x) (x) (x) (x)
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 integer as as an ASCII character ??? Depends: int = 104; int anumber = 104; printf (“%c”, anumber); If we issue the commands: The output will be: h Which is the ASCII character for decimal value 104 int = 6502; int anumber = 6502; printf (“%c”, anumber); If we issue the commands: The output will be: f 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): right-most When we try to print out a character according to the ASCII tables, we consider ONLY 8-bits (the right-most 8-bits) = = = 102 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?? The same situation occurs as did when we entered an illegal value for the data type char: If we make the declaration: int = ; int badnumber = ; Since: 52, = Then: - 52, = ’s Comp = = = 1 3,102 Which is the output produced by the Statement: printf (“%d”, badnumber); 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!!! OK – let’s write another program – but instead of using the data type int, let’s use the data type short 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 program I wrote: And here is the output I received:
Page 23 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation One more quick example: Suppose we make the declaration: int = 72925; int badnumber = 72925; Where: = Requiring 17-bits Since an integer is stored on 16-bits: = = = 7,389 Which is the output produced by the Statement: printf (“%d”, badnumber);
Page 24 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Other Integers: Data Type unsigned int Bits Required 16 (2-bytes) Legal Values 0 through 65,535 (No sign bit) long Or 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 long 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 require 4 CONTIGUOUS bytes (32-bits) of storage Consider the c declaration: long long l1 = ‘3’, l2; In Fact we are (once again): 1. Requesting 8-bytes (4 per variable) of RAM be reserved 2. Associating each variable name with a reserved location (LOCATIONS l1, and l2) 3. Initializing location l1 with ‘3’ ( = ASCII 51 = = 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 9015 through 9017 used??? contiguous Because we need 4 contiguous 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 long long l1 = ‘3’, l2; l1 = l2 is unassigned l1 stored at address 9011 (through 9014) l2 stored at address 9021 (through 9024)
Page 28 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation What ‘Garbage’ will we find at location l1 ??? At addresses 9021 through 9024, we find: (On 32-bits) Since the left-most bit = ‘1’ ( ==> the number is negative) we must compliment: = -( )21) = -(8,388, ,048, , , , , , ) = 9,493,870 -9,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) data type float: CONTIGUOUS 4 CONTIGUOUS bytes (32-bits) per variable 7 decimals of precision Sometimes also referred to as single precision reals data type double: CONTIGUOUS 8 CONTIGUOUS bytes (64-bits) per variable ANSI: 10 decimals of precision data type long double: CONTIGUOUS 16 CONTIGUOUS 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 float float f1 = , f2; f1 stored at address 8910 (through 8913) f2 stored at address 8914 (through 8917) 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 ?? 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!!) Get ready for Quiz It’s coming
Page 32 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation