Pointers, Dynamic Memory Allocation and Structures.

Slides:



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

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers and Dynamic Memory Allocation. Dynamic Data Suppose we write a program to keep track of a list of students How many student records should we.
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.
Structures.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Lesson 9 Pointers CS 1 Lesson 9 -- John Cole1. Pointers in Wonderland The name of the song is called ‘Haddock’s Eyes’.” “Oh, that’s the name of the song,
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.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
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:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
1 Writing a Good Program 8. Elementary Data Structure.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
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.
Object-Oriented Programming in C++
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
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.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 9: Pointers.
Linked lists.
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers and References
Dynamic Memory Allocation
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory.
The Stack.
Linked lists.
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointers, Dynamic Memory Allocation and Structures

Dynamic Data Suppose we write a program to keep track of a list of students How many student records should we create? What if we create too few? What if we create too many? Wouldn’t it be nice to create just as many as we need?!

Pointer Review 1.What is a pointer? 2.How does one declare a pointer variable? 3.When using indirection, what steps are followed to get the specified value *ptr in the ex. below? ie) int *ptr, x, y = 5; ptr = &y; x = *ptr;

Dynamic Memory Allocation Stack: Area where function data is allocated and reclaimed as program executed Heap: Area C++ sets aside assuming that the programmer will ask for more memory as program executes

Dynamic Memory Allocation type *var = new type –allocate a chunk of memory large enough to store something of type type –new returns address of location of memory allocated delete(var) –free up the memory pointed to by var –otherwise -- memory leak!

Example string *s = new string(“hello”); cout << “The string is “ << *s << endl; cout << “The length is “ << (*s).length() << endl; *s += “ world”; cout << “Now the string is “ << *s << endl; delete s;

Common Errors Stale Pointers –string *s = new string(“hello”); –string *t = s; –delete s; //t is stale Double Delete –delete t; //already deleted? Dynamic memory allocation isn’t so interesting with primitive types…

Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items to keep track of? Would you want to keep track of 10, 20, 30 parallel arrays? Why not?

Structures Group many record items into one element Inventory: Item Name: Shirt Item Number: 1232 Cost: $20.00 Num in Inventory: 10

Structures struct inventory_item { string name; int number; double cost; int num_in_inventory; };

Structures struct new_type{ type name; … type name; } ; goes after #include and #define – not inside of main type can be another structure data type struct name{ string first; string last; } ; struct student{ name full_name; int id; };

Declaring and Initializing structure declaration does not allocate any memory inventory_item shirts;.name.number.cost.num_in_inventory …

Declaring and Initializing Dot member operator -- used to access an item in the structure shirts.number = 1232; shirts.cost = 20.00; shirts.num_in_inventory = 10; shirts.name = “shirts”;. name.number.cost.num_in_inventory …

Accessing Print “There are 10 of item: shirt left in the inventory.”

Accessing Print “There are 10 of item: shirt left in the inventory.” cout << “There are “ << shirts.num_in_inventory << “ of item: “ << shirts.name << “ left in the inventory.” << endl;

Structures and Functions Write a function to print all information about an inventory item

Structures and Functions Write a function to print all information about an inventory item void print_item(inventory_item item) { cout << “Item: “ << item.name << endl; cout << “Item Number: “ << item.number << endl; cout << “Cost: “ << item.cost << endl; cout << “Number Remaining: “ << item.num_in_inventory << endl; }

Structures and Functions Call the function inventory_item shirts; … print_item(shirts);

Structures as Output Parameters Write a function to “sell” an item by deducting 1 from the number of the item left in the inventory Return 1 if the sell was successful – 0 otherwise How would we call this function?

Structures as Output Parameters How would we call this function? inventory_item shirts; … sell_item(shirts);

Structures as Output Parameters How would we call this function? inventory_item shirts; int sell_ok; … sell_ok = sell_item(shirts); if(sell_ok) { printf(“Item sold\n”); } else { printf(“Problem encountered! Item not sold.\n”); }

Structures as Output Parameters int sell_item(inventory_item_t &to_sell) { int sell_ok; if((to_sell.num_in_inventory > 0) { to_sell.num_in_inventory = to_sell.num_in_inventory – 1; sell_ok = 1; } else { sell_ok = 0; } return (sell_ok); }

Arrays of Structures inventory_item items[20]; Access the name of item 5

Arrays of Structures inventory_item items[20]; Access the name of item 5 items[4].name; items[4].num_in_inventory = items[4].num_in_inventory – 1; items[5].cost = ; sell_item(items[10]);

Pointers to Structures inventory_item *item = new inventory_item; *item.name = “Hats”; //okay?

Pointers to Structures inventory_item *item = new inventory_item; *item.name = “Hats”; //okay? NO (*item).name = “Hats”; Alternative: -> operator item->name = “Hats”;

Pointers to Structs with Pointers struct Student{ string *name; int id; }; Student *stu = new Student; stu->name = new string(“Jane”);