Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2023-06

Subscript pointers

9 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

I've forgotten entirely, what does subscripting a pointer do in Pascal? p[0] returns 100 but after that garbage. Seems like a c-style array which doesn't feel right in the language.

var
  i: Integer;
  p: PInteger;
begin
	p := @i;
	p^ := 100;
	writeln(p[0]);
	writeln(p[1]);
	writeln(p[2]);

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 11 lines
I've forgotten entirely, what does subscripting a pointer do in Pascal? p[0] returns 100 but after that garbage. Seems like a c-style array which doesn't feel right in the language. var i: Integer; p: PInteger; begin p := @i; p^ := 100; writeln(p[0]); writeln(p[1]); writeln(p[2]);

It's for easier pointer math, a construct which has been used in FPC since day 1 (AFAIK). Use with care, since no boundary checks are done.

Michael.

Reply by email

Hairy Pixels
to Michael Van Canneyt
On Jun 22, 2023, at 8:41 PM, Michael Van Canneyt via fpc-pascal wrote: It's for easier pointer math, a construct which has been used in FPC since day 1 (AFAIK). Use with care, since no boundary checks are done.

You mean instead of doing p + 1 or you can do p[1]? It may have been used for C compatibility too since an array in C is just a pointer.

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 8 lines
> On Jun 22, 2023, at 8:41 PM, Michael Van Canneyt via fpc-pascal wrote: > > It's for easier pointer math, a construct which has been used in FPC since day 1 (AFAIK). > Use with care, since no boundary checks are done. You mean instead of doing p + 1 or you can do p[1]?

Yes.

It may have been used for C compatibility too since an array in C is just a pointer.

Yes, the concepts are similar.

Michael.

Reply by email

Steve Litt
to Hairy Pixels: I've forgotten entirely, what does subscripting a pointer do in Pascal? p[0] returns 100…

Hairy Pixels via fpc-pascal said on Thu, 22 Jun 2023 20:23:13 +0700

Show quoted text 16 lines
I've forgotten entirely, what does subscripting a pointer do in Pascal? p[0] returns 100 but after that garbage. Seems like a c-style array which doesn't feel right in the language. var i: Integer; p: PInteger; begin p := @i; p^ := 100; writeln(p[0]); writeln(p[1]); writeln(p[2]); Regards, Ryan Joseph

I don't do much with Pascal pointers, but it looks to me like you:

* Set p to the address of i
* Set the contents of p to 100
* Read the contents of p, which of course would be the value of i, 100
* Read the contents of p+1, which would be one integer width away on the stack, and has not been assigned to anything.

So what you report is exactly what I'd expect to happen.

If p were pointing to the first element of an array of integer that had been initialized, it would have worked the way you intended.

p[1] is undefined, but if I had to guess, the way most compilers seem to work, I'd say it would be one integer width of the value of p[0], which is the address of i. If my supposition is correct, by modifying the contents of p[1] and maybe some other elements of p, you could write some pretty interesting self-modifying code. But don't do that.

SteveT

Steve Litt
Autumn 2022 featured book: Thriving in Tough Times http://www.troubleshooters.com/bookstore/thrive.htm

Reply by email

Hairy Pixels
to Steve Litt
Show quoted text 9 lines
On Jun 24, 2023, at 8:00 PM, Steve Litt via fpc-pascal wrote: I don't do much with Pascal pointers, but it looks to me like you: * Set p to the address of i * Set the contents of p to 100 * Read the contents of p, which of course would be the value of i, 100 * Read the contents of p+1, which would be one integer width away on the stack, and has not been assigned to anything.

Indeed that's what it's doing. It's strange as an array syntax though which suggests there's a range of uniform elements, when in reality there's just a pointer to a single location.

I think the more correct Pascal way to do it would be define an array type and pointer to that type and then subscript that instead of the C-style way.

Regards,
Ryan Joseph

Reply by email

Steve Litt
to Hairy Pixels

Hairy Pixels via fpc-pascal said on Sun, 25 Jun 2023 11:20:53 +0700

Show quoted text 19 lines
> On Jun 24, 2023, at 8:00 PM, Steve Litt via fpc-pascal > wrote: > > I don't do much with Pascal pointers, but it looks to me like you: > > * Set p to the address of i > * Set the contents of p to 100 > * Read the contents of p, which of course would be the value of i, > 100 > * Read the contents of p+1, which would be one integer width away on > the stack, and has not been assigned to anything. Indeed that's what it's doing. It's strange as an array syntax though which suggests there's a range of uniform elements, when in reality there's just a pointer to a single location. I think the more correct Pascal way to do it would be define an array type and pointer to that type and then subscript that instead of the C-style way.

Why do you need pointers at all? Pointers aren't *usually* an asset.

SteveT

Steve Litt
Autumn 2022 featured book: Thriving in Tough Times http://www.troubleshooters.com/bookstore/thrive.htm

Reply by email

Hairy Pixels
to Steve Litt
On Jun 26, 2023, at 3:48 PM, Steve Litt via fpc-pascal wrote: Why do you need pointers at all? Pointers aren't *usually* an asset.

Seriously? anytime you allocate something on the heap you need a pointer. Classes are pointers even.

Regards,
Ryan Joseph

Reply by email

Steve Litt
to Hairy Pixels

Hairy Pixels via fpc-pascal said on Mon, 26 Jun 2023 18:52:16 +0700

Show quoted text 8 lines
> On Jun 26, 2023, at 3:48 PM, Steve Litt via fpc-pascal > wrote: > > Why do you need pointers at all? Pointers aren't *usually* an asset. > Seriously? anytime you allocate something on the heap you need a pointer.

Exactly. Unless there are going to be an unknown and unlimited number of these structures, or unless they're expected to be insanely huge, I'll make them global variables. Globals suck, but so do unnecessary pointers.

Classes are pointers even.

Let me get back to you on that.

SteveT

Steve Litt
Autumn 2022 featured book: Thriving in Tough Times http://www.troubleshooters.com/bookstore/thrive.htm

Reply by email