Bases and Representations, Memory, Pointers, Arrays, For-Loops

Slides:



Advertisements
Similar presentations
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Advertisements

Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Data types and variables
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
JavaScript, Third Edition
CS0007: Introduction to Computer Programming Introduction to Arrays.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Objectives You should be able to describe: Data Types
CS231 Fundamentals1 Fundamentals What kind of data do computers work with? – Deep down inside, it’s all 1s and 0s What can you do with 1s and 0s? – Boolean.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
June 10, 2002© Howard Huang1 Number systems To get started, we’ll discuss one of the fundamental concepts underlying digital computer design:
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CS231: Computer Architecture I Laxmikant Kale Fall 2002.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
EGR 2261 Unit 11 Pointers and Dynamic Variables
User-Written Functions
The Machine Model Memory
Chapter 4 C Program Control Part I
Week 3 - Friday CS222.
© 2016 Pearson Education, Ltd. All rights reserved.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Revision Lecture
Multiple variables can be created in one declaration
Variables and Primative Types
Binary Representation
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
CS 240 – Lecture 11 Pseudocode.
Circular Buffers, Linked Lists
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Arrays, For loop While loop Do while loop
Introduction to Programming
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
Lecture 18 Arrays and Pointer Arithmetic
Coding Concepts (Basics)
BIT115: Introduction to Programming
Lecture 8 Data structures
Bits and Bytes Topics Representing information as bits
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Binary Representation
Your questions from last session
Homework Starting K&R Chapter 5 Good tutorial on pointers
Homework Reading Programming Assignments Finish K&R Chapter 1
Comp Org & Assembly Lang
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Introduction to Primitives
Introduction to Primitives
Java Programming Language
The C Language: Intro.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Bases and Representations, Memory, Pointers, Arrays, For-Loops CS 240 – Lecture 3 Bases and Representations, Memory, Pointers, Arrays, For-Loops

Number Systems – Decimal The Decimal system is the one we've grown up with, where every number is a sequence of digits of which there are only 10 values. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Any number can be decomposed as a weighted sum of powers of 10. 2(10)3 + 0(10)2 + 1(10)1 + 8(10)0 = 2018 This is a unique representation when only digits 0 through 9 are allowed. We can switch to another number system simply by changing the digits and the base we're working with. All of the arithmetic we already know for the decimal system will apply to the new bases as well.

Number Systems – Octal Octal is the base 8 number system. 0, 1, 2, 3, 4, 5, 6, 7 1(8)2 + 2(8)1 + 3(8)0 = 1238 = 83 For systems other than base 10, a subscript next to the number indicates the base of the number system. The benefits of the octal number system come from it's closeness with the binary number system as seen below. 0002 = 08, 0012 = 18, 0102 = 28, 0112 = 38, 1002 = 48, 1012 = 58, 1102 = 68, 1112 = 78. To provide an int value in octal in C, precede the digits with 0. Example: 0123

Number Systems – Hexadecimal Octal is the base 16 number system. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f 7(16)1 + f(16)0 = 7f16 = 247 For systems with a base greater than 10, the letters of the alphabet are used for higher digits. In arithmetic using the letters, treat those digits as their position in the ordering. a = 10, b = 11, c = 12, d = 13, e = 14, f = 15 This system is generally preferred over octal as it takes exactly 2 hex digits to represent a single byte of data. To provide an int value in hex in C, precede the digits with 0x. Example: 0x12ab007f

Number Systems – Binary Binary is the base 2 number system. 0, 1 1(2)7 + 0(2)6 + 1(2)5 + 0(2)4 + 1(2)3 + 1(2)2 + 0(2)1 + 0(2)0 = 101011002 = 172 It's often better to operate in other number systems for arithmetic purposes. The binary number system lends itself better to logical operations on numbers which we will discuss later on in the course. To provide an int value in binary in C, precede the digits with 0b. Example: 0b00110011

Number Systems – Intrinsic Value It's important to note that base number systems are just representations of the intrinsic value of a number. 1510 = f16 They are the same number! When considering which representation to use, there is no operational different. You decide on a representation based on practicality of interpretation. Hex is a good representation for memory addresses and arbitrary byte data Octal is a great for logical bit groups of three, like file permissions. Binary is useful for bit fields and in-depth bit manipulation required by some applications.

Memory – An Abstract View void main() { int a = 2; int b = 5; ... } Here, we simply consider variables to be labeled boxes which contain values. At a certain point in the execution, a variable that has been declared will have a value. Fails to take into account numeric addresses of memory locations. int a 2 int b 5 Useful for solving problems in your head or on paper.

Memory – A Realistic View void main() { char a = 0x00; int b = 0x12345678; Address Value Variable 0x82ff6f80 0x00 a 0x82ff6f81 0x78 b 0x82ff6f82 0x56 0x82ff6f83 0x34 0x82ff6f84 0x12 0x82ff6f85 *junk* 0x82ff6f86 Memory is organized byte-by-byte in a long line. Variables are associated with one address, even if they span more than one.

Memory – A Realistic View void main() { char a = 0x00; int b = 0x12345678; Address Value Variable 0x82ff6f80 0x00 a 0x82ff6f81 0x78 b 0x82ff6f82 0x56 0x82ff6f83 0x34 0x82ff6f84 0x12 0x82ff6f85 *junk* 0x82ff6f86 Also, note that the bytes of b are in reverse order. This is called Little Endian and is characteristic of Intel x86 and AMD64 Processors.

Memory – A Realistic View void main() { char a = 0x00; int b; Address Value Variable 0x82ff6f80 0x00 a 0x82ff6f81 *junk* b 0x82ff6f82 0x82ff6f83 0x82ff6f84 0x82ff6f85 0x82ff6f86 Memory locations are ever-present and are reused constantly. Newly-declared variables in a function will contain junk data until assigned a value.

Memory Addresses as Values Often times, it's useful to use the address of a variable programmatically. This is something that isn't easy or even possible in many other programming languages. Computer hardware generally communicate with programs through addresses, indicating where data should be written to memory and where it should be read from. This bypasses the use of variable names entirely. In theory, you could write an entire program without variables and still have it make use of memory.

Memory Addresses as Values – Pointers Pointers are a type of variable which are specifically designed to hold memory addresses. Addresses on 32-bit systems are 4 bytes, while on 64-bit systems they're 8 bytes! Declaring a pointer variable requires three things: a type, an asterisk (*), and a name; type * name; int * iaddr; You can consider it to be a usual declaration statement by taking the type and asterisk together to be its own type. int* iaddr; char *cptr; The asterisk can be to either side of the space, but convention favors the right.

Pointers – Accessing Variable Addresses To get the address of a variable in C, we use the & operator. int sum; int *iaddr; iaddr = ∑ The & operator can be read as "address of". So, the above example the value to the right would be the "address of" sum. The expression will produce an address of the specific type corresponding to the variable. int variables give int* type addresses, char variables give char*, etc.

Pointers – Accessing Values at Addresses To get the value at an address in C, we use the * operator. int sum; int *iaddr = 0x82ff6f81; sum = *iaddr; Don't get confused! The *iaddr in line 2 has different meaning from the *iaddr in line 3. The * operator can be read as "value at" The "value at" iaddr. The type of the pointer indicates what type of value to return. char* gives 1 byte starting from the address. int* gives 4 bytes starting from the address.

Arrays – One variable, Many values Arrays are a primitive collection structure in C which houses multiple variables under one name. Each sub-variable is called an element of the array. An array is declared like a variable, and so it's declaration has three specific parts: a type, a name, and a number of elements. type name[number]; char line[1000]; In the above example, line is an array of 1000 characters. In the C99 standard, the number of elements is allowed to be variable. In older standards, the number of elements must be constant or a literal value.

Arrays – In Memory char buffer[6]; buffer[0] = 'b'; buffer[1] = 'u'; … Address Value Variable 0x82ff6f80 'b' buffer[0] 0x82ff6f81 'u' buffer[1] 0x82ff6f82 'f' buffer[2] 0x82ff6f83 buffer[3] 0x82ff6f84 'e' buffer[4] 0x82ff6f85 'r' buffer[5] 0x82ff6f86 *junk* Accessing elements of the array is done with the bracket operator ([]). The index of the element you want accessed goes inside.

Arrays – Essentially a Pointer Array variables are essentially a pointer variable with one difference: the memory allocation of elements. Pointers allocate space for an address. Arrays allocate space for elements. Array variables when used without the [] operator will instead be treated as the address of the first element of the array. Pointer variables can be used with the [] operator to treat an address as the 0th element of an array. char *chaddr = 0x1248feed; chadder[4] = '\n'; The above example modifies the address that is 4 character-widths away from 0x1234feed (which is 0x1234fef1).

[] operator on Pointers – Same as Array! char *cptr; cptr = 0x82ff6f80; cptr[1] = 'c'; This allows us to reference other locations in memory based on addresses without variable names. Note that attempting to access memory that your program did not allocate is likely to result in error. Address Value Variable 0x82ff6f80 *junk* 0x82ff6f81 'c' 0x82ff6f82 0x82ff6f83 0x82ff6f84 0x82ff6f85 0x82ff6f86

Control Flow – for-statement Going back to a topic we skipped over, now is a good time to introduce the for-statement. Like while-statements, for-statements are looping control flow compound statements used for repeated execution of statements. However, they include additional parts which make them a more powerful looping statement. for (init; cond; inc) for(i=0; i<4; i++) Within the for-statement's parentheses, there are three parts: the initialization, the condition, and the "increment". The "increment" doesn't always increment.

Control Flow – for-statement for (i = 0; i < N; i++) { i = 0; doSomething(); while (i < N) { } doSomething(); i++; } The for-statement isn't really all that different. Above are two equivalent snippets of code. Any time you can use a for-loop, you can use a while-loop instead. Exercise: Write an equivalent for-loop for the while-loop on Slide 17 of lecture 2.