Tema 6: Dynamic memory

Similar documents
Pointers, Dynamic Data, and Reference Types

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CSC 211 Intermediate Programming. Pointers

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Homework #3 CS2255 Fall 2012

The University of Nottingham

Dynamic Memory Allocation

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Stack memory - "scratch pad" memory that is used by automatic variables.

Array Elements as Function Parameters

[0569] p 0318 garbage

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Introduction to Computer Science Midterm 3 Fall, Points

Memory and Pointers written by Cathy Saxton

Algorithms & Data Structures

CS201- Introduction to Programming Current Quizzes

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Week 3: Pointers (Part 2)

CSC 211 Intermediate Programming. Arrays & Pointers

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Reference operator (&)

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

LAB 4.1 Relational Operators and the if Statement

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

CA31-1K DIS. Pointers. TA: You Lu

Exam 3 Chapters 7 & 9

Pointers. Memory. void foo() { }//return

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

CSCE Practice Midterm. Data Types

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

1 Memory management in C++

Computer Programming

C++ Structures Programming Workshop 2 (CSCI 1061U)

FORM 2 (Please put your name and form # on the scantron!!!!)

Separate Compilation Model

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

BITG 1113: POINTER LECTURE 12

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Chapter 3 Function Overloading

C:\Temp\Templates. Download This PDF From The Web Site

Propedéutico de Programación

CS 161 Exam II Winter 2018 FORM 1

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C Pointers. ENGG1002 Computer Programming and Applica;ons Dr. Hayden Kwok Hay So Week 2. GeGng Hold of Memory. Sta;c: When we define a variable

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

In Java we have the keyword null, which is the value of an uninitialized reference type

CSC 1300 Exam 4 Comprehensive-ish and Structs

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

a data type is Types

2 2

Tin học cơ sở 4$ Structures!

Dynamic Allocation of Memory

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Chapter 1: Object-Oriented Programming Using C++

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

1. Which of the following best describes the situation after Line 1 has been executed?

CS201 Some Important Definitions

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 3

Chapter 7 - Notes User-Defined Functions II

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Midterm Review. PIC 10B Spring 2018

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Binding and Variables

cout<< \n Enter values for a and b... ; cin>>a>>b;

6.096 Introduction to C++ January (IAP) 2009

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Homework Assignment #2 (revised)

Pointer Data Type and Pointer Variables

Exercise 1.1 Hello world

CS 376b Computer Vision

Chapter 2: Basic Elements of Java

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

CS 314 Principles of Programming Languages. Lecture 11

REFERENCES, POINTERS AND STRUCTS

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Exercise: Inventing Language

1. In C++, reserved words are the same as predefined identifiers. a. True

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lecture on pointers, references, and arrays and vectors

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Pointers. Variable Declaration. Chapter 10

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Pointers. 10/5/07 Pointers 1

Short Notes of CS201

Pointers, Arrays and Parameters

Introduction to C++ with content from

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Variation of Pointers

Programming in C++: Assignment Week 2

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Structured Data. CIS 15 : Spring 2007

Transcription:

Tema 6: Programming 2 and vectors defined with 2013-2014 and

Index and vectors defined with and 1 2 3 and vectors defined with and

and vectors defined with and Size is constant and known a-priori when the program is implemented Declaration of variables int i=0; char c; float vf[3]={1.0, 2.0, 3.0}; i c vf[0] vf[1] vf[2] 0 1.0 2.0 3.0 1000 1002 1004 1006 1008

and vectors can be used to store large amounts of data which maximum size is unknown when the program is implemented The amount of data to be stored is calculated during the program execution and it can dynamically change can be managed in C++ using pointers defined with and

and vectors defined with and A pointer is a number (long integer) which represents a memory address (location) The type of data contained in the memory address must be specified when the pointer is declared are declared using the character * Examples: int *punteroaentero; char *punteroachar; int *VectorPunterosAEntero[tVECTOR]; double **punteroapunteroareal;

and vectors defined with and *x Content of the address pointed by x &x address of the variable x Example: int i=3; int *pi; pi=&i; // pi=memory address of i *pi = 11; // content of pi=11. Therefore, i = 11 i pi 11 1000 1000 1002 1004 1006 1008

and vectors defined with and : int *pi=&i; // pi stores the address of i The NULL pointer is that one which is not pointing to any variable int *pi=null; Important: pointers without any assigned address should always be set to NULL.

and vectors defined with and Exercise 1 Analyze the output of these code excerpts: int e1; int *p1, *p2; e1 = 7; p1 = &e1; p2 = p1; e1++; (*p2) += e1; cout << *p1; int a=7; int *p=&a; int **pp=&p; cout << **pp;

(1/2) and vectors defined with and can be used to allocate (with new) or deallocate (with delete) dynamic memory Example: double *pd; pd = new double; // memory allocation *pd = 4.75; cout << *pd << endl; // prints 4.75 delete pd; // memory deallocation pd 2000 4.75 1000 1002... 2000 2002 Important: Variables allocated with new are stored in a different memory region than local variables.

(2/2) and vectors defined with and Using new, the compiler allocates memory and returns the initial address of the space allocated If there is not enough free memory for the allocation, new returns NULL Always that dynamic memory is allocated using new, it must be deallocated with delete Warning: After delete, the pointer is not set to NULL by default A pointer can be reused; after deallocating its content, it can be used again to allocate new memory

and vectors (1/2) and vectors defined with and can also be used to create new vectors (arrays) and matrices The memory allocation size must be specified between square brackets To free all the allocated memory, it s necessary to use the square brackets again Example: int *pv; int n=10; pv=new int[n]; // memory allocation for n integers pv[0]=585; // the pointer is used as a vector delete [] pv; // free the alocated memory

and vectors (2/2) can be used for direct access to the components of vectors and matrices. Example: int v[tvector]; int *pv; pv = &(v[7]); *pv = 117; // v[7] = 117; and vectors defined with and

defined with and vectors defined with and New data types can be defined using : int entero; // entero is a type, like int entero a,b; // int a,b; To obtain a clear code, pointers are usually defined with : int *punteroaentero; struct { char c; int i; } Registro, *PunteroRegistro;

and and vectors defined with and The operator -> can be used for accessing the fields of a structure which is being pointed struct { char c; int i; } Registro, *PunteroRegistro; PunteroRegistro pr; pr = new Registro; pr->c = a ; // (*pr).c = a ;

(1/2) and vectors defined with and Passing pointers to functions void f (int *p) { *p=2; } // By value void f2 (int *&p) { // By reference int num=10; p=&num; } int main() { int i=0; int *p=&i; f(p); f2(p); } // Function call

(2/2) Passing pointers to functions using and vectors defined with and int* PInt; void f (PInt p){ *p=2; } // By value void f2 (PInt &p) { // By reference int num=10; p=&num; } int main() { int i=0; PInt p=&i; f(p); f2(p); } // Function call

and vectors defined with and Using a pointer without allocated memory or pointing nowhere int *pentero; *pentero = 7; // Error!!! Use a pointer after being deallocated punteroregistro p,q; p = new REGISTRO;... q = p; delete p; q->num =7; // Error!!! Free deallocated memory int *p=&i; delete p;

and vectors defined with and Exercise 2 Given the following structure: struct { char nombre[32]; int edad; } Cliente; Make a program for reading a client (only one) from a binary file. The program must allocate the structure in dynamic memory using a pointer, print its content and finally free the memory.