Sign in Join

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

Mailing lists / fpc-pascal / 2004-09

Apple Mac pointer problem

16 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Stephen Downs

I am porting a Mac CodeWarrior project to FreePascal and have hit a compile problem with a pointer. In my original code I have a structure defined along the lines of

testData = record
   testIndex :  array [0..12] of integer;
end;
testDataPointer = ^testData;
testDataHandle = ^testDataPointer;

Within my main code I open a file and try to read it into my structure

testDataHdl := testDataHandle(NewHandleClear(sizeof(testData)));
OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^)

The FSRead line fails with error: 62: Incompatible type for arg no. 3: Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is a pointer. I presume this is because FreePascal uses typed pointers. I tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a similar error - Got "ShortInt", expected "Ptr"

Does anyone know how I can get around this?

regards... Steve

Marcel Martin
to Stephen Downs
Stephen Downs wrote 24 lines
I am porting a Mac CodeWarrior project to FreePascal and have hit a compile problem with a pointer. In my original code I have a structure defined along the lines of testData = record testIndex : array [0..12] of integer; end; testDataPointer = ^testData; testDataHandle = ^testDataPointer; Within my main code I open a file and try to read it into my structure testDataHdl := testDataHandle(NewHandleClear(sizeof(testData))); OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^) The FSRead line fails with error: 62: Incompatible type for arg no. 3: Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is a pointer. I presume this is because FreePascal uses typed pointers. I tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a similar error - Got "ShortInt", expected "Ptr" Does anyone know how I can get around this?

This is very likely a stupid suggestion but have you tried this

Ptr(testDataHdl^)

mm http://www.ellipsa.net/ ( suppress no.sp.am. )
Stephen Downs

-----Original Message-----

Earlier message 37 lines
From: Marcel Martin Sent: Thu 9/23/2004 9:18 PM To: Cc: Subject: Re: [fpc-pascal]Apple Mac pointer problem Stephen Downs a écrit : > > I am porting a Mac CodeWarrior project to FreePascal and have hit a > compile problem with a pointer. In my original code I have a structure > defined along the lines of > > testData = record > testIndex : array [0..12] of integer; > end; > > testDataPointer = ^testData; > testDataHandle = ^testDataPointer; > > Within my main code I open a file and try to read it into my structure > > testDataHdl := testDataHandle(NewHandleClear(sizeof(testData))); > OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^) > > The FSRead line fails with error: 62: Incompatible type for arg no. 3: > Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is a > pointer. I presume this is because FreePascal uses typed pointers. I > tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a > similar error - Got "ShortInt", expected "Ptr" > > Does anyone know how I can get around this? >This is very likely a stupid suggestion but have you tried this > > Ptr(testDataHdl^) I had tried that and it compiles ok but when it runs, the FSRead statement returns an error -36 which is an I/O error
Olle Raab
to Stephen Downs

04-09-23 23.09, skrev Stephen Downs följande:

Show quoted text 28 lines
-----Original Message----- From: Marcel Martin Sent: Thu 9/23/2004 9:18 PM To: Cc: Subject: Re: [fpc-pascal]Apple Mac pointer problem Stephen Downs a écrit : > > I am porting a Mac CodeWarrior project to FreePascal and have hit a > compile problem with a pointer. In my original code I have a structure > defined along the lines of > > testData = record > testIndex : array [0..12] of integer; > end; > > testDataPointer = ^testData; > testDataHandle = ^testDataPointer; > > Within my main code I open a file and try to read it into my structure > > testDataHdl := testDataHandle(NewHandleClear(sizeof(testData))); > OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^) > > The FSRead line fails with error: 62: Incompatible type for arg no. 3: > Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is a > pointer. I presume this is because FreePascal uses typed pointers.

The reason you get the error msg is that fpc supports UNIV as a dummy keyword. Is it is accepted but doesn't do anything.

Since the third param is declared UNIV you do not need any typecast in Metrowerks, but you need to cast to Ptr in fpc (as Marcel suggests). From UI:
FUNCTION FSRead(refNum: INTEGER; VAR count: LONGINT; buffPtr: UNIV Ptr): OSErr;

