开发者

Did languages before C/C++ have pointers?

Was there any pointer usage in old languages such as FORTRAN (pre Fortran-90), cobol or pascal? If not, then what is the method those languages used to implement the works done by pointers that are used in 开发者_开发知识库today's high level languages.

And As there is no pointers in java is there any way other way to replicate the work done by a pointer.


Algol 68 had references that were a lot like C/C++ pointers.

Pascal had pointers, though you couldn't do arithmetic on them.

PL/I had pointers.

In FORTRAN and COBOL, you mostly lived without such things as dynamic allocation written in the language. I did once write some linked-list code in FORTRAN that used an array, with an array index as the link to the "next" item (i.e., x[1] was a data item, x[2] was its link to the next data item, x[3] was another data item, and so on). Calling that "clumsy" was being almost excessively generous.


BCPL had the construct a*[b] (which could be simplified to a!b in our implementation, a 6809 embedded system compiler running on a 3B2 UNIX box) which was equivalent to a[b] in C. Of course, BCPL only had the concept of words, without all the structures and so forth that give C more power.

a!b was a word offset from a word address but the implementation we used also had options for byte offset from word address a!%b, and byte offset from byte address a%%b.


In these languages is possible to pass parameters to functions by reference or by value. By reference means exactly passing a pointer to that function. This is done in modern languages such as C# and Visual Basic too.


Don't forget ADA (and VHDL) access types. And assembly and (gasp) machine code support indirection, although there's no pointer type, it's all in the usage.


The functionality represented by pointers has been present in computer machine languages since very early on, so languages have always allowed some sort of access to the functionality, even if not always as generally as with pointers. I remember consulting on a port of some K&R C code into Fortran 66. The C code was full of pointers to structures, which didn't map nicely into any feature of Fortran 66.

Say the C code has structures like so:

struct datastruct { int ival; float fval; } data[100];

The Fortran mapping had a common block (not sure I have the formatting right, my Fortran 66 is extremely stale):

COMMON /DATASTRUCT/ IVAL(100), FVAL(100)

A function in C which takes a pointer to a structure doesn't have to know whether the structure came from the array or was malloc'ed or anything:

float func(struct datastruct *sp) { return sp->ival * sp->fval; }

When mapped into Fortran 66, all "structures" were just indexes into the arrays contained in the common block:

FUNCTION FUNC(INDEX)
COMMON /DATASTRUCT/ IVAL(100), FVAL(100)
RETURN IVAL(INDEX) * FVAL(INDEX)

Not nearly as elegant as pointers, but got the job done on a CDC 7600 back in the day.


Lisp, a language invented in the 1950s, had pointers. In fact, pointers were central to the manaagement of data in Lisp. "Lisp" was short for "List Processor" and lists were (are) linked lists in Lisp. This may be one of the earliest languages to use pointers as a building block.

In most dialects of Lisp, a list element costsists of two items. One is either a primitive data lelement, like a number, or a pointer to the eldest child of this element. The second element is a pointer to the next sibling of this element. Tree or graph structures of arbitrary complexity can be built up from these elements.

The first implementations of lisp all had automatic garbage collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