Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

4.2Pointer Types

4.2.1 Introduction

The computer has a significantly large memory, ranging in the millions of bytes. At one time, it may have to deal with different applications of various types. Each application has different requests and assignments. To better manage this, a good deal of the job is handed to the compiler. For example, as you declare and initialize variables, the compiler has to keep track of what variable is where? How much memory space a variable needs? Importantly, when is a variable needed? And when is a variable not needed anymore? For example, we saw that, when you declare a value variable, the compiler reserves a space for it in an area reserved and called the stack memory. When such a variable is not used anymore, for example when the application gets closed, the compiler removes that variable from memory to make that memory available to other applications.

This also means that the compiler is in charge of managing the memory used by value variables. Of course, the compiler needs to keep track of the various variables used in your program. In fact, each variables has an address. To know the address of a variable, which will not mean much to you anyway, you can precede it with the & operator when accessing. This operation should be performed using the cout class. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

int Nbr;

cout << "Address of Number: " << &Nbr;

Console::WriteLine();

return 0;

}

This would produce:

Address of Number: 0012F3D0

4.2.2Pointer Declaration

In C++, instead of declaring a variable on the stack, the compiler reserves another area of memory called the heap. When declaring a variable, you can ask the compiler to "put" the variable in that area. To do this, precede the variable with an asterisk operator. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

Int *Number;

cout << "Address of Number: " << &Number;

Console::WriteLine();

return 0;

}

Such a variable is not a variable in the strict sense as that of a value. Instead, it is used to "point" to the memory location of another variable. For this reason, it is called a pointer to a variable or simply, a pointer.

Pointers allow you to manage computer memory and resources. When you declare a variable, the compiler reserves an amount of memory for that variable and waits for you to use that section of memory. If you declare a variable but do not use it throughout the life time of your program, that section of memory would have been busy, holding garbage, until your program ends. That section of memory would not be available to any other variable and you cannot prevent the compiler from creating it, as long as the unused variable exists. Therefore, another reason you would prefer pointers over regular variables is because pointers give you more control over the use of computer memory. Pointers allow you to decide when you need to use memory for a variable and when to free that memory space even if the program is still running.

Pointers allow you to create callback functions. These are functions whose name can be used as variables. This allows a function to be assigned as a value and to be passed as an argument. Something that is not possible with regular functions.

When declaring a pointer, the asterisk (*) lets the compiler know that the variable that follows is a pointer. There are three ways you can type the asterisk. These are

DataType* PointerName;

DataType * PointerName;

DataType *PointerName;

By default, it does not matter how you append the *, the compiler will know that the variable that follows is a pointer, as long as the word or group of words on the left side of the asterisk represents a valid data type.