> I > tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a > similar error - Got "ShortInt", expected "Ptr"

The mac type Ptr is defined as usual in the UI, as a pointer to a signed byte, hence the reference to ShortInt in the error msg, which is a signed byte.

Show quoted text 7 lines
> Does anyone know how I can get around this? =20 > This is very likely a stupid suggestion but have you tried this >=20 > Ptr(testDataHdl^) I had tried that and it compiles ok but when it runs, the FSRead statemen=

t

returns an error -36 which is an I/O error

Are you shure nothing else is wrong ?

If you read a binary file, alignment might not be the same.

Actually FPC does not align arrays the same as trad mac compilers.

Olle

Harald Houppermans
to Stephen Downs: I am porting a Mac CodeWarrior project to FreePascal and have hit a compile problem with…

----- Original Message -----

Earlier message 48 lines
From: "Stephen Downs" To: Sent: Thursday, September 23, 2004 8:49 PM Subject: [fpc-pascal]Apple Mac pointer problem I am porting a Mac CodeWarrior project to FreePascal and have hit a compile problem with a pointer. In my original code I have a structure defined along the lines of testData = record testIndex : array [0..12] of integer; end; testDataPointer = ^testData; testDataHandle = ^testDataPointer; Within my main code I open a file and try to read it into my structure testDataHdl := testDataHandle(NewHandleClear(sizeof(testData))); OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^) The FSRead line fails with error: 62: Incompatible type for arg no. 3: Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is a pointer. I presume this is because FreePascal uses typed pointers. I tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a similar error - Got "ShortInt", expected "Ptr" Does anyone know how I can get around this? regards... Steve Euhm... just my two cents: testDataHdl is in fact a pointer to a pointer to data. so testDataHdl^ points to a pointer. If you want to read a pointer from the file that's good. But if you actually want to read the data then it's a mistake. Then you need to use this: testDataHdl^^ Bye, Skybuck.
Harald Houppermans

----- Original Message -----

Earlier message 106 lines
From: "Harald Houppermans" To: Sent: Friday, September 24, 2004 3:06 AM Subject: Re: [fpc-pascal]Apple Mac pointer problem > > > > ----- Original Message ----- > From: "Stephen Downs" > To: > Sent: Thursday, September 23, 2004 8:49 PM > Subject: [fpc-pascal]Apple Mac pointer problem > > > I am porting a Mac CodeWarrior project to FreePascal and have hit a > compile problem with a pointer. In my original code I have a structure > defined along the lines of > > testData = record > testIndex : array [0..12] of integer; > end; > > testDataPointer = ^testData; > testDataHandle = ^testDataPointer; > > Within my main code I open a file and try to read it into my structure > > testDataHdl := testDataHandle(NewHandleClear(sizeof(testData))); > OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^) > > The FSRead line fails with error: 62: Incompatible type for arg no. 3: > Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is a > pointer. I presume this is because FreePascal uses typed pointers. I > tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a > similar error - Got "ShortInt", expected "Ptr" > > Does anyone know how I can get around this? > > regards... Steve > > Euhm... just my two cents: > > testDataHdl is in fact a pointer to a pointer to data. > > so testDataHdl^ points to a pointer. > > If you want to read a pointer from the file that's good. > > But if you actually want to read the data then it's a mistake. > > Then you need to use this: > > testDataHdl^^ > > Bye, > Skybuck. > Though euhm.. it depends on the way the routine really works. It seems it's excepting pointers. So testDataHdl^ seems correct... since that will supply the routine with a pointer to the data. Maybe the prototype is incorrect ? You can always try testDataHdl^^ and see if it works ;) lol. Though looking at the code again... why is testDataPointer declared (it doesn't seem to be used) ? In other words... 1. Does the routine expect a pointer to data ? 2. Does the routine expect a pointer to a pointer to data ? 3. Or does the routine expect like a var parameter... that means the data itself... like a const reference or something ;) For example pascal and delphi file routines and streams work with number 3 ;) In your case it seems the routine work with number 1. That means testDataHdl^ is simply a mistake. Since you are now dereferencing it to the data. The routine doesn't want the data... the routine wants a pointer. So the correct call is probably something like: OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl ) testDataHdl (without the hat ^ ) The funny thing is the declaration of the testDataPointer put you, me and everybody else off ;) It's simply misleading :) Bye, Skybuck.
Harald Houppermans

