Hello ,
Is typecasting like
MyType(Myvar);
not allowed?
How do i do something like the following?
-------------------------------------
p:pointer; mytype=record ..... end;
with mytype(p^) do begin ..... end;
----------------------------------------- Here i cannot use another temporary variable also.
Thanks & Regards, Anuradha
ANURADHA Srinivasan wrote:
Is typecasting like
MyType(Myvar);
not allowed?
Yes, GPC allows this as an extension.
How do i do something like the following?
p:pointer; mytype=record ..... end;
with mytype(p^) do begin ..... end;
Since you gave neither a complete sample or an error message, I don't know what's your problem. (I'm really tired of second-guessing.)
Frank
Hello ,
It is not a typecasting error.It's probably fillchar question.I find the following sample program ending in a "segmentation fault -core dump" , i think because of doing a fillchar of a record which has a member that's a pointer . -------------------------------------- program a; type myrec=record x:^integer; end; var ptr :^myrec;
begin getmem(ptr,sizeof(myrec)); fillchar(ptr^,sizeof(myrec),0); ptr^.x^:=5; write(ptr^.x^); end. --------------------------------
If the member is made just an integer and not a pointer to integer,it works. this is how it is done in the code that i am porting.
regards, Anuradha.
Frank Heckenbach wrote:
ANURADHA Srinivasan wrote:
Is typecasting like
MyType(Myvar);
not allowed?
Yes, GPC allows this as an extension.
How do i do something like the following?
p:pointer; mytype=record ..... end;
with mytype(p^) do begin ..... end;
Since you gave neither a complete sample or an error message, I don't know what's your problem. (I'm really tired of second-guessing.)
Frank
-- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/ GPC To-Do list, latest features, fixed bugs: http://agnes.dida.physik.uni-essen.de/~gnu-pascal/todo.html
-- S.Anuradha Generic Data Tools, Alcatel Chennai. Alcanet : 2-757-7123
Anuradha wrote:
It is not a typecasting error.It's probably fillchar question.I find the following sample program ending in a "segmentation fault -core dump" , i think because of doing a fillchar of a record which has a member that's a pointer .
program a; type myrec=record x:^integer; end; var ptr :^myrec;
begin getmem(ptr,sizeof(myrec)); fillchar(ptr^,sizeof(myrec),0); ptr^.x^:=5; write(ptr^.x^); end.
Well, sure it crashes. You set ptr^.x to nil (by filling it with 0's), and then dereference it (ptr^.x^). Dereferencing nil pointers is one the most obvious bugs one can make with pointers, and it crashes on any Pascal compiler (except BP under Dos which will instead overwrite the system IRQ tables ;-).
Please don't take the following personal, but I'd really suggest people not to use such low-level features (like GetMem, FillChar, etc.) unless they really understand what they do. Pascal is a high-level language and has plenty of "clean" features to use instead -- e.g., a simple `New (ptr)' instead of the GetMem line, and instead of the FillChar, either `ptr^.x := nil' or `New (ptr^.x)' -- depending on what you really mean (the latter will work correctly in the program above, the former will at least make it quite clear why it crashes).
Rant over. ;-)
Frank