Engineering Science 1036A/B Lecture Notes - Lecture 21: Compile Time

24 views6 pages
Data Storage and Memory Diagram
Four sections of the memory areas store four different types data:
Stack
Stores Local variables and formal parameters
§
Managed by the compiler
§
Heap
Stores dynamic variables
§
Managed by storage allocator
§
§
Global Data
Stores global variables
§
Program code
Stores other program data
§
Dynamic Memory Allocation
Dynamically allocated memory is determined at runtime
Not during compile time, when the compiler is building the project
It allows a program to create as many or as few variables as required, offering greater
flexibility
Dynamic allocation is often used to support data structures such as stacks, queues, linked
lists and binary trees
application of pointers
Dynamic memory is finite
Dynamically allocated memory may be freed during execution
Operator: new
Creates a new dynamic variable/object of the specified type
Returns the address of a memory location of the new variable and assigns it to a pointer
int *ptr = new int;
If allocation fails, new terminates the program
or returns NULL on some compilers
int *iPtr = new int;
4 bytes are allocated in the heap section of the memory
iPtr gets the address of that allocated space
int size = 0
cout << “Input array size: “; cin>>size;
user enters 20
double *dPtr = new double[size];
size does not have to be a constant
An array of 20 double spaces (160 bytes) are allocated in the heap and the address
of the first location is assigned to the dptr
The name of the pointer becomes the array identifier (array name)
dPtr = new double[50];
dPtr now points to a new array of 50 elements
Previous array is abandoned
Always check after each request.
Initializing Dynamically Allocated Memory
To initialize a dynamically allocated variable, specify the initial value inside round bracket
after the data-type
new variable pointed to by ptr has a value of 100
§
Operator: delete
The delete operator frees up the memory produced by new
int *ptr = new int; delete ptr;
While delete uses a pointer variable, it does not destroy the pointer variable, it only frees
up the storage to which the variable is pointing
Deleting dynamically allocated array:
int *ptr=new int[5]; delete [] ptr;
After releasing the dynamic memory, always assign NULL to the pointer
int *ptr=new int; delete ptr;
ptr = NULL;
Example
Dynamic Memory Locations
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
Data Storage and Memory Diagram
Four sections of the memory areas store four different types data:
Stack
Stores Local variables and formal parameters
Managed by the compiler
Heap
Stores dynamic variables
Managed by storage allocator
Global Data
Stores global variables
Program code
Stores other program data
Dynamic Memory Allocation
Dynamically allocated memory is determined at runtime
Not during compile time, when the compiler is building the project
It allows a program to create as many or as few variables as required, offering greater
flexibility
Dynamic allocation is often used to support data structures such as stacks, queues, linked
lists and binary trees
application of pointers
Dynamic memory is finite
Dynamically allocated memory may be freed during execution
Operator: new
Creates a new dynamic variable/object of the specified type
Returns the address of a memory location of the new variable and assigns it to a pointer
int *ptr = new int;
If allocation fails, new terminates the program
or returns NULL on some compilers
int *iPtr = new int;
4 bytes are allocated in the heap section of the memory
iPtr gets the address of that allocated space
int size = 0
cout << “Input array size: “; cin>>size;
user enters 20
double *dPtr = new double[size];
size does not have to be a constant
An array of 20 double spaces (160 bytes) are allocated in the heap and the address
of the first location is assigned to the dptr
The name of the pointer becomes the array identifier (array name)
dPtr = new double[50];
dPtr now points to a new array of 50 elements
Previous array is abandoned
Always check after each request.
Initializing Dynamically Allocated Memory
To initialize a dynamically allocated variable, specify the initial value inside round bracket
after the data-type
new variable pointed to by ptr has a value of 100
§
Operator: delete
The delete operator frees up the memory produced by new
int *ptr = new int; delete ptr;
While delete uses a pointer variable, it does not destroy the pointer variable, it only frees
up the storage to which the variable is pointing
Deleting dynamically allocated array:
int *ptr=new int[5]; delete [] ptr;
After releasing the dynamic memory, always assign NULL to the pointer
int *ptr=new int; delete ptr;
ptr = NULL;
Example
Dynamic Memory Locations
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Four sections of the memory areas store four different types data: Not during compile time, when the compiler is building the project. It allows a program to create as many or as few variables as required, offering greater flexibility. Dynamic allocation is often used to support data structures such as stacks, queues, linked lists and binary trees application of pointers. Dynamically allocated memory may be freed during execution. Creates a new dynamic variable/object of the specified type. Returns the address of a memory location of the new variable and assigns it to a pointer int *ptr = new int; If allocation fails, new terminates the program or returns null on some compilers nked ter or returns null on some compilers int *iptr = new int; An array of 20 double spaces (160 bytes) are allocated in the heap and the address of the first location is assigned to the dptr.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents