Title: Introduction to Abstract Data Types ADTs
1Introduction to Abstract Data Types (ADTs)
2Chapter Contents
- 2.1 A first look at ADTs and Implementations
- 2.2 C's Simple Data Types
- 2.3 Programmer-Defined Data Types
- 2.4 Pointers
3First Look at ADTs Implementations
- For a programming task we must identify
- The collection of data items
- Basic operations to be performed on them
- Taken together (data items operations)
- are called an Abstract Data Type (ADT)
- Implementation
- Storage structures for the data items
- Algorithms for the operations
4C Simple Data TypesIntegers
- Unsigned integers
- unsigned short int, unsigned int, unsigned long
int - Represented in 2, 4, or 8 bytes
- Signed integers
- Short int, int, long int
- represented in two's complement
5Two's Complement Representation
- For nonnegative n
- Use ordinary base-two representation with leading
(sign) bit 0 - For n lt 0
- Find w-bit base-2 representation of n
- Complement each bit.
- Add 1
6Two's Complement Representation
- Example 88
- 88 as a 16-bit base-two number0000000001011000
- Complement this bit string1111111110100111
- Add 11111111110101000
7Two's Complement Representation
- Works well for arithmetic computations
5 6 0000000000000101 1111111111111010
1111111111111111
8 Type Size Values unsigned short int 2
bytes 0 to 65,535 short int 2 bytes -32,768
to 32,767 unsigned long int 4 bytes 0 to
4,294,967,295 long int 4 bytes -2,147,483,648
to 2,147,483,647 int (16 bit) 2
bytes -32,768 to 32,767 int (32 bit) 4
bytes -2,147,483,648 to 2,147,483,647 uns
igned int (16 bit) 2 bytes 0 to
65,535 unsigned int (32 bit) 2 bytes 0 to
4,294,967,295 char 1 byte 256 character
values float 4 bytes 1.2e-38 to
3.4e38 double 8 bytes 2.2e-308 to 1.8e308
9Problems with Integer Representation
- Limited Capacity a finite number of bits
- An operation can produce a value that excess the
maximum number allowed.This is called
overflow . - None of these is a perfect representation of
(mathematical) integers - Can only store a finite (sub)range of them.
10C Simple Data TypesReal Data
- Types float and double in C
- Use single precision (IEEE Floating-Point)
- Store
- sign of mantissa in leftmost bit (0 , 1 )
- represent exponent in next 8 bits (real exponent
127) - bits b2b3 . . .b24 mantissa in rightmost 23
bits. - Need not store b1 (we know it's 1, Its
implied)
b1.b2b32k
11Real Data
- Example 22.625 10110.1012
- Floating point form 1.01101012 24
4127131
12Problems with Real Representation
- Exponent overflow and underflow (8 bit exponent)
- Round off error
- Most reals do not have terminating binary
representations. - Example
- 0.7 (0.10110011001100110011001100. . .)2
13Problems with Real Representation
- Round off error may be accumulated in a sequence
of operations. - Real-world example Gulf War Patriot missile
guidance affected by accumulated round off - Be careful in comparing reals
- with and !.
- Instead use comparison for closenessif (abs (x
12.34) lt 0.001)
14C Simple Data TypesCharacter Data
- 1 byte for ASCII
- 2 bytes for Unicode (java)or C wide character
type - Operations , lt, gt, , etc. Using numeric code
15C Simple Data TypesBoolean Data
- Values false, true
- Could be stored in bits, usually use a byte
- Operations ,
- In C
- bool type
- int (boolVal) evaluates to
- 0 if false
- 1 if true
16Programmer-Defined Data Types
- Typedefs
- Mechanism usable to create a new type
- Give new name to existing type
- typedef OldType NewType
- Example
- typedef double real
- Now either double or real can be used.
17Programmer-Defined Data Types
- Enumerations
- Mechanism for creating types whose literals are
identifiers - Each identifier associated with unique integer
- enum Color RED, ORANGE, YELLOW, GREEN,
- BLUE, INGIGO, VIOLET
18Programmer-Defined Data Types
- Also possible to specify explicit values to give
the enumeratorsenum NumberBase BINARY 2,
OCTAL 8,
DECIMAL 10, HEXADECIMAL 16
19Pointers
- Pointer Variables
- A pointer is a variable that holds a memory
address. - Declares a pointer variable that can store int
address - int x 5
- int iptr x same as int iptr
- iptr x
-
- Note program exampleFig 2.4 (textbook)
address-of-operator
20Memory Map
- When regular variables are declared
- Memory is allocated for a value of the specified
type - The variables name is associated with the
address of that memory location - That memory is initialized with values provided
in the declaration (if any)
27
int intVar 27
21Basic Pointer Operations
- Dereferencing with the indirection operator
- Pointer variable stores address of a location
- Accessing contents of that location requires
dereferencing operator - iptr 11
- Note program exampleFig 2.5 (textbook)
22More on pointers
- The indirection operator can be applied more
than once. - Example
- int ptr
- declare ptr to be a pointer to a memory
location that contains a pointer to another
memory location where an int can be stored
0xdfffff10
Ptr
0xdfff2100
Ptr
Ptr
23More on pointers
- If a pointer is uninitialized, a pointer can have
any random value. It could be pointing to
anything. That is dangerous. Initialize all of
your pointers for safety. - Null pointer is a special pointer whose value is
zero. Dereferencing a null pointer always causes
a segmentation fault, because null pointer does
not point to anything. - int ptr 0
- ptr 35 // error!
24Basic Pointer Operations
Known as aliasing problem
- Assignment
- Pointer variables can be assigned the values of
other pointer variables bound to same type
25Basic Pointer Operations
- After the assignment jPtr iPtr
0xbffffd20
i
j
0xbffffd24
iPtr
iPtr
jPtr
jPtr
26Basic Pointer Operations
- Comparison
- Relational operators used to compare two pointers
- Must be bound to same type
- Most common and !
- The null address may be compared with any pointer
variable
27Header filer
- A header file is used to define constants,
variables, structs, and functions (classes in
C) that may be common to several applications. - It provide an interface for multiple applications
that all access the same data and the same
methods to operate on that data. - It provide a small amount of documentation
explaining how to use the components declared in
the file. It makes thing's simpler and easy to
use for large, complex program
28No Header file
- include ltstdio.hgt
- int add (int,int) / function prototype for add
/ - void main()
-
- int x 8
- printf(Sum d\n",add(x, x))
-
- int add(int i, int j)
-
- return ij
-
29Use header file
- / File add.h /
- ifndef ADD_H
- define ADD_H
- int add(int, int)
- endif / ADD_H /
- / File add.c /
- include "add.h"
- int add(int i, int j)
-
- return i j
-
/ File test.c / include iostream include
"add.h" using namespace std int main()
int x 8 cout ltlt Sum is ltlt add(x, x))
ltlt endl
Compile gcc Wall test.c add.c o test.exe
30Conditional Preprocessor Directive
- It is possible for a header file to be included
multiple times in a single source file. This lead
to redeclaration errors during compilation. Use
conditional preprocessor directive to solve this. - ifndef MYHEADER
- define MYHEADER
- /contents of my_header.h /
- endif
31Dynamic Memory Allocation
- The new operation require memory from operating
system during program execution - Example int intPtr intPtr new int
- The new operation return the starting address of
the sizeof(int)bytes of memory
intPtr
32Static vs. Dynamic Memory Allocation
- Statically allocated data types
- Programmer knows how many pieces of data exist
when writing program - Compiler sets aside space statically at
compile-time - Actual memory is allocated as soon as the program
begins executing
33Static vs. Dynamic Memory Allocation
- Dynamically allocated data types
- Compiler does not set aside space
- Memory is allocated only during execution and
only when the program specifically requests for
memory to be allocated - Can be at the beginning, middle, or end of
execution
34Pointer Arguments
- Pointers can be passed as arguments to functions
- This is logically equivalent to reference
parameters
35Reference
- A reference is an alias of the target object.
- int x 5
- int xRef x
- Target object and its reference has same address
- Anything you do to the reference is really done
to the target object - Reference must be initialized when they are
declared
36Passing Function Arguments
- Arguments values passed to a function when it is
called - Parameters variables in the function heading
used to hold these values - Passing by value
- local copies of the arguments are made when
function calls, and they are thrown away when the
function returns - Passing by reference using a pointer or using a
reference - The address of the arguments is being passed,
the actual original variables is made directly
available to the function
37Passing by Value
- A value parameter is allocated different memory
locations than the corresponding arguments - The value of the corresponding arguments is
copied to a value parameter - Changing a value parameter does not change the
corresponding argument
38Passing by Reference
- A reference parameter is an alias for the
corresponding argument - A reference parameter is allocated the same
memory locations as the corresponding argument - Changing a reference parameter changes the
corresponding argument
39Const Reference Parameters
- Why we use const reference parameter?
- We do want to protect the argument
- But we do not want to copy all the arguments each
time when a function is called. It is inefficient - Compiler error occurs if the function tries to
change the value of a constant reference
parameter
40Example Shows
- Basic C data type
- Parameter Passing by value
- Parameter Passing by reference (using pointer)
- Parameter Passing by reference (using reference)
- Parameter Passing by reference but constant
41Reading Assignment