C++ tutorial : Pointers and Dynamic Arrays

C++ tutorial : Pointers and Dynamic Arrays





C++ tutorial : Pointers and Dynamic Arrays


















 Introduction
The topic of this chapter is the notion of pointer and some of the uses of pointers.

Benjamin Whorf said that the language one uses has a great effect on how you think, even to the extent of determining what you can think.  Few natural language experts think the human language a person uses has quite that control over thoughts. Programming languages on the other hand, have a quite strong influence on what we can code. Some programming languages do not have the idea of an address in the language, making some kinds of programming very difficult.[1] Most modern programming languages support the memory address abstraction we call pointer.

The notion of pointer gives us the abstraction of memory addresses in C and C++ programming. The main use of pointers is to construct and use linked structures and dynamically allocated arrays. (We will study linked structures in Chapter 17.)  The C and C++ pointer construct gives almost full reign to our ability to allocate, structure, and deallocate memory.

A pointer variable holds a pointer value. Pointer variables are typed, like all objects in C++. Typed means the pointer variable’s pointer value can only point to an object of the type with which the pointer variable was declared. Following a common abuse of the language, we will use the word pointer indiscriminately to refer to either a pointer variable or a pointer value.
1. Outline of topics in the chapter
10.1 Pointers
Pointer Variables
Basic Memory Management
Dynamic Variables and Automatic Variables
Uses for Pointers

10.2 Dynamic Arrays
Array Variables and Pointer Variables
Creating and Using Dynamic Arrays
Example: A Function that Returns an Array
Pointer Arithmetic
Multidimensional Dynamic Arrays

10.3 Classes, Pointers, and Dynamic Arrays
The -> Operator
The this pointer
Overloading the Assignment Operator
Example: A class for Partially Filled Arrays
Destructors
Copy Constructors

2. General remarks on the chapter
From this point in the book, you will find it very difficult to use of any version of Windows 9x or Windows Me to run programs you develop. Avoiding errors while developing programs that use pointers is nearly impossible.  With these operating systems, almost any pointer error will either crash the system or worse, render the system unstable so that it crashes shortly.

Preemptively rebooting after a pointer error isn’t easy. My Windows 98 2nd edition system continues to claim I have a “program” it does not name that has to be ended before rebooting is possible, but the system make killing these “programs” difficult.
I strongly recommend that you obtain and use some version of the (free) Linux operating system or Windows NT, 2000 or XP. Some Linux distributions are Red Hat, Debian, Suse, and Mandrake. I use and recommend Debian because of the ease of upgrading from one release to the next. I have found Windows NT and 2000 stable enough for development of student programs. I have not used XP. However, I have been told by XP users that it is sufficiently stable for this use.

Most Linux distributions provide GCC, the Gnu Collections of Compilers, among which are g++, the GNU c++ compiler. This is free software. There are a few commercial compilers for Linux. (A few are inexpensive. These compete with free compilers, so it is reasonable that these should all be very good compilers.

For Windows, Borland’s C++ command line compiler may be down loaded free from Borland’s web site. There is no GUI development environment. Borland Builder uses this same compiler, and is very good. Microsoft’s VC++6.0 is bundled with the book.

At the time of this writing, to use the introductory version of VC++ 6.0 bundled with the text, you must to go to the Microsoft download web site[2] down load and install the level 5 patch. You can compile the following program to determine whether your version needs patching:
class A
{
public:
  friend A operator+(A&, A&);
private:
  int a;
};

A operator+(A& lhs, A& rhs)
{
  int x;
  x = lhs.a + rhs.a;
  return A();
}

int main()
{
  A u, v, w;
  u = v + w;
  return 0;
}

If your compiler requires the patch, the compiler will ignore the friend declaration. It complains about the access to private class data by the friend function operator+().

You must have a required Microsoft program, MDAC version 2.6 or later to successfully install the patch for all Visual Studio components. Version 2.5 will install the patches for VC++6.0.  I do not know how to determine what version of MDAC is on a system other than downloading the patch and trying to install it.

I do know nothing about Macintosh systems or development environments, so I refrain from commenting on them.

10.1 Pointers
The text states that a pointer is the address of a variable. A variable is the name of a memory location. A memory location has both an address and an extent.  An example is the array, which has an address, the address of the index 0 element, and extent, which is the number of elements times the size of the base type. The two examples the text gives for pointers that we have already used are passing an array to a function and passing any variable by reference.....













C++ tutorial : Pointers and Dynamic Arrays

0 commentaires: