[fpc-pascal] Pointers

Rainer Stratmann RainerStratmann at t-online.de
Thu Feb 11 18:17:52 CET 2010


I thought that adding something to a pointer always adds 1 sizeof(byte) to it.
So if something is added to a pointer the compiler looks the sizeof the 
(typed) pointer points to?
Ok, that makes some sense, but I did not know it before.
In the past with the turbopascal compiler and other always sizeof byte was 
added.
Thank you and regards, Rainer


Am Thursday 11 February 2010 17:36:04 schrieb Frank Peelo:
> So you're allowed add an integer to an untyped pointer?!
>
> Wow!
>
> Usually if you add 1 to a pointer of type t, then sizeof(t) gets added
> to the value of the pointer. So if p points at an array of byte, p+1
> would point at the next element of the array, 1 byte after p. But if p
> points at an array of LongInt, then the next element is 4 bytes on from
> p, so adding 1 to p increases p by 4. So what would sizeof(pointer^) be?
>
> To me, adding an integer to an untyped pointer feels undefined (i.e. I
> haven't studied the spec for it but I wouldn't have expected the
> compiler to do anything sensible with it) because what pointer points to
> is undefined and sizeof(something undefined) is not defined. So I
> haven't tried that myself.
>
>  From your code, I'm guessing that adding 1 to an untyped pointer adds 1
> to the address. So ppp + http.header_anz would be http.header_anz bytes
> after ppp.
>
> But http.pdata + http.header_anz means to add http.header_anz to a
> variable of type pointer to tchararr. That means, imagine http.pdata is
> pointing to an array[0..lots] of tcharrarr, find the address of element
> http.header_anz of that array. Now, tcharrarr is 1000 bytes long, so I
> would expect http.pdata + http.header_anz to be 1000*http.header_anz
> bytes after http.pdata.
>
> Is that what you are seeing?
I did not test it, but I think you are right.
> FP
>
> On 11/02/2010 15:38, Rainer Stratmann wrote:
> > What is the difference between A* and B*?
> > regards, Rainer
> >
> >
> > type
> >  tchararr = array[ 0..999 ] of char;
> >  pchararr = ^tchararr;
> >
> >  http_obj = object
> >   pdata            : pchararr;
> >   header_anz        : longint;
> >   content_anz       : longint;
> >  end;
> >
> > var
> >  http : http_obj;
> >  ppp : pointer;
> >
> >
> >  // This works for me A*
> >  ppp := http.pdata;
> >  ppp := ppp + http.header_anz;
> >  // This is not working B*
> >  ppp := http.pdata + http.header_anz;
> >
> >
> >  write( 'IP-Adress = ' + parse_ip( ppp , http.content_anz ) );
> > _______________________________________________
> > fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> > http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal





More information about the fpc-pascal mailing list