Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers, Dynamic Memory Allocation and Structures.

Similar presentations


Presentation on theme: "Pointers, Dynamic Memory Allocation and Structures."— Presentation transcript:

1 Pointers, Dynamic Memory Allocation and Structures

2 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?!

3 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;

4 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

5 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!

6 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;

7 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…

8 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?

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

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

11 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; };

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

13 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 1232 20.00 10 …

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

15 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;

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

17 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; }

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

19 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?

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

21 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”); }

22 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); }

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

24 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 = 105.99; sell_item(items[10]);

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

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

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


Download ppt "Pointers, Dynamic Memory Allocation and Structures."

Similar presentations


Ads by Google