Pointers, Dynamic Memory Allocation and Structures
E N D
Presentation Transcript
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 • What is a pointer? • How does one declare a pointer variable? • 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 … 1232 20.00 10
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 = 105.99; 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”);