Book contents
- Frontmatter
- Contents
- Chapter 1 Introduction
- Chapter 2 Octave programming
- Chapter 3 Installing and running the Dev-C++ programming environment
- Chapter 4 Introduction to computer and software architecture
- Chapter 5 Fundamental concepts
- Chapter 6 Procedural programming basics
- Chapter 7 An introduction to object-oriented analysis
- Chapter 8 C++ object-oriented programming syntax
- Chapter 9 Arrays and matrices
- Chapter 10 Input and output streams
- Chapter 11 References
- Chapter 12 Pointers and dynamic memory allocation
- Chapter 13 Memory management
- Chapter 14 The static keyword, multiple and virtual inheritance, templates and the STL
- Chapter 15 Creating a Java development environment
- Chapter 16 Basic Java programming constructs
- Chapter 17 Java classes and objects
- Chapter 18 Advanced Java features
- Chapter 19 Introductory numerical analysis
- Chapter 20 Linear algebra
- Chapter 21 Fourier transforms
- Chapter 22 Differential equations
- Chapter 23 Monte Carlo methods
- Chapter 24 Partial differential equations
- Index
Chapter 12 - Pointers and dynamic memory allocation
Published online by Cambridge University Press: 05 June 2012
- Frontmatter
- Contents
- Chapter 1 Introduction
- Chapter 2 Octave programming
- Chapter 3 Installing and running the Dev-C++ programming environment
- Chapter 4 Introduction to computer and software architecture
- Chapter 5 Fundamental concepts
- Chapter 6 Procedural programming basics
- Chapter 7 An introduction to object-oriented analysis
- Chapter 8 C++ object-oriented programming syntax
- Chapter 9 Arrays and matrices
- Chapter 10 Input and output streams
- Chapter 11 References
- Chapter 12 Pointers and dynamic memory allocation
- Chapter 13 Memory management
- Chapter 14 The static keyword, multiple and virtual inheritance, templates and the STL
- Chapter 15 Creating a Java development environment
- Chapter 16 Basic Java programming constructs
- Chapter 17 Java classes and objects
- Chapter 18 Advanced Java features
- Chapter 19 Introductory numerical analysis
- Chapter 20 Linear algebra
- Chapter 21 Fourier transforms
- Chapter 22 Differential equations
- Chapter 23 Monte Carlo methods
- Chapter 24 Partial differential equations
- Index
Summary
A pointer variable like a reference or array variable stores a memory address; however, this address can be arbitrarily changed, enabling the contents of any accessible memory location to be addressed and manipulated directly. While enabling access to all available resources, new and subtle types of errors arise. For example, when a program requests additional memory during runtime from the operating system, the address of the starting location to the new, dynamically allocated variable is returned. Since the value stored in a preexisting compiler-allocated pointer variable can be altered, the running program can preserve the address passed back by the operating system. However, if the pointer variable was defined within an inner block, it will be destroyed when the block terminates. The location of the dynamically allocated memory is then lost and the memory cannot subsequently be accessed or later freed.
Introduction to pointers
A definition in C++ establishes the amount of memory space required for a variable and the interpretation of the value stored at this memory location. The value of a pointer variable is associated with the starting memory address of a variable of a specified type, i.e. the value of a pointer is a memory address. That is, a double pointer that stores a value such as 80000 interprets the 8-byte region from physical memory location 80000 to location 80007 as the storage location of a double variable. The amount of memory space reserved by any pointer variable equals the number of bits required to store a hardware memory address – on a 32-bit machine, an address requires 4 bytes so that applying the sizeof( )function to any pointer yields 4.
- Type
- Chapter
- Information
- A Short Course in Computational Science and EngineeringC++, Java and Octave Numerical Programming with Free Software Tools, pp. 106 - 122Publisher: Cambridge University PressPrint publication year: 2012