🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #3334

Re: pointers

#3334
From: Jeffrey E Stoner
Date: Mon, 04 Mar 1996 05:24
39 lines
2129 bytes
Soo Fighter (wsoo1@icarus.uic.edu) wrote:
: I have a quick question: are two pointers to two variables (which may or 
: may not be of the same type) not interassignable and intercomparable?
: If they are, why?

Pointers are just addresses.  They point to one particular place in memory.
When you specify a type for a pointer (*POINTER TO INTEGER*) you also
indicate the size of the address space.  
This is a round about way of saying no.  The why can be easily seen with
an example:

FirstVar : POINTER TO INTEGER;
SecondVar : POINTER TO REAL;

For this example, an integer will be 2 bytes and a real will be 4 bytes in 
IEEE standard format.

When you dereference FirstVar, you get 2 bytes of information.  When you
dereference SecondVar, you get 4.  If you execute this statement:
SecondVar := FirstVar;
SecondVar still dereferences 4 bytes.  The first 2 bytes will be your integer,
but the second 2 will be who knows what.  In effect, when you dereference
SecondVar, you'll get garbage.  This gets uglier when to store values in
SecondVar because you'll overwrite whatever was in those last 2 bytes.  
What was there could be instruction code or another variable.  You've
just trashed your memory.  Pretty bad, eh?

If FirstVar and SecondVar are of the same type, there's no problem because
they're the same "size."  Most compilers won't (shouldn't) let you 
assign a pointer to another of a different type without a type-cast.  

Simple, eh?  

Jeff

-- 
   *  internet-->  jesst63+@pitt.edu             * This space reserved  *
   *  http://www.pitt.edu/~jesst63               * for something much   *
   * "I am outcast, unclean." - Thomas Covenant  * better to come along *
Technical Details
Message-ID: <4hdupi$80u@usenet.srv.cis.pitt.edu>
Path: arc1-ca.pugleaf.net!newsfeed.pitt.edu!jesst63
References: <Pine.SOL.3.91.960229172245.1613A-100000@icarus.cc.uic.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newsfeed.pitt.edu!jesst63
From: jesst63+@pitt.edu (Jeffrey E Stoner)
Newsgroups: comp.lang.modula2
Subject: Re: pointers
Date: 4 Mar 1996 05:24:02 GMT
Organization: University of Pittsburgh
Message-ID: <4hdupi$80u@usenet.srv.cis.pitt.edu>
References: <Pine.SOL.3.91.960229172245.1613A-100000@icarus.cc.uic.edu>
NNTP-Posting-Host: unixs-eval.cis.pitt.edu
X-Newsreader: TIN [version 1.2 PL2]