C++. Pointers. Part 1. General concepts. Pointer types. Managed and unmanaged pointers. Pointers to a function. Examples of the use of pointers




Pointers. Part 1. General concepts. Pointer types. Managed and unmanaged pointers. Pointers to a function. Examples of the use of pointers


Content

Search other websites:

1. What is a pointer? Definition of a pointer. Ways to access a variable

C++ supports two ways to access a variable: a variable reference by its name and the use of a pointers mechanism.

One of the advantages of C++ in comparison with other programming languages is the use of pointers to access a variable. Also, as you know, you can access the variable by its name.

Using pointers, you can conveniently organize, for example, linked lists, dynamic allocation of memory, etc.

A pointer is a variable that contains the address of another variable in memory.

For example, if the variable a contains the address of b, then the variable a points to b.

2. What types of pointers are supported in C++?

In Visual C++ (CLR – Common Language Runtime) three types of pointers are supported:

3. What is the difference between managed and unmanaged pointers?

The difference between managed and unmanaged pointers is as follows.

Unlike managed pointer, to unmanaged pointer can be assigned to any memory address, even that which is outside the executive environment.

In this case, the memory is unregulated.

In the case of a managed memory, managed pointer can not be assigned an address outside the runtime environment.

4. What are referenced pointers or managed pointers?

Managed pointers are pointers of the reference type. These pointers are passed for arguments, methods, which are passed by reference. These pointers are compatible with the Common Language Runtime (CLR) specification. Managed pointers are references to objects. These objects are placed in the shared managed memory, which is allocated to the program at the time of its execution.

In these pointers, the symbol ‘^’ is used instead of the ‘*’ symbol. To allocate memory for a managed pointer, use the gcnew utility.






5. Example of using managed pointers. The gcnew utility and the ref keyword in Visual Studio C++

In managed pointers, the gcnew utility generates an instance of an object. The gcnew utility allocates memory for the object instance and returns a reference to this object.

The description of this object must begin with the keyword ref. The ref keyword provides information that the description is a reference type. The general form of using the ref keyword is:

ref description;

After this description, the memory for the object is allocated via gcnew utility.

Example. The example demonstrates:

  • a BOOK structure declaration of a reference type that contains information about the book;
  • use of the ref keyword to indicate that the structure is a reference type;
  • using the utility gcnew to allocate memory for the structure;
  • demonstration of the use of managed pointers to operate the objects.

Let the following structure be declared outside the class definition:

ref struct BOOK // ref - indicates that the structure is a reference type
{
    System::String  Title;  // Book title
    System::String  Author; // Name of the author of the book
    int year;                // the year of publishing
    float price;             // book cost
};

Then in the methods of this class, you can use the BOOK structure approximately as follows:

...

BOOK ^B; // variable B has a type of "managed pointer"

B = gcnew BOOK; // allocating memory for the structure

// filling the fields of the structure
B->Title = "Title of Book";
B->Author = "Author of Book";
B->year = 1998;
B->price = 399.85f;

...

6. What are unmanaged pointers and unmanaged function pointers?

Unmanaged pointers are conventional C/C++ pointers. They are pointers to objects in unmanaged memory size that is allocated for the program. Unmanaged pointers are not compatible with the CLR specification.

Unmanaged function pointers – are pointers to functions that can be processed in such the same way as and unmanaged pointers to objects (data).

Features of using unmanaged pointers are described in the next topic.

7. What is the general form of an unmanaged pointer declaration? Example

General form of the declaration of the pointer:

variable_type * pointer's_name;

where

  • variable_type – this is type of the variable to which the pointer points. It is also called the base type of pointer;
  • pointer’s_name – the name of the pointer variable.

Example. Definition of pointers that point to different types of variables.

int * pi;   // pointer with name pi to type int
float * pf; // pointer with name pf to type float

8. Unmanaged pointers. The operation of getting the address &. Example

To get the address of a variable in memory, use the operation of taking the address &.

This operation is a unary operation. It returns the address of its operand in memory.

An example of the operation of taking the address, and use the pointer to access the variable.

int * pi; // pointer with name pi to type int
float * pf; // pointer with name pf to type float
float f;
int i;

i = 8;
pi = &i; // pi points to i
*pi = 10; // i = 10

f = 2.93f;
pf = &f; // pf points to f
*pf = 8.85f; // f = 8.85

Figure 1 shows schematically the use of pointers for type int.

C++ pointer integer figure

Figure 1. Using a pointer to int

As you can see in Figure 1, with the help of the pointer you can access the variable and change its value. In this case, the type pointed to by the pointer must match the type of the variable.

9. An example of using an unmanaged function pointer.

Function pointers are allowed to organize a functions call in the form of a sequence, depending on the requirements. In this case, functions that perform different tasks should:

  • – have the same number and types of arguments;
  • – return value of the same type.

At the declaration, the pointer to the function is written as follows:

type (*function_name) (parameters_list);

where

  • type – the type of the value returned by the function;

Example. Using an unmanaged function pointer for the console application.

Definition and using a function pointer named func_ptr, which takes two integer parameters x, y and returns a double value.

Let there be given 2 functions that have the same number of parameters and return the same value:

// function that computes x to a power of y.
double Power(int x, int y)
{
    double res;
    res = System::Math::Pow((double)x, (double)y);
    return res;
}

// function that divides x by y
double Div(int x, int y)
{
    double res;
    res = (double)x / (double)y;
    return res;
}

In the console application, you can use this pointer as follows:

// define a pointer to a function that receives 2 parameters of type int
double (*func_ptr)(int x, int y);
double res;

...

// Call the exponentiation function
func_ptr = Power; //  points to Power - exponentiation
res = (*func_ptr)(4,3); // res = 64

// Calling the function of dividing numbers using a pointer

func_ptr = Div; // func_ptr points to Div

res = (*func_ptr)(4,3); // res = 1.33333333333333

This way of using pointers to a function is only suitable for console applications.

10. An example of using a managed function pointer in a C++/CLI environment

Using a function pointer in a C++/CLI environment for applications created using the Windows Forms template. In this case, you need to use the so-called delegates. Delegates are supported in the .NET Framework.

Let there be given 2 functions that have the same number of parameters and return the same value:

// Function that calculates x to a power of y
public: double Power(int x, int y)
{
    double res;
    res = System::Math::Pow((double)x, (double)y);
    return res;
}

// function that divides x by y
public: double Div(int x, int y)
{
    double res;
    res = (double)x / (double)y;
    return res;
}

To use a delegate, you need to give the following definition at some place in the form class (or other class):

// Definition of the DelFun delegate, which receives two parameters of type int
// and returns the value of double type
public: delegate double DelFun(int , int);

...

Then, in some method (for example, the event handler for a click event on a button), you can use this pointer as follows:

double result;

DelFun ^ PFun = gcnew DelFun(this, &Lab1::Form1::Power);
result = PFun(3,2); // result = 9

PFun = gcnew DelFun(this, &Lab1::Form1::Div);
result = PFun(3,2); // result = 1.5

In the example above in the strings:

&Lab1::Form1::Power

...

&Lab1::Form1::Div

identifiers have the following purpose:

  • Lab1 – project name;
  • Form1 – the name of main form of application (variable name);
  • Power – function name.

11. What is the difference between a variable pointer and a constant pointer?

A variable pointer (pointer) is a variable that is used to store an address in memory.

A constant pointer is the value of the memory address.


Related topics