Lecture 01c: C++ review Topics: static arrays array / pointer connection dynamic arrays (and pointers) 2D arrays.

Slides:



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

DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
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.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
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.
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.
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Data Structure & Algorithm Pointer & Class. 2 Pointer can be used to store the address of other variables with types of int, char, float, and double.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
System Programming Practical Session 7 C++ Memory Handling.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
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.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Exception Handling How to handle the runtime errors.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Learners Support Publications Operators.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
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.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Memory Management in Java Mr. Gerb Computer Science 4.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Introduction to Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dynamic Memory CSCE 121 J. Michael Moore.
Dynamic Memory Allocation Reference Variables
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Local Variables, Global Variables and Variable Scope
Dynamic Memory A whole heap of fun….
Destructor CSCE 121.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Lecture 01c: C++ review Topics: static arrays array / pointer connection dynamic arrays (and pointers) 2D arrays

Overview Arrays are a group of contiguous, homogeneous data. – We can get around both restrictions with [our own] data structures Two types: – statically allocated (the "easier" kind): we know at compile time how many elements will be in the array. – dynamically allocated: we don't Example: reading an image from disk => memory

PartI: Static arrays double enemyDistances[6]; // Use const int or macros? Enemy enemies[6]; // A user-defined class enemyDistances[0] = 15.7; enemyDistances[1] = enemyDistance[2] * 2; cout << enemyDistances[3] << endl;// output? cout << enemyDistances << endl; // output?

Scope We'll talk more about this after functions… Basically scope defines: – where a particular name is valid – when that name "dies" C/C++ use block scope

Scope, cont. All "normal" variables are "destroyed" when control leaves their scope. – This includes statically allocated arrays. int main() { int x; float sizes[4]; cin >> x; if (x > 10) { double x; // Note: "name-masking" // … } }

Part II: Arrays and pointers [Look at the memory diagram for last example]. Now add… double enemyDistances[6]; // Use const int or macros? double d; double * p; enemyDistances[0] = 15.7; p = &d;*p = 17.6; p = &enemyDistances[5];*p = 9.1; p = enemyDistances;*p = 3.3;// Error? cout << p << endl; cout << sizeof(double) << endl; cout << p + 2 << endl; *(p + 2) = 11.2; p[2] = 11.2;// Exactly the same!

Part III: Dynamic allocation Some differences – Memory allocation is from the heap; "normal" allocation (static) is from the stack. – The memory stays allocated until we free it No automatic cleanup when we leave the scope. OK…the compiler / OS probably clean up after the program ends, but… – It must be done through pointers. Note: there are two (slightly different) syntaxes: – array allocation – single allocation

Example fstream fp; int width, height; char * data = NULL; fp.open("my_img.jpg", ios::in | ios::binary); fp.seekg(8, ios_base::beg); // I'm fudging jpeg format... fp.read((char*)&width, 4); // Note the (C-style) cast fp.read((char*)&height, 4); fp.seekg(4, ios_base::cur); data = new char[width * height]; fp.read(data, width * height); // No cast needed fp.close(); //... Use data... // De-allocate data if (data) // Note: NULL = 0 = false delete [] data;

Dynamic allocation (of singular types) Note: you can allocate a single item on the heap – not super useful for "standard" types – Very useful for objects The syntax is a bit different double * single_double = NULL; single_double = new double;// No brackets *single_double = 9.3; cout << *single_double << endl; delete single_double;// No brackets

Part IV: 2D arrays A "normal" array (dynamic and static) are like a 1-dimensional array. You can also represent 2 (or higher) dimensional data Two options: – Create a "flat" array and convert row/col numbers to an offset. – Let the compiler generate these offsets for you.

Examples float static_arr[2][3] = {{4.0f, -6.3f, 2.1f}, {1.9f, 0.0f, -5.8f}}; for (i=0; i<2; i++) { for (j=0; j<3; j++) cout << static_arr[i][j] << " "; cout << endl; } Static array, declared, then initialized float static_arr2[2][3]; static_arr2[0][0] = 4.0f; static_arr2[0][1] = -6.3f; static_arr2[0][2] = 2.1f; static_arr2[1][0] = 1.9f; static_arr2[1][1] = 0.0f; static_arr2[1][2] = -5.8f;

Examples, cont. dynamically allocated [Show memory diagram] float ** dynamic_array; dynamic_array = new float*[2]; dynamic_array[0] = new float[3]; dynamic_array[1] = new float[3]; dynamic_array[0][0] = 4.0f; dynamic_array[0][1] = -6.3f; dynamic_array[0][2] = 2.1f; dynamic_array[1][0] = 1.9f; dynamic_array[1][1] = 0.0f; dynamic_array[1][2] = -5.8f; // Use the same way as the static array before delete [] dynamic_array[0]; delete [] dynamic_array[1]; delete [] dynamic_array;

Examples, cont. Dynamically allocated "flat" array float * dynamic_flat = NULL; int i; dynamic_flat = new float[2 * 3]; dynamic_flat[0 * 3 + 0] = 4.0f; dynamic_flat[0 * 3 + 1] = -6.3f; dynamic_flat[0 * 3 + 2] = 2.1f; dynamic_flat[1 * 3 + 0] = 1.9f; dynamic_flat[1 * 3 + 1] = 0.0f; dynamic_flat[1 * 3 + 2] = -5.8f; for (i=0; i < 3 * 2; i++) { cout << dyamic_flat[i]; if (i > 0 && i % 3 == 0)cout << endl; elsecout << " "; } delete [] dynamic_flat;