Download presentation
Presentation is loading. Please wait.
Published bySherman Smith Modified over 9 years ago
1
CSC 107 – Programming For Science
2
Today’s Goal Learn how arrays normally used in real programs Why a function returning an array causes bugs How to declare array if size varies with each run Using (& fixing problems) with new & delete All doing great, but pointers still hard
3
Arrays vs. Pointers Arrays = YamsPointers = Sweet Potatoes Makes space at declaration Variable value is address Use [] to access entries Can also access using * Can be assigned a pointer Needs target to be useful Variable value is address Use * to access target Can also access using [] Can be assigned array Often use pointers & arrays interchangeably
4
Use Like ArrayUse Like Pointer Arrays + Pointers = char array[100]; char * ptr = array; *array = ‘E’; *(ptr + 1) = ‘q’; *(ptr + 2) = ‘u’; *(array + 3) = ‘a’; *(ptr + 4) = ‘l’; *(array + 5) = ‘\0’; char array[100]; char * ptr = array; array[0] = ‘E’; ptr[1] = ‘q’; ptr[2] = ‘u’; array[3] = ‘a’; ptr[4] = ‘l’; array[5] = ‘\0’;
5
Pointers Can Be NULL
6
Starting a New Company Developing software for your new company Website selling engineering gear of all possible types Plans to grow site to offer consulting & other services Being as big as Amazon.com is your eventual goal But, for the moment, your company is very small
7
Developing the Software Want to write code that scales with company Read inventory from file to make updates easy By writing software in C++, can port to new machines No hard limits (if possible) since guesses will be wrong
8
First Attempt At Writing Code Use 2 arrays: one for names and one for costs Work in parallel, so item data at same index in arrays Number of items 1 st line of file to use declaring arrays Know that in the future may not want files Databases can be faster, but also more expensive Read from files using function so easy to change later Code is simple & easy Code is simple & easy; what could go wrong?
9
First Attempt At Writing Code Use 2 arrays: one for names and one for costs Work in parallel, so item data at same index in arrays Number of items 1 st line of file to use declaring arrays Know that in the future may not want files Databases can be faster, but also more expensive Read from files using function so easy to change later Code is simple & easy Code is simple & easy; what could go wrong?
10
First Attempt At Writing Code Code is simple & easy Code is simple & easy; what could go wrong?
11
First Try At Code double[] readCosts(int & nItems) { // Open the file ifstream fIn(“moolah.txt”); // Read in the number of items & create the array fIn >> nItems; double costs[nItems]; // Now read the cost of each item from the file for (int i = 0; i > costs[i]; } // And return the array… return costs; }
12
Compile In MS Visual C++... double[] readCosts(int & nItems) { // Open the file ifstream fIn(“moolah.txt”); // Read in the number of items & create the array fIn >> nItems; double costs[nItems]; // Now read the cost of each item from the file for (int i = 0; i > costs[i]; } // And return the array… return costs; }
13
Compile In MS Visual C++... double[] readCosts(int & nItems) { // Open the file ifstream fIn(“moolah.txt”); // Read in the number of items & create the array fIn >> nItems; double costs[nItems]; // Now read the cost of each item from the file for (int i = 0; i > costs[i]; } // And return the array… return costs; }
14
Move to Eclipse + Mac OS double[] readCosts(int & nItems) { // Open the file ifstream fIn(“moolah.txt”); // Read in the number of items & create the array fIn >> nItems; double costs[nItems]; // Now read the cost of each item from the file for (int i = 0; i > costs[i]; } // And return the array… return costs; }
15
Compiles on Eclipse + Mac OS… double[] readCosts(int & nItems) { // Open the file ifstream fIn(“moolah.txt”); // Read in the number of items & create the array fIn >> nItems; double costs[nItems]; // Now read the cost of each item from the file for (int i = 0; i > costs[i]; } // And return the array… return costs; }
16
…& Crashes on Eclipse + Mac OS Program behaves oddly without clear reasons During program value of variables just change Prices go up and down for no clear reason At some bizarre time, program eventually crashes
17
…& Crashes on Eclipse + Mac OS Program behaves oddly without clear reasons During program value of variables just change Prices go up and down for no clear reason At some bizarre time, program eventually crashes
18
How To Write Buggy Programs Unsafe to return Unsafe to return array declared in function Know how we draw boxes when we call function Cross out box at end of that call to the function boxes really exist Guess what? Those boxes really exist in memory Within box are declared arrays’ memory locations! Array uses the same address as other things Odd behavior results as overwrite each other’s values Memory shared with important data
19
How To Write Buggy Programs Unsafe to return Unsafe to return array declared in function Know how we draw boxes when we call function Cross out box at end of that call to the function boxes really exist Guess what? Those boxes really exist in memory Within box are declared arrays’ memory locations! Array uses the same address as other things Odd behavior results as overwrite each other’s values Memory shared with important data
20
What To Do? Do not want to have to declare array in main Lots of rewrites needed if we do file I/O in main Do not have money for huge array would need later Cannot declare in function without risk of crash How to make long-lived or variable-length arrays?
21
Dynamic Storage Allocation! Way of allocating arrays in own memory space No limits on space – array can be any (integer) size Type must match variable, but no new restrictions Memory reserved until explicitly returned to system Array entries initialized to 0 when array allocated Once it is allocated, use like normal array Cannot tell difference given two valid arrays Passing as parameter or returning from function okay
22
Dynamic Storage Allocation new new new new new int * ptr = new int[100]; char * cstring; double * ddd; int * arr; const int FOUR = 5; int val; cin >> val; cstring = new char[10]; ddd = new double [FOUR – 1 + 2]; arr = new int[val]; arr[2] = 23; long * pirate = new int[arr[2]];
23
All Done Now
24
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); Main Memory (RAM)
25
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); Main Memory (RAM) x
26
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); char *ptr = x; Main Memory (RAM) x
27
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); char *ptr = x; Main Memory (RAM) x ptr
28
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); char *ptr = x; delete [] x; Main Memory (RAM) x ptr
29
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); char *ptr = x; delete [] x; Main Memory (RAM) x ptr
30
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); char *ptr = x; delete [] x; // But I’m not dead yet! Main Memory (RAM) ptr
31
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); char *ptr = x; delete [] x; // But I’m not dead yet! char *y = new char[6]; strcpy(y, "sad"); cout << ptr << endl; // oops… Main Memory (RAM) y ptr
32
Dangling Pointers char *x = new char[6]; strcpy(x, "happy"); char *ptr = x; delete [] x; // But I’m not dead yet! char *y = new char[6]; strcpy(y, "sad"); cout << ptr << endl; // oops… Hard-to-find bugs created throughout code
34
Not Always A Laughing Matter
38
Your Turn Get into your groups and try this assignment
39
For Next Lecture Review of pointers in 12.1 - 12.9 What questions do you have about how they work? Are you ready to use & trace them in programs? Create and delete pointers during program running? Angel has Weekly Assignment #11 available
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.