Re: about reference



On 3/17/07, Jm lists <practicalperl@xxxxxxxxx> wrote:
snip
What's the difference between Perl's reference and C's pointer?
I'm always confused about them.Thanks for any direction.
snip

In ANSI C a pointer is an address into raw memory. How much memory it
returns is based on its type which is determined either by declaration
or casting. Static arrays are implemented as pointers that cannot be
changed and dynamic arrays are created using pointers. There is no
difference between saying *p and p[0]. Pointers can address any
memory, although accessing protected memory can cause a segfault.

In Perl the reference is a address of an entry in the symbol table.
That entry has a type which can be determined by calling the ref
function. References have nothing to do with how arrays are
implemented except that arrays have an entry in the symbol table like
everything else. Inside of Perl it is impossible to have a reference
to something that does not exist* since things only get garbage
collected when their reference count drops to zero.

So, it all comes down to this, a pointer is an unconstrained address
into memory and a reference is the result of the symbol table look up.

Hope this helps.

* If you are using XS, or an XS based module, then it is possible for
the C code to decrement the reference count manually.
.



Relevant Pages

  • Re: Array comparison
    ... > Deep and recursive. ... the pointers and comparing their referents instead of comparing the ... >> arrays, then I think you also need to argue against assigning arrays. ... semantics (notably, reference counting). ...
    (alt.comp.lang.borland-delphi)
  • Re: I need a new compiler...
    ... comp.lang.c FAQ would also have cleared up this misconception. ... Note it says nothing about any correspondence between such a reference ... and "low-level memory". ... The quoted text doesn't say that pointers could be ...
    (comp.lang.cobol)
  • Re: pointers, positions and datastorage
    ... > The purpose for all my questions stems from the fact that I am trying to ... > stored in memory in reverse order for the duration of ... > with relation to pointers is that you may want to use a int* pointer (and ... The reference table will be changed by the garbage ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Local copy of variable not acting locally
    ... Languages that support pointers can do ... > programming is just a matter of manipulating memory locations. ... A reference is also a location in memory, ...
    (microsoft.public.vb.general.discussion)
  • Re: Fortran and .NET (C#)
    ... Was there "garbage collection" in Fortran 90 or was there reference counting? ... For functions returning arrays that are either explicit-sized or allocatable, the language is designed such that you don't need either of those. ... And if one is a novice, well, one pretty much abandons all hope of a bug-free program if one uses functions that return pointers. ... Recall that, unlike C, Fortran doesn't force you into using pointers just because you have arrays. ...
    (comp.lang.fortran)