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

4.2.5Type Defining a Pointer

The C++ language also allows you to type define a pointer to a data type. To use this feature, type the typedef keyword, followed by a known data type, followed by an asterisk and followed by a common for the pointers you want to create. Here is an example:

typedef int *Positive;

After using the typedef keyword like this, the Positive word become synonym to a pointer to int. It can be used to declare variables that are pointers to an integer. Here is an example:

// Programmer-defined data type

typedef int *Positive;

Positive Age, Category;

In this example, Age is a pointer to int; so is Category.

To hint that the name of a variable is a pointer, it is a tradition to start it with p, P, ptr, or Ptr. Many of the pointers you will encounter in the Visual Component Library and in the MSDN documentation use this convention. The pointers we have seen so far could be declared as follows:

Int *pAge;

unsigned int *ptrCategory;

double* PSalary;

long double * PtrDenominator;

double *PHourlySalary, *PWeeklySalary;

typedef int *PPositive;

Positive pAge, pCategory;

 

4.3Pointers and Memory Management

4.3.1 Introduction

Imagine you write the following program:

using namespace System;

Int main()

{

typedef double Salary;

Salary Monthly;

Salary Hourly, Weekly;

double WeeklyHours;

return 0;

}

When your program launches, that is, when it loads in the computer memory, the compiler reserves an appropriate amount of space in the heap memory for each declared variable. This technique of providing memory space is called static allocation, the memory space is "allocated" to that variable. Just after the program has launched and memory has been allocated (or assigned) to each variable, each memory space is filled 0. Check it with the following program:

using namespace System;

Int main()

{

typedef double Salary;

Salary Monthly;

Salary Hourly, Weekly;

double WeeklyHours;

Console::WriteLine("Employee Payroll");

Console::Write("Weekly Hours: ");

Console::WriteLine(WeeklyHours);

Console::Write("Hourly Salary: ");

Console::WriteLine(Hourly);

Console::Write("Weekly Salary: ");

Console::WriteLine(Weekly);

Console::Write("Monthly Salary: ");

Console::WriteLine(Monthly);

return 0;

}

This would produce the following warnings:

warning C4700: uninitialized local variable 'WeeklyHours' used

warning C4700: uninitialized local variable 'Hourly' used

warning C4700: uninitialized local variable 'Weekly' used

warning C4700: uninitialized local variable 'Monthly' used

It would display the following:

Employee Payroll

Weekly Hours: 0

Hourly Salary: 0

Weekly Salary: 0

Monthly Salary: 0

Press any key to continue

As you can see, the memory allocated to each variable is filled with a 0 value. If you decide to use a pointer to a variable, you can declare and initialize such a variable. This system of declaring and initializing a pointer also allows the compiler to allocate a memory space for the pointer in the heap. Remember, just like a regular variable, a pointer is also a variable and uses its own memory space. This pointer can also be assigned a value that would be shared with the pointed to variable:

using namespace System;