february 11, 2005 more pointers dynamic memory allocation

16
February 11, 2005 More Pointers Dynamic Memory Allocation

Upload: cassandra-freeman

Post on 01-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: February 11, 2005 More Pointers Dynamic Memory Allocation

February 11, 2005

More PointersDynamic Memory Allocation

Page 2: February 11, 2005 More Pointers Dynamic Memory Allocation

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.

Page 3: February 11, 2005 More Pointers Dynamic Memory Allocation

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.

Page 4: February 11, 2005 More Pointers Dynamic Memory Allocation

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

Page 5: February 11, 2005 More Pointers Dynamic Memory Allocation

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)

Page 6: February 11, 2005 More Pointers Dynamic Memory Allocation

Syntax

To do this we need two new C++ operators

new and delete

Page 7: February 11, 2005 More Pointers Dynamic Memory Allocation

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;

Page 8: February 11, 2005 More Pointers Dynamic Memory Allocation

• 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)

Page 9: February 11, 2005 More Pointers Dynamic Memory Allocation

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.

Page 10: February 11, 2005 More Pointers Dynamic Memory Allocation

#include <iostream>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;}

Page 11: February 11, 2005 More Pointers Dynamic Memory Allocation

#include <iostream>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 memorycout << “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;}

Page 12: February 11, 2005 More Pointers Dynamic Memory Allocation

#include <iostream>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 memorycout << “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;}

Page 13: February 11, 2005 More Pointers Dynamic Memory Allocation

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.

Page 14: February 11, 2005 More Pointers Dynamic Memory Allocation

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

Page 15: February 11, 2005 More Pointers Dynamic Memory Allocation

ExampleWe 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.

Page 16: February 11, 2005 More Pointers Dynamic Memory Allocation

This Monday

Chapter 10 – Characters, Strings and the String Class