Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Advertisements

Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Writing a Good Program 6. Pointers and Arrays
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Stack and Heap Memory Stack resident variables include:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
ITEC 320 C++ Examples.
Pointers OVERVIEW.
1 Writing a Good Program 8. Elementary Data Structure.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
System Programming Practical Session 7 C++ Memory Handling.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointers Declare a pointer Address of a variable Pointers and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers and Arrays Dynamic Variables and Arrays.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Overview 4 major memory segments Key differences from Java stack
Motivation and Overview
Command Line Arguments
Dynamic Memory Allocation
Overview 4 major memory segments Key differences from Java stack
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
Pointers and dynamic objects
Pointers and References
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false questions about arrays. Second part implement a stack and a queue using an array.

Today Argv and Argc String conversion The Heap The new and delete commands Dynamic Arrays Structs

Argc and Argv Programs often take arguments just like functions. For example in unix you might type: cp file1.cpp file2.cpp The program takes the arguments file1.cpp and file2.cpp and does something with those arguments. It copies the file with name file1.cpp to file2.cpp

Argc and Argv Programs can take an unlimited number of arguments all of type string. The arguments are passed into the program via argc and argv Argv is an array of strings (the argument values) Argc is a integer and is the number of argument the user gave your program

Argc and Argv #include using namespace std; int main( int argc, char* argv[] ) { cout << "Argument list:" << endl; for ( int i = 0; i < argc; i++ ) { cout << argv[i] << " " << endl; } return 0; } Input: program1 argument1 2 3 Output: Argument list: program1 argument1 2 3

Argc and Argv Since program arguments are always strings we often have to do some work to extract the value we want from an argument. There are a number functions for converting strings into other types. Eg. atoi( string ) converts the string into an integer.

Argc and Argv #include using namespace std; int main( int argc, char* argv[] ) { int x = 0, y = 0; x = atoi( argv[1] ); y = atoi( argv[2] ); cout << x*y << endl; return 0; } Input: program2 4 3 Output: 12

String conversion functions Other string conversion functions: long int atol( char* string ); double strtod( char* start, char* end); long int strtol( char* start, char* end); unsigned long int strtoul(char* start, char* end, int base ); Page 1026 of Deitel and Deitel.

The Stack Until now all the memory locations we have used for our data existed on the stack. Stack memory is fixed when the program is compiled. We can’t dynamically get more stack memory after the program starts. This is a problem.

The Heap If we don’t know how much space we will need before the program starts we are in trouble. … but we can ask for memory on the heap even after the program starts.

The Heap When we ask for memory from the heap we are responsible for managing that memory. Before, the memory we needed to store data was created for us when we declared a variable And destroyed for us when the end of that variables scope was reached. We have to manually create and destroy the memory we use on the heap.

The New and Delete commands We ask for memory on the heap with the new command (in old C it was malloc). We return memory to the heap with the delete command (in old C it was free). Syntax for new and delete: type* variable_name = new type; delete variable_name; or delete [] variable_name; // If deleting an array

New and Delete int main() { int* x = new int(0); // Initialize variable to 0 float* y = new float(6.0); char* z = new char('z'); cout << *x << " " << *y << " " << *z << endl; *x = 7; *y = 10.0; *z = 'k'; cout << *x << " " << *y << " " << *z << endl; delete x; delete y; delete z; return 0; } Output: 0 6 z 7 10 k

New and Delete – Memory Leaks If the program keeps allocating memory and doesn’t return it all, eventually all the memory in the system will be used up. For example: for( int i = 0; i < size; i++ ) // the memory reserved for x is returned to the { // stack when x goes out of scope “}” int x = 5; } for( int i = 0; i < size; i++ ) // The pointer goes out of scope but the memory { // it pointed too is still reserved int* x = new int(5); // But now we have lost its address, there is no } // way to find it again – it is orphaned memory If this occurs enough times the system will crash for lack of unallocated memory. During GWI the patriot missile systems had to be shutdown and restarted every few hours because their control system had a memory leak.

Dynamic Arrays int main() { int size = 0; cin >> size; int array[size] = {0}; for ( int i = 0; i < size; i++ ) { array[i] = i; cout << array[i] << “ “; } return 0; } Output: error C2057: expected constant expression

Dynamic Arrays int main() { int size = 0; cin >> size; int* array = new int[size]; for ( int i = 0; i < size; i++ ) { array[i] = i; cout << array[i] << “ “; } delete [] array; return 0; } Output:

Multidimensional Dynamic Arrays We can create multidimensional dynamic arrays too. They consist of dynamic arrays of pointers to other dynamic arrays. They are not quite like multidimensional static arrays since all the memory locations are not guaranteed to be contiguous. But they look the same from our point of view though they might be slightly slower under certain circumstances.

Multidimensional Dynamic Arrays int main() { const int d1size = 10, d2size = 10; int array[d1size][d2size]; for ( int i = 0; i < d1size; i++ ) { for ( int j = 0; j <d2size; j++ ) { array[i][j] = (i+1)*(j+1); cout << array[i][j] << " "; } cout << endl; } return 0; }

Multidimensional Dynamic Arrays int main() { int d1size = 0, d2size = 0; cin >> d1size >> d2size; int** array = new int*[d1size]; for ( int k = 0; k < d1size; k++ ) { array[ k ] = new int[d2size]; } for ( int i = 0; i < d1size; i++ ) { for ( int j = 0; j < d2size; j++ ) { array[i][j] = (i+1)*(j+1); cout << array[i][j] << " "; } cout << endl; } Input: 4 5 Output:

Multidimensional Dynamic Arrays Input: 4 5 Output: // Clean up for ( int k = 0; k < d1size; k++ ) { delete [] array[k]; } delete [] array; return 0; }