Presentation is loading. Please wait.

Presentation is loading. Please wait.

February 11, 2005 More Pointers Dynamic Memory Allocation.

Similar presentations


Presentation on theme: "February 11, 2005 More Pointers Dynamic Memory Allocation."— Presentation transcript:

1 February 11, 2005 More Pointers Dynamic Memory Allocation

2 Two types of memory Two main types of memory are the stack and the heap They can be physically next to each other or not. This is up to the compiler writer. Their differences are functional.

3 Stack The stack is for variables whose size and scope are known at compile time. Function parameters are ordinarily put on the stack and local variables defined inside of functions are defined on the stack. (unless they are static). Stack memory is automatically released when the scope is done because the size and scope of the memory were known at compile time. The ordinary way of declaring variables gets memory from the stack.

4 Heap The heap is for variables whose size and scope are not known until runtime. For variables defined on the heap, the compiler does not know the size and scope and therefore cannot release their memory You need to release their memory explicitly or else you will have ‘memory leaks’;

5 Creating Variables for Compile Time When we know how many variables of a certain type we need in a program, we can declare those variables in the source code to be compiled at compile time. If we do not know how many we need until runtime, then we need to use dynamic memory allocation (on the heap)

6 Syntax To do this we need two new C++ operators new and delete

7 Example int *iptr; iptr = new int; *iptr = 27; cout << “The address of the new variable is << iptr << endl; cout << “The value of the new variable is << *iptr << endl; Then to delete the memory, do this: delete iptr;

8 We can also do this with arrays. In this case, when you delete, you need to do this: delete [] arrayName; or C++ will only delete the memory in the first memory location. (A very common error)

9 Lets write a program that: 1.Asks the user for a positive integer. 2.Creates an array of that many integers. 3.Asks the user for integers to put in the array and fills it up one at a time.

10 #include using namespace std; int main() { int *numbers, howMany = 0; cout << “How many integers would you like to create?” << endl; cin >> howMany; numbers = new int[howMany]; cout << “Please enter your “ << howMany << “ integers and hit enter after each one.” << endl; for (int i=0; i<howMany; i++) { cin>>numbers[i]; } cout << “lets check that they are here. Hit enter “ << endl; char tmp; cin >> tmp; for (int i=0; i<howMany; i++) { cout << numbers[i] << endl; } return 0; }

11 #include using namespace std; int main() { int *numbers, howMany = 0; cout << “How many integers would you like to create?” << endl; cin >> howMany; numbers = new int[howMany]; You will need to delete this memory cout << “Please enter your “ << howMany << “ integers and hit enter after each one.” << endl; for (int i=0; i<howMany; i++) { cin>>numbers[i]; } cout << “lets check that they are here. Hit enter “ << endl; char tmp; cin >> tmp; for (int i=0; i<howMany; i++) { cout << numbers[i] << endl; } return 0; }

12 #include using namespace std; int main() { int *numbers, howMany = 0; cout << “How many integers would you like to create?” << endl; cin >> howMany; numbers = new int[howMany]; You will need to delete this memory cout << “Please enter your “ << howMany << “ integers and hit enter after each one.” << endl; for (int i=0; i<howMany; i++) { cin>>numbers[i]; } cout << “lets check that they are here. Hit enter “ << endl; char tmp; cin >> tmp; for (int i=0; i<howMany; i++) { cout << numbers[i] << endl; } delete [] numbers; return 0; }

13 Remember Memory that is allocated within a function statically, that is, not dynamically, is released automatically when the function returns. But memory that is allocated dynamically exists until delete is called or the program ends, whichever comes first. In particular, dynamically allocated memory continues to exist after the function it was created in (if it was created in a function) returns.

14 Example Consider this function that returns a pointer. What is wrong with it? char *getName() { char name[100]; cout << “Enter your name.” << endl; cin>>name; return name; }

15 Example We can do this: char *getName() { char *name; name = new char[100]; cout << “Enter your name.” << endl; cin>>name; return name; } You would then delete the memory after the above function returned.

16 This Monday Chapter 10 – Characters, Strings and the String Class


Download ppt "February 11, 2005 More Pointers Dynamic Memory Allocation."

Similar presentations


Ads by Google