PDA

View Full Version : pointer offset ..



marmin
21-03-2005, 06:27 AM
small question, maybe n00b



var
foo: array [0..100] of single;
p: ^single;

begin
p := @foo[0];
if p {+ offset} ^:= 30 then exit;
end;


So I want to point to address of foo[0] + a certain amount of memory addresses; let's say, offset of 10. Can I do this in Pascal or do I have to use asm?

Thanks.

{MSX}
21-03-2005, 08:29 AM
small question, maybe n00b


var
foo: array [0..100] of single;
p: ^single;

begin
p := @foo[0];
if p {+ offset} ^:= 30 then exit;
end;


So I want to point to address of foo[0] + a certain amount of memory addresses; let's say, offset of 10. Can I do this in Pascal or do I have to use asm?

Thanks.

Well i sometimes did it using mighty double cast:


newpointer := pointer(cardinal(p)+offset);


But then i realized that using pointers is almost always a bad idea and switched to something else :P
So if you have this kind of problem maybe you could ask a way to perform it without using pointers.

"Leave dirty pointers hell to C programmers"

marmin
21-03-2005, 09:55 AM
maybe that's why most commercial games are written in C /C++ hehe.. buy yeah it can be Hell if things are not wriiten down clearly.
no I'll stick to pointers. of course there's a work around

which uses a pointer to a pointer.


var
foo : array [0..100] of single;
p : ^single;
p2 : ^DWORD; // mem address
offset : integer;
value : single;

begin
value := 30;
offset := 5;
p := @foo[0];
p2 := @p;
p2 := p2 + offset; // 5 mem addresses advance
if (p2 ^ = value) then exit;
end;


cool, this actually works. I love Pascal.

marmin
21-03-2005, 10:09 AM
[quote="marmin"]small question, maybe n00b


newpointer := pointer(cardinal(p)+offset);


That may works also, it compiles in any case. I'll test.

{MSX}
21-03-2005, 10:11 AM
maybe that's why most commercial games are written in C /C++ hehe..
no I'll stick to pointers. of course there's a work around

which uses a pointer to a pointer.


I'll pretend i didn't read :D



cool, this actually works. I love Pascal. b.t.w. how can I add Pascal code in my posts?

Simply use the PASCAL tag instead of the CODE tag, or use the PASCAL button when you reply.

Bye :P

marmin
21-03-2005, 10:13 AM
I found out.

Sly
21-03-2005, 11:35 AM
Pointers rock! :D I use pointer casting tricks like MSX pointed out all the time, especially when converting C/C++ code to Delphi or reading binary file formats.

K4Z
21-03-2005, 11:44 AM
I like to get really drunk when dealing with pointers, makes all those hours of debugging more tolerable. :)

marmin
21-03-2005, 11:44 AM
yeah I agree; they make the code shorter and faster. The way Pascal 'handles' pointers I like very much. You're free to use them, but there is always a check by the compiler if things not go really out of hand. That's, imho, the way it should be.

And yes, I too had a really, really hard time understanding pointers. I used to be awake in the middle of the night, frantic, crying, a bottle of wine beside me. It's getting better now ..:) (But I still find a Bordeaux in the night a good friend).

Sly
21-03-2005, 11:48 AM
I code for Playstation 2, PC and Xbox all day. I live in pointer land. :D

{MSX}
21-03-2005, 12:39 PM
Oh my god i can't believe this :P
I'm surrouded by dirty users of dirty pointers :mrgreen:

Anyone thinks like me that pointers should be really left to inferior C programmers ? :P

I really have very few occasion where i feel the need to use pointers. I'm almost always using arrays, list, etc, turning to pointers only for api calls and the like.

After all my mother told me that pointing is inpolite.

WILL
21-03-2005, 05:12 PM
Hmm... I try to use objects most of the time. Pointers only when I am allocating dynamic memory chunks and arrays and records when I need a fixed size of storage ie. saveing/loading file routeens.

One of my biggest game designs used alot of Pointers... and oh man... you have to be careful with them. But they do have their uses, if you manage them well.