What is the purpose of these two lines : ? word(p):=sizeof(mytype); p:=pchar(p)+2;
Joe.
-----Original Message----- From: Anuradha [SMTP:anuradha@adcc.alcatel.be] Sent: Tuesday, October 30, 2001 8:54 AM To: gpc Subject: Re: Typecasting
Hello,
Sorry for the wrong sample program and a silly doubt.The problem is we have large codes already written and we work in the support department that converts these sources to executables runnable on unix and i cannot change much of the code. This time i have analysed more and am attaching a code that works fine in BP and doesnot work in GPC.It is something to do with the pointers. Can you kindly tell me what the problem is?
program typcast1; {type word=word(16);} type Mytype=record c:char; end; var p:pointer; begin getmem(p,sizeof(mytype)+2); word(p):=sizeof(mytype); p:=pchar(p)+2; mytype(p^).c:='h'; writeln(mytype(p^).c); end.
thanks and regards, Anu
Frank Heckenbach wrote:
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
-- 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
Hello,
I am really sorry for having missed a ^ in the program but you see , the program really worked in BP and not in GPC.But i do know that it is stupid to manipulate some parts of the pointer.kindly delete the previous mail. It is this program that i have problems in. ----------------- program typcast1; const ct :char='h'; type Mytype=record i : integer; c:^char; end; var p:pointer; begin getmem(p,sizeof(mytype)+2); fillchar(p^,sizeof(mytype)+2,0); word(p^):=sizeof(mytype); p:=pchar(p)+2; mytype(p^).c:=@ct; writeln(mytype(p^).c^); end. ------------------------
But if i change p:=pchar(p)+2; to p:=pchar(p)+4; then it works.Is it something to do with the word-boundaries?
Thanks and regards, Anuradha
Marco van de Voort wrote:
What is the purpose of these two lines : ? word(p):=sizeof(mytype);
In real mode TP it sets the offset of the segment:offset pair to 1.
Probably it originally was meant as a simple inc(p);
The problem it expects that the offset part of a newly allocated block is always zero, which is afaik not true. (real mode TP allocates per half segment).
p:=pchar(p)+2;
inc(p,2); or p:=@p[2];
The overall effect therefore is inc(p,3); or p:=pchar(p)[3];
-- S.Anuradha Generic Data Tools, Alcatel Chennai. Alcanet : 2-757-7123
What is the purpose of these two lines : ? word(p):=sizeof(mytype);
In real mode TP it sets the offset of the segment:offset pair to 1.
Probably it originally was meant as a simple inc(p);
The problem it expects that the offset part of a newly allocated block is always zero, which is afaik not true. (real mode TP allocates per half segment).
p:=pchar(p)+2;
inc(p,2); or p:=@p[2];
The overall effect therefore is inc(p,3); or p:=pchar(p)[3];
Anuradha wrote:
I am really sorry for having missed a ^ in the program but you see, the program really worked in BP and not in GPC.But i do know that it is stupid to manipulate some parts of the pointer.kindly delete the previous mail. It is this program that i have problems in.
program typcast1; const ct :char='h'; type Mytype=record i : integer; c:^char; end; var p:pointer; begin getmem(p,sizeof(mytype)+2); fillchar(p^,sizeof(mytype)+2,0); word(p^):=sizeof(mytype); p:=pchar(p)+2; mytype(p^).c:=@ct; writeln(mytype(p^).c^); end.
What are you trying to do? Why are you distorting the language? A const is a const, not a type (silly borland extension). EP and GPC have the value attribute if you have to initialize something, but it is better to write explicit code and not depend on extensions.
If you stick to the real language your code will be much clearer, and has a good chance of working.
From the little I see you would be advised to declare:
const ctv = 'h'; type Mytype = record i : integer; c : ^char; end;
recdptr = ^therecd; therecd = RECORD size : integer; content : mytype; end;
var p : recdptr;
ct : char;
.... ct = ctv; new(p); WITH p^, therecd DO BEGIN (* init ALL the fields *) size = sizeof(mytype); new(c); c^ = ctv; i = whatever; END;
Which should work both under real Pascal and under TPC/BP.