Pointers. Dynamic Variables. Table of Contents. Static Variables. Dynamic Variables. Example of Pointers. Pointers. Pointers

Similar documents
Object Oriented Software Design II

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

The C Programming Language course syllabus associate level

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Object Oriented Software Design II

C++FA 5.1 PRACTICE MID-TERM EXAM

Chapter One Introduction to Programming

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Sources: On the Web: Slides will be available on:

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Stacks. Linear data structures

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

C++ INTERVIEW QUESTIONS

1 Abstract Data Types Information Hiding

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Glossary of Object Oriented Terms

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Semantic Analysis: Types and Type Checking

Illustration 1: Diagram of program function and data flow

Chapter 2: Elements of Java

arrays C Programming Language - Arrays

Lecture Data Types and Types of a Language

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

Data Structures using OOP C++ Lecture 1

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

1 The Java Virtual Machine

Functions and Parameter Passing

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Comp151. Definitions & Declarations

C++ Programming Language

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Programming languages C

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

7.1 Our Current Model

C++ Language Tutorial

5 Arrays and Pointers

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

How To Write Portable Programs In C

Goals for This Lecture:

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Java from a C perspective. Plan

Chapter 7D The Java Virtual Machine

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Answers to Review Questions Chapter 7

Java Interview Questions and Answers

MISRA-C:2012 Standards Model Summary for C / C++

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

An Incomplete C++ Primer. University of Wyoming MA 5310

13 Classes & Objects with Constructors/Destructors

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

A Grammar for the C- Programming Language (Version S16) March 12, 2016

C Interview Questions

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Basics of C++ and object orientation in OpenFOAM

Tutorial on C Language Programming

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Chapter 13 Storage classes

Module 816. File Management in C. M. Campbell 1993 Deakin University

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Moving from CS 61A Scheme to CS 61B Java

Example 1/24. Example

A Memory Model for Static Analysis of C Programs

Common Beginner C++ Programming Mistakes

Passing 1D arrays to functions.

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Chapter 5 Functions. Introducing Functions

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Informatica e Sistemi in Tempo Reale

Lecture 22: C Programming 4 Embedded Systems

/* File: blkcopy.c. size_t n

Bonus ChapteR. Objective-C

Implementation Aspects of OO-Languages

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Adding Alignment Support to the C++ Programming Language / Wording

Conditions & Boolean Expressions

Applied Informatics C++ Coding Style Guide

COMPUTER SCIENCE 1999 (Delhi Board)

Transcription:

1 Dynamic Variables 2 Table of Contents Dynamic Variables Pointer Declaration Pointer References Pointer Manipulation Addressing: Direct & Indirect Record Arrays of Pointer Expressions Dynamic Srage Dynamic Memory Problems Reference Variables Static Variables Size is fixed throughout execution Size is known at compile time Space/ is allocated at compilation Dynamic Variables Created during execution "dynamic allocation" No space allocated at compilation time Size may vary Structures are created and destroyed during execution. Knowledge of structure size not needed Memory is not wasted by non-used allocated space. Srage is for addresses. Example of Assume: Houses represent data Addresses represent the locations of the houses. Notice: To get a house you must have an address. No houses can exist without addresses. An address can exist without a house (vacant lot / NULL )

Pointer Declaration 3 Pointer References 4 Pointer Type Simple type of variables for sring the addresses of other locations Pointer Variables Declarations The asterisk * character is used for variable s: int* float iptr; *fptr; iptr is a an integer fptr is a a real Given the : Declares be a variable, but is a simple integer variable. Equivalent : Declare all variables in separate statements. Pointer Type Definitions: int* int recommended form common common int*, ; ; ; typedef int *intptr; intptr ; strong strong type type (preferred) (preferred) Address Operar: & Unary operar that returns the hardware location address of it s operand. Given: Address Assignment: Dereference / Indirection Operar: * unary operar that returns the contents at the address contained in the variable. Pointer Output: Results: int* ; int* ; int numa, numb; numa = 1; numb = 2; = &numa; = &numb; cout << << * << endl; cout << << * << endl; 0xF4240 1 0x3B9ACA00 2

Pointer References 5 Pointer Manipulation NULL Pointer Pointer constant, address 0 Named constant in the <stddef.h> include header Represents the empty points nowhere, unique /address value Symbolic/graphic representations: Illegal: *NULL Pointer Diagrams Given (text/code representation) Graphic representation #include <stddef.h> void main() { int* = NULL; int* = NULL; int numa, numb; numa = 1; numb = 2; } Pointer Assignments Independent from initial code above. #1 = &numa; = &numb; 1 numa numb 2 numa 1 numb 2 #2 * = * - 1; = ; numa 1 numb 0 #3 = *; * = ;

Addressing: Direct & Indirect Direct Addressing normal variable access non- variables represent one-level of addressing non- variables are addresses locations containing data values. compilers sre variable information in a symbol table : symbol type address x int 0xF4240 iptr (int) 0xF4241 compilers replace non- variables with their addresses & fetch/sre operations during code generation. Indirect Addressing accessing a location s contents thru a variables represent two-levels of addressing variables are addresses locations containing addresses. compilers replace variables with their addresses & double fetch/sre operations during code generation. indirect indirect addressing addressing variable. variable. x = 28 ; iptr = &x ; MEMORY address contents 0xF4239??? 0xF4240 28 0xF4241 0xF4240 0xF4241??? 7 Record structures: Given: Member Access Field Access Examples: Errors: Arrow Operar Short-hand notation: const int f3size = 20; struct rectype { int field1; float field2; char field3[f3size]; }; typedef rectype *recptr; rectype recptr r1ptr = &rec1; cout << (*r1ptr).field1 << (*r1ptr).field2 << (*r1ptr).field3 ; cout << *r1ptr.field1 << *r1ptr.field2 << *r1ptr.field3 ; cout << r1ptr->field1 << r1ptr->field2 << r1ptr->field3 ; rec1(1, 3.1415, pi ); r1ptr; 8 parenthesis parenthesis due due operar operar precedence; precedence; without without compiler compiler attempts attempts fields. fields. -> -> is is an an ANSI ANSI C C member member selection selection operar. operar. Equivalent Equivalent : : (*).member (*).member

Arrays of 9 Pointer Expressions 10 Declarations: Given: Member Access Field Access Examples: Arrow Operar Short-hand notation: const int size = 20; struct rectype { int field1; float field2; char field3[size]; }; typedef rectype *recptr; rectype rec1(1, 3.1415, pi ); recptr rayptrs[size]; rayptrs[size-1] = &rec1; cout << (*rayptrs[size-1]).field1 << (*rayptrs[size-1]).field2 << (*rayptrs[size-1]).field3 ; cout << rayptrs[size-1]->field1 << rayptrs[size-1]->field2 << rayptrs[size-1]->field3 ; Arrays == Non-indexed Array variables are considered s in C Array names as s contain the address of the zero element (termed the base address of the array). Given: equivalent assignments Pointer Indexing allocation) allocation) All s can be indexed, (logically meaningful only if the references an array). Logical Expressions NULL tests: Equivalence Tests: const int size = 20; char name[size]; char *person ; person = name ; person = &name[0] ; person[0] = ; person[size-2] =. ; if (!person) //true if (person == NULL) if (person == name) //true if s reference //the same address Does Does not not create create a a copy, copy, (no (no preferred check types types must must be be identical identical

Dynamic Srage Heap (Free Sre, Free Memory) Area of reserved by the compiler for allocating & deallocating a program during execution. Operations: Allocation char* name; int* iptr ; // C++ C name = new char ; name = (char *) malloc(sizeof(char)); iptr = new int [20] ; iptr = (char *) malloc(20 * sizeof(char)); //initialization name = new char ( A ) ; dynamic array allocation Deallocation C++ function C new type allocation malloc(# bytes) delete deallocation free NULL is returned if the heap is empty. typecasts // C++ C delete name ; free(name); delete [] iptr ; are undefined after //delete [20] iptr ; deallocation. 11 Dynamic Memory Problems Given: Garbage Previously allocated that is inaccessible thru any program s or structures. * Aliases typedef int *intptr; intptr, ; = new int (); = NULL; Two or more s referencing the same location. = new int (); = ; before during after Dangling References that reference locations previously deallocated.? = new int (); = ; delete ; leaks leaks?? 12?

Reference Variables 13 Reference Variable Declarations The and & character is used for reference variable s: int& float iptr; &fptr1, &fptr2; Reference Reference variables variables are are aliases aliases for for variables. variables. Pointer Differences Reference variables do NOT use the address and operars (& *). Compiler s reference variables transparently. Reference variables are constant addresses, assignment can only occur as initialization or as parameter passing, reassignment is NOT allowed. Examples: Purpose char char& //char* chref = B ; //achar = B ; //*chptr = B ; achar = A ; chref = achar; chptr = &achar; Frees programmers from explicitly dereferencing accessing, (in the same way non variables do). Cleans up the syntax for standard C arguments and parameters.