Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 5/Pointers/ja

Sent here from Pointers/ja.

English · Français · Български · 中文(中国) · 日本語

5F - ポインタ (著者: Tao Yue, 状態: 原文のまま変更なし)

ポインタ はメモリ・アドレスを持ったデータタイプである。 ポインタはそのメモリアドレスへの参照と考えることができる。一方、変数はそのメモリアドレスに直接アクセスする。もし、変数が誰かの電話番号なら、ポインタは電話帳に載っているページや行番号に相当する。そのメモリアドレスに保存されているデータにアクセスするためには、ポインタを参照する。

メモリ・ルーチン

ポインタ・データ型を宣言するためには、それが指し示すものを指定しなくてはならない。データ・タイプの前にはキャラット (^) をつける。 たとえば、整数でポインタを作成するなら、次のようなコードを用いる。

pascal
type
    PointerType = ^integer;

もちろん、その後で変数をPointerTypeと宣言することができる。

ポインタにアクセスする前に、アクセスするポインタのためにメモリのその部分を遮断する。 そのためには次のようにする。

pascal
New(PointerVariable);

ポインタのメモリ位置にあるデータにアクセスするためには、ポインタ名のあとにキャレット(^)を加える。 For example, if PointerVariable was declared as type PointerType (from above), you can assign the memory location a value by using:

pascal
PointerVariable^ := 5;

After you are done with the pointer, you must deallocate the memory space. Otherwise, each time the program is run, it will allocate more and more memory until your computer has no more. To deallocate the memory, you use the Dispose command:

pascal
Dispose(PointerVariable);

A pointer can be assigned to another pointer. However, note that since only the address, not the value, is being copied, once you modify the data located at one pointer, the other pointer, when dereferenced, also yields modified data. Also, if you free (or deallocate) a pointer, the copied pointer now points to meaningless data.

Trivial usage example: singly linked lists

What is a pointer good for? Why can't you just use an integer in the examples above instead of a pointer to an integer? Well, the above is clearly a contrived example. The real power of pointers is that, in conjunction with records, it makes dynamically-sized data structures possible. If you need to store many items of one data type in order, you can use an array. However, your array has a predefined size. If you don't have a large enough size, you may not be able to accommodate all the data. If you have a huge array, you take up a lot of memory when sometimes that memory is not being used.

A dynamic data structure, on the other hand, takes up only as much memory as is being used. What you do is to create a data type that points to a record. Then, the record has that pointer type as one of its fields. E. g. stacks and queues can all be implemented using this data structure:

pascal
type
    PointerType = ^RecordType;
    RecordType = record
        data : integer;
        next : PointerType;
    end;

Each element points to the next. The last record in the chain indicates that there is no next record by setting its next field to a value of nil.

previous contents next

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 5/Pointers/ja", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 4 people, 2015–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
DerakunCreated page
Derakun
Derakun
FTurtletemplatename has not suffix
Derakun
Derakun
Derakun
TrevFixed syntax highlighting (minor)
Kai BurghardtKai Burghardt moved page Pointers/ja to Basic Pascal Tutorial/Chapter 5/Pointers/ja: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/149778#Clutte... (minor)
Kai Burghardtbypass redirects [cf. discussion]
Kai Burghardtfix previous edit (minor)
Kai Burghardtbypass language bar/categorization template redirect [cf. discussion] (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.