----- Original Message -----

Earlier message 166 lines
From: "Harald Houppermans" To: Sent: Friday, September 24, 2004 3:19 AM Subject: Re: [fpc-pascal]Apple Mac pointer problem > > ----- Original Message ----- > From: "Harald Houppermans" > To: > Sent: Friday, September 24, 2004 3:06 AM > Subject: Re: [fpc-pascal]Apple Mac pointer problem > > > > > > > > > > ----- Original Message ----- > > From: "Stephen Downs" > > To: > > Sent: Thursday, September 23, 2004 8:49 PM > > Subject: [fpc-pascal]Apple Mac pointer problem > > > > > > I am porting a Mac CodeWarrior project to FreePascal and have hit a > > compile problem with a pointer. In my original code I have a structure > > defined along the lines of > > > > testData = record > > testIndex : array [0..12] of integer; > > end; > > > > testDataPointer = ^testData; > > testDataHandle = ^testDataPointer; > > > > Within my main code I open a file and try to read it into my structure > > > > testDataHdl := testDataHandle(NewHandleClear(sizeof(testData))); > > OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^) > > > > The FSRead line fails with error: 62: Incompatible type for arg no. 3: > > Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is > a > > pointer. I presume this is because FreePascal uses typed pointers. I > > tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a > > similar error - Got "ShortInt", expected "Ptr" > > > > Does anyone know how I can get around this? > > > > regards... Steve > > > > Euhm... just my two cents: > > > > testDataHdl is in fact a pointer to a pointer to data. > > > > so testDataHdl^ points to a pointer. > > > > If you want to read a pointer from the file that's good. > > > > But if you actually want to read the data then it's a mistake. > > > > Then you need to use this: > > > > testDataHdl^^ > > > > Bye, > > Skybuck. > > > > Though euhm.. it depends on the way the routine really works. > > It seems it's excepting pointers. > > So testDataHdl^ seems correct... since that will supply the routine with a > pointer to the data. > > Maybe the prototype is incorrect ? > > You can always try testDataHdl^^ and see if it works ;) lol. > > Though looking at the code again... why is testDataPointer declared (it > doesn't seem to be used) ? > > In other words... > > 1. Does the routine expect a pointer to data ? > 2. Does the routine expect a pointer to a pointer to data ? > 3. Or does the routine expect like a var parameter... that means the data > itself... like a const reference or something ;) > > For example pascal and delphi file routines and streams work with number 3 > ;) > > In your case it seems the routine work with number 1. > > That means testDataHdl^ is simply a mistake. > > Since you are now dereferencing it to the data. > > The routine doesn't want the data... the routine wants a pointer. > > So the correct call is probably something like: > > OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl ) > > testDataHdl (without the hat ^ ) > > The funny thing is the declaration of the testDataPointer put you, me and > everybody else off ;) > > It's simply misleading :) For a second there... I thought testDataHdl is a pointer to data... But my first posting was correct... it's a pointer to a pointer to data. Are you sure you want that ? Maybe you should try changing the declarion from: testDataHandle = ^testDataPointer; to this: testDataHandle = ^testData; Now the type points directly to the data. And then the routine call should probably be: OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl ) So the real problem with your code... might be that it is missing a piece of allocated memory. test data is probably allocated test data handle pointer is allocated. but the pointer in between is missing. +----------------+ +-----------------+ +----------+ | test data handle | ->| test data pointer | -> | test data | +----------------+ +-----------------+ +----------+ ( By changing the declarion like i mentioned the problem could be solved ;) ) Though it depends on what this routine does: testDataHandle(NewHandleClear(sizeof(testData))); It seems to be just clearing the data... Maybe you should post some more code... how the structure and pointers are allocated. And maybe a description of the routine and it's prototype ;) So to me it seems the structures etc are a bit f*cked up ;) If that's not the case... and you are 100% sure you know what you doing yhen it's probably something totally else... and I should just butt out... lol. Bye, Skybuck.
Olle Raab
to Harald Houppermans

04-09-24 03.33, skrev Harald Houppermans följande:

Show quoted text 12 lines
> Though looking at the code again... why is testDataPointer declared (it > doesn't seem to be used) ? > > In other words... > > 1. Does the routine expect a pointer to data ? > 2. Does the routine expect a pointer to a pointer to data ? > 3. Or does the routine expect like a var parameter... that means the data > itself... like a const reference or something ;) > > For example pascal and delphi file routines and streams work with number 3 > ;)

This kind of parameters do not exist on tradiional mac compilers, you have to use pointers.

Show quoted text 17 lines
test data is probably allocated test data handle pointer is allocated. but the pointer in between is missing. +----------------+ +-----------------+ +----------+ | test data handle | ->| test data pointer | -> | test data | +----------------+ +-----------------+ +----------+ ( By changing the declarion like i mentioned the problem could be solved ;) ) Though it depends on what this routine does: testDataHandle(NewHandleClear(sizeof(testData))); It seems to be just clearing the data...

No, this is a Mac OS routine to allocate a zeroed block of memory, which is pointed to by a mac "handle", which is simply a pointer to a pointer. This kind of handles will be automatically reallocated if needed, to keep memory unfragmented.

Show quoted text 6 lines
Maybe you should post some more code... how the structure and pointers are allocated. And maybe a description of the routine and it's prototype ;) So to me it seems the structures etc are a bit f*cked up ;)

They are OK :)

If that's not the case... and you are 100% sure you know what you doing yhen it's probably something totally else... and I should just butt out... lol.

Yes, I think so :)

Olle

Unknown author
to Olle Raab

----- Original Message -----

Earlier message 38 lines
From: "Olle Raab" To: Sent: Friday, September 24, 2004 8:10 AM Subject: Re: [fpc-pascal]Apple Mac pointer problem 04-09-24 03.33, skrev Harald Houppermans följande: >> Though looking at the code again... why is testDataPointer declared (it >> doesn't seem to be used) ? >> >> In other words... >> >> 1. Does the routine expect a pointer to data ? >> 2. Does the routine expect a pointer to a pointer to data ? >> 3. Or does the routine expect like a var parameter... that means the data >> itself... like a const reference or something ;) >> >> For example pascal and delphi file routines and streams work with number 3 >> ;) This kind of parameters do not exist on tradiional mac compilers, you have to use pointers. ---- Well that is interesting in itself... I hope this only applies to macintosh operating system API's ? How would I otherwise port my code to macintosh os with a free pascal (cross) compiler ;) ? Don't tell me I have to replace every single var and const parameter ? :) Bye, Skybuck.
Stephen Downs
Show quoted text 73 lines
-----Original Message----- From: Olle Raab Sent: 23 September 2004 21:45 To: Subject: Re: [fpc-pascal]Apple Mac pointer problem 04-09-23 23.09, skrev Stephen Downs följande: > -----Original Message----- > From: Marcel Martin > Sent: Thu 9/23/2004 9:18 PM > To: > Cc: > Subject: Re: [fpc-pascal]Apple Mac pointer problem > > Stephen Downs a écrit : >> >> I am porting a Mac CodeWarrior project to FreePascal and have hit a >> compile problem with a pointer. In my original code I have a structure >> defined along the lines of >> >> testData = record >> testIndex : array [0..12] of integer; >> end; >> >> testDataPointer = ^testData; >> testDataHandle = ^testDataPointer; >> >> Within my main code I open a file and try to read it into my structure >> >> testDataHdl := testDataHandle(NewHandleClear(sizeof(testData))); >> OSError := FSRead(fileRefNum, numberOfBytes, testDataHdl^) >> >> The FSRead line fails with error: 62: Incompatible type for arg no. 3: >> Got "testDataPointer", expected "Ptr" - the third parameter for FSRead is a >> pointer. I presume this is because FreePascal uses typed pointers. The reason you get the error msg is that fpc supports UNIV as a dummy keyword. Is it is accepted but doesn't do anything. Since the third param is declared UNIV you do not need any typecast in Metrowerks, but you need to cast to Ptr in fpc (as Marcel suggests). From UI: FUNCTION FSRead(refNum: INTEGER; VAR count: LONGINT; buffPtr: UNIV Ptr): OSErr; >> I >> tried replacing testDataHdl^ by Ptr(testDataHdl)^ but that gives a >> similar error - Got "ShortInt", expected "Ptr" The mac type Ptr is defined as usual in the UI, as a pointer to a signed byte, hence the reference to ShortInt in the error msg, which is a signed byte. >> Does anyone know how I can get around this? > >> This is very likely a stupid suggestion but have you tried this >> >> Ptr(testDataHdl^) > I had tried that and it compiles ok but when it runs, the FSRead statement > returns an error -36 which is an I/O error Are you shure nothing else is wrong ? If you read a binary file, alignment might not be the same. Actually FPC does not align arrays the same as trad mac compilers.

Thanks for everyones input (Harold - it a Mac thing !!). I'm away for a couple of days now so won't be able to follow up until next week but I guess I need to read up on alignment of binary data.

I assume from what you are saying is that Ptr(testDataHdl^) is the right way to do it in FreePascal but I need to check on how the data is read into the structure.

Show quoted text 6 lines
Olle _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Olle Raab
to Stephen Downs

04-09-24 09.44, skrev Stephen Downs följande:

Show quoted text 8 lines
Thanks for everyones input (Harold - it a Mac thing !!). I'm away for a couple of days now so won't be able to follow up until next week but I guess I need to read up on alignment of binary data. I assume from what you are saying is that Ptr(testDataHdl^) is the right way to do it in FreePascal but I need to check on how the data is read into the structure.

If the problem is the alignment, you can try to read element by element.

Olle

Unknown author
to Olle Raab

----- Original Message -----

Earlier message 41 lines
From: "Olle Raab" To: Sent: Friday, September 24, 2004 3:57 PM Subject: Re: [fpc-pascal]Apple Mac pointer problem 04-09-24 09.44, skrev Stephen Downs följande: > > Thanks for everyones input (Harold - it a Mac thing !!). I'm away for a couple > of days now so won't be able to follow up until next week but I guess I need > to read up on alignment of binary data. > > I assume from what you are saying is that Ptr(testDataHdl^) is the right way > to do it in FreePascal but I need to check on how the data is read into the > structure. If the problem is the alignment, you can try to read element by element. What about using the 'packed' directive ? testData = record testIndex : packed array [0..12] of integer; end; Wouldn't that solve any alignment problems ? Or doesn't free pascal have a 'packed' directive ? Or is it a reallllly strange mac problem ? :) Bye, Skybuck ( Gjez... outlook still uses the wrong default mail account... weird... maybe it s because the mailing list uses my other mail account to sent mail to... )
Jonas Maebe
to Unknown author
Show quoted text 7 lines
testData = record testIndex : packed array [0..12] of integer; end; Wouldn't that solve any alignment problems ? Or doesn't free pascal have a 'packed' directive ?

It does, but it only works for individual records. It's ignored for arrays.

Jonas

Olle Raab
to Unknown author: ----- Original Message ----- From: "Olle Raab" To: Sent: Friday, September 24, 2004 8:10…

04-09-24 08.49, skrev följande:

Show quoted text 32 lines
----- Original Message ----- From: "Olle Raab" To: Sent: Friday, September 24, 2004 8:10 AM Subject: Re: [fpc-pascal]Apple Mac pointer problem 04-09-24 03.33, skrev Harald Houppermans följande: >> Though looking at the code again... why is testDataPointer declared (it >> doesn't seem to be used) ? >> >> In other words... >> >> 1. Does the routine expect a pointer to data ? >> 2. Does the routine expect a pointer to a pointer to data ? >> 3. Or does the routine expect like a var parameter... that means the data >> itself... like a const reference or something ;) >> >> For example pascal and delphi file routines and streams work with number 3 >> ;) This kind of parameters do not exist on tradiional mac compilers, you have to use pointers. ---- Well that is interesting in itself... I hope this only applies to macintosh operating system API's ?

Exactly, the above apply only when calling the macos api directly.

Olle