On 3/17/07, Jm lists <[EMAIL PROTECTED]> 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.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to