L20dynamicmemory

L20dynamicmemory - Pointers Revisited q q q q q q q q q q q...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Pointers Revisited What is variable address, name, value? What is a pointer? How is a pointer declared? What is address-of and dereference operators? How can a pointer be assigned a value? How can a value of a memory location referred to by a pointer be accessed? What is a constant pointer? What is a pointer to a constant? What is the relationship between array name and a pointer? What is null pointer? What is lose pointer problem? Can pointers be used with objects? How can a method be invoked on an object using pointer? What is -> operator?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dynamic Memory Allocation
Background image of page 2
Why Dynamic Memory when a variable is declared - a certain memory area (sufficient to hold the variable of this type or class) is allocated not all memory can be allocated or efficiently allocated at the compilation time we are manipulating an set of data of arbitrary size what if we allocate an array too small? what if we allocate an array too large? static memory allocation - memory allocation at compile time dynamic memory allocation - allocation at execution time dynamic memory allocation is more flexible since the program can claim and release memory as needed and potentially can get as much memory as the computer resources allow heap - the system structure where the memory is allocated heap is separate for every program and it is removed when the program finishes
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
new and delete new and delete - operations are used to dynamically manipulate memory new - allocates a nameless variable of specified type (or class) and returns a pointer to it int *ip; // declare pointer ip = new int; // ip points to integer var note that the variable has no name and the only way to access the variable is though pointer: cin >> *ip; *ip += 20; cout << *ip; new may take a parameter to initialize the variable with ip = new int(5); // 5 assigned to the var delete - releases memory back to heap so that it can be reused delete ip; note - the memory pointed to by ip goes away not the pointer variable itself dynamic variable - variable created with new automatic variable – local variables, parameters, temp. variables
Background image of page 4
Memory Leak Problem note that the pointer that points to the dynamic variable is the only way to access this variable if the pointer is reassigned the dynamic variable is “lost”. This is called a memory leak problem int *ptr = new int; ptr = new int; // ERROR - memory leak
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/22/2010 for the course CS 23021 taught by Professor Staff during the Spring '08 term at Kent State.

Page1 / 19

L20dynamicmemory - Pointers Revisited q q q q q q q q q q q...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online