Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.

Similar presentations


Presentation on theme: "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3."— Presentation transcript:

1

2 Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3

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

4 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

5 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 = 1100110 2 4. We are placing the value 87 (= 1010111 2 ) into location c

6 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 ???

7 Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation 5005 00101001 5006 11000110 5007 10111001 5008 00100111 5009 00000100 5010 (a) 11100001 5011 00000000 5012 01110011 5013 00000001 5014 (b) 01100110 5015 (c) 01010111 5016 11010011 5017 00100010 5018 10010010 5019 00000000 5020 00110110 Given: char = ‘f’, c = 87; char a, b = ‘f’, c = 87; Where: Variable a => 5010: Unassigned Variable b => 5014: ‘f’ = 102 10 = 01100110 2 Variable c => 5015: 87 10 = 01010111 2

8 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

9 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 1 Left-most bit = ‘1’: Value is negative: COMPLIMENT 0011110 Since we are using 2’s Compliment: ADD 1: + 1 1111100 = -(2 4 + 2 3 + 2 2 + 2 1 + 2 0 ) = -(16 + 8 + 4 + 2 + 1) = -31

10 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: 1 1 1 0 0 0 0 1 Evaluates to 2 7 + 2 6 + 2 5 + 2 0 = 128 + 64 + 32 + 1 = 225 Which corresponds to the ASCII Character 

11 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:

12 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 69 837 10 = 1101000101 2 WHY ??? BUT BUT requires 10-bits of storage WE RESERVED ONLY 8-BITS !!!

13 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: 1 1 0 1 0 0 0 1 0 1 = 2 6 + 2 2 + 2 0 = 64 + 4 + 1 = 69 Which Corresponds to the ASCII Character ‘E’

14 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 = 1010111 2 = 0000000001010111 2 on 16-bits) 4. Initializing location z with 5487 (5487 10 = 1010101101111 2 = 0001010101101111 2 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

15 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:

16 Page 15 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation int int 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 00101001 7212 (x) 00000000 7213 (x) 01010111 7214 00100111 7215 00000100 7216 11100001 7217 00000000 7218 01110011 7219 (y) 00011001 7220 (y) 01100110 7221 (z) 000101010 7222 (z) 01101111 7223 00100010 7224 10010010 7225 00000000 7226 00110110

17 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): 0001100101100110 72197220 = 2 12 + 2 11 + 2 8 + 2 6 + 2 5 + 2 2 + 2121 = 4096 + 2048 + 256 + 64 + 32 + 4 + 2 = 6,502 Which is the output we would obtain if we issued the command: printf (“%d”, y);

18 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 = 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 00101001 7212 (x) 00000000 7213 (x) 00000000 7214 (x) 0000000001010111 7215 (x)7216 01100001 7217 00010010 7218 00000000 7219 (y) 00000000 7220 (y) 0000000000011001 7221 (y)7222 (y) 01100110 7223 (x) 00000000 7224 (x) 00000000 7225 (x) 00010101 7226 (x) 0110111100010000 72277228 01111000

19 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 ???

20 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 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) = 2 6 + 2 5 + 2 2 + 2 1 = 64 + 32 + 4 + 2 = 102 Which corresponds to the ASCII character ‘f’

21 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 = -52434; int badnumber = -52434; Since: 52,434 10 = 1100110011010010 2 Then: - 52,434 10 = 0011001100101101 2 1’s Comp. + 1 0011001100101110 = 2 13 + 2 12 + 2 9 + 2 8 + 2 5 + 2 3 + 2 2 + 2 1 = 8192 + 4096 + 512 + 256 + 32 + 8 + 4 + 2 = 1 3,102 Which is the output produced by the Statement: printf (“%d”, badnumber); 2’s Comp.

22 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

23 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:

24 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: 72925 10 = 10001110011011101 2 Requiring 17-bits Since an integer is stored on 16-bits: = 2 12 + 2 11 + 2 10 + 2 7 + 2 6 + 2 4 + 2 3 + 2 2 + 2 0 = 4096 + 2048 + 1024 + 128 + 64 + 16 + 8 + 4 + 1 = 7,389 Which is the output produced by the Statement: printf (“%d”, badnumber);

25 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 ???

26 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 = 110011 2 = 000000000000000000000000110011 2 on 32-bits) (REMEMBER: The data types int and long are now the same)

27 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:

28 Page 27 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation long long l1 = ‘3’, l2; l1 = 0000000000000000 0000000000110011 l2 is unassigned l1 stored at address 9011 (through 9014) l2 stored at address 9021 (through 9024) 9011 00000000 9012 00000000 9013 00000000 9014 00110011 9015 00000100 9016 11100001 9017 00000000 9018 01110011 9019 00011001 9020 01100110 9021 111111111 9022 01101111 9023 00100010 9024 10010010 9025 00000000 9026 00110110

29 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: 11111111011011110010001010010010 (On 32-bits) Since the left-most bit = ‘1’ ( ==> the number is negative) we must compliment: 0000000100100001101110101101101 + 1 0000000100100001101110101101110 = -(2 23 + 2 20 + 2 15 + 2 14 + 2 12 + 2 11 + 2 10 + 28 28 + 26 26 + 25 25 + 23 23 + 22 22 + 21)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,870 -9,493,870 (1’s Comp.) (2’s Comp.)

30 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

31 Page 30 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation float float f1 = -92.337, f2; f1 stored at address 8910 (through 8913) f2 stored at address 8914 (through 8917) 8909 00000000 8910 10 100010 8911 01001110 8912 00110011 8913 00000100 8914 10100010 8915 01011101 8916 00100010 8917 00100010 8918 01100110 8919 101110011 8920 01101111 8921 00100010 8922 00100010 8924 00000000 8925 00110110 RAM Storage ??? Available: Addresses 8910 through 8993

32 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 1 --- It’s coming

33 Page 32 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation


Download ppt "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3."

Similar presentations


Ads by Google