Sign in Join

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

Mailing lists / fpc-devel / 2007-03

porting from fpc to tp7

7 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Evgeniy Ivanov

Hi! I'm doing my tasks with fpc in linux, but in the university we have only tp, and there are several problems with bringing binaries. I have such code to have something like dynamic arrays:

procedure add_mem(var P: dynamic_array_ptr; var cur_size:Integer);
var
i:Integer;
buff_ptr:dynamic_array_ptr;
begin
  getmem(buff_ptr,cur_size*sizeof(BusStation) );
  for i:=1 to cur_size do
   buff_ptr^[i] := P^[i];
  freemem(P);
  cur_size:=cur_size+buf_count;
  getmem(P,cur_size*sizeof(BusStation) );
  for i:=1 to (cur_size-buf_count) do
   P^[i] := buff_ptr^[i];
  freemem(buff_ptr);
  cur_size:=cur_size+buf_count;
end;

In fpc it works. But in tp my programme sometimes starts from last time and have a part of last data and some garbage (not text). Why does it work in fpc, but doesn't in tp? I see the only way to rewrite code, but I want to know why.

Best regards E.I.

Reply by email

Daniël Mantione
to Evgeniy Ivanov

Op Sat, 24 Mar 2007, schreef Evgeniy Ivanov:

Hi! I'm doing my tasks with fpc in linux, but in the university we have only tp, and there are several problems with bringing binaries.

You have some negotiations to do :)

Show quoted text 22 lines
I have such code to have something like dynamic arrays: procedure add_mem(var P: dynamic_array_ptr; var cur_size:Integer); var i:Integer; buff_ptr:dynamic_array_ptr; begin getmem(buff_ptr,cur_size*sizeof(BusStation) ); for i:=1 to cur_size do buff_ptr^[i] := P^[i]; freemem(P); cur_size:=cur_size+buf_count; getmem(P,cur_size*sizeof(BusStation) ); for i:=1 to (cur_size-buf_count) do P^[i] := buff_ptr^[i]; freemem(buff_ptr); cur_size:=cur_size+buf_count; end; In fpc it works. But in tp my programme sometimes starts from last time and have a part of last data and some garbage (not text). Why does it work in fpc, but doesn't in tp? I see the only way to rewrite code, but I want to know why.

Why do you increase cur_size two times? At the end of the procedure cur_size is larger than the buffer is.

Further, you want to use move instead of a for loop, since in TP the speed difference is even larger than in FP.

Daniël

Reply by email

Evgeniy Ivanov
to Daniël Mantione
Daniël Mantione wrote 20 lines
> procedure add_mem(var P: dynamic_array_ptr; var cur_size:Integer); > var > i:Integer; > buff_ptr:dynamic_array_ptr; > begin > getmem(buff_ptr,cur_size*sizeof(BusStation) ); > for i:=1 to cur_size do > buff_ptr^[i] := P^[i]; > freemem(P); > cur_size:=cur_size+buf_count; > getmem(P,cur_size*sizeof(BusStation) ); > for i:=1 to (cur_size-buf_count) do > P^[i] := buff_ptr^[i]; > freemem(buff_ptr); > cur_size:=cur_size+buf_count; > end; Why do you increase cur_size two times? At the end of the procedure cur_size is larger than the buffer is.

Ou, surely. But it is new bug. When I used tp there was'n second increase. Also buf_count value is 10 and I have tried to add only 5 elements. After second startup the problems have begun.

Further, you want to use move instead of a for loop, since in TP the speed difference is even larger than in FP.

Sorry, but I didn't understand what you ment exactly. You ment to use ptr_buf = P and then getmem(P) and for loop?

Best regards E.I.

Reply by email

Daniël Mantione
to Evgeniy Ivanov

Op Sat, 24 Mar 2007, schreef Evgeniy Ivanov:

Show quoted text 28 lines
On Sat, 24 Mar 2007 13:05:54 +0300, Daniël Mantione wrote: > > procedure add_mem(var P: dynamic_array_ptr; var cur_size:Integer); > > var > > i:Integer; > > buff_ptr:dynamic_array_ptr; > > begin > > getmem(buff_ptr,cur_size*sizeof(BusStation) ); > > for i:=1 to cur_size do > > buff_ptr^[i] := P^[i]; > > freemem(P); > > cur_size:=cur_size+buf_count; > > getmem(P,cur_size*sizeof(BusStation) ); > > for i:=1 to (cur_size-buf_count) do > > P^[i] := buff_ptr^[i]; > > freemem(buff_ptr); > > cur_size:=cur_size+buf_count; > > end; > > Why do you increase cur_size two times? At the end of the procedure > cur_size is larger than the buffer is. Ou, surely. But it is new bug. When I used tp there was'n second increase. Also buf_count value is 10 and I have tried to add only 5 elements. After second startup the problems have begun.

Well, all I can say is check your code securely. The basic idea is good, so it is likely the implementation.

Show quoted text 5 lines
> Further, you want to use move instead of a for loop, since in TP the > speed difference is even larger than in FP. Sorry, but I didn't understand what you ment exactly. You ment to use ptr_buf = P and then getmem(P) and for loop?

Instead of:

for i:=1 to cur_size do
  buff_ptr^[i] := P^[i];

... do:

move(p^[1],buff_ptr^[1],(cur_size-1)*sizeof(p^);

Also, is your array really 1-based?

Daniël

Reply by email

Evgeniy Ivanov
to Daniël Mantione
Daniël Mantione wrote 29 lines
> > > procedure add_mem(var P: dynamic_array_ptr; var cur_size:Integer); > > > var > > > i:Integer; > > > buff_ptr:dynamic_array_ptr; > > > begin > > > getmem(buff_ptr,cur_size*sizeof(BusStation) ); > > > for i:=1 to cur_size do > > > buff_ptr^[i] := P^[i]; > > > freemem(P); > > > cur_size:=cur_size+buf_count; > > > getmem(P,cur_size*sizeof(BusStation) ); > > > for i:=1 to (cur_size-buf_count) do > > > P^[i] := buff_ptr^[i]; > > > freemem(buff_ptr); > > > cur_size:=cur_size+buf_count; > > > end; > > > > > Why do you increase cur_size two times? At the end of the procedure > > cur_size is larger than the buffer is. > Ou, surely. But it is new bug. When I used tp there was'n second > increase. > Also buf_count value is 10 and I have tried to add only 5 elements. > After > second startup the problems have begun. Well, all I can say is check your code securely. The basic idea is good, so it is likely the implementation.

I will try. May be there is some problem with index and memory in tp and dos (dos emulation in NT) is unsecure.

Show quoted text 15 lines
> > Further, you want to use move instead of a for loop, since in TP the > > speed difference is even larger than in FP. > > Sorry, but I didn't understand what you ment exactly. You ment to use > ptr_buf > = P and then getmem(P) and for loop? Instead of: for i:=1 to cur_size do buff_ptr^[i] := P^[i]; ... do: move(p^[1],buff_ptr^[1],(cur_size-1)*sizeof(p^);

Thanks, I will try.

Also, is your array really 1-based?

Hm... I didn't understand. I have such definitions of my array:

type dynamic_array = array[1..buf_count] of BusStation;  //BusStation is
record with 4 strings
dynamic_array_ptr = ^dynamic_array;
var
list:dynamic_array_ptr;  // it's my "dynamic array" with its size counter
list_count:Integer;
...
// How I use it:
for i:=1 to num_of_strings do
   begin
   if i > list_count then
      add_mem(list,list_count);

I herd that there were problems with dos games: sometimes they started not from the beginning, but from last time ending.

Best regards E.I.

Reply by email

Daniël Mantione
to Evgeniy Ivanov

Op Sat, 24 Mar 2007, schreef Evgeniy Ivanov:

> Well, all I can say is check your code securely. The basic idea is good, > so it is likely the implementation. I will try. May be there is some problem with index and memory in tp and dos (dos emulation in NT) is unsecure.

Note that in Dos, the memory at program start is in random state; in other words, if your program contains bugs like uninitialized memory, it can have different behaviour between runs.

Show quoted text 19 lines
> Also, is your array really 1-based? Hm... I didn't understand. I have such definitions of my array: type dynamic_array = array[1..buf_count] of BusStation; //BusStation is record with 4 strings dynamic_array_ptr = ^dynamic_array; var list:dynamic_array_ptr; // it's my "dynamic array" with its size counter list_count:Integer; ... // How I use it: for i:=1 to num_of_strings do begin if i > list_count then add_mem(list,list_count);

This is okay if everything is 1-based. If it is 0-based, you need to be carefull in getmem to allocate memory for the 0th element too.

I herd that there were problems with dos games: sometimes they started not from the beginning, but from last time ending.

No idea.

Daniël

Reply by email

Evgeniy Ivanov
to Daniël Mantione
Daniël Mantione wrote 5 lines
Note that in Dos, the memory at program start is in random state; in other words, if your program contains bugs like uninitialized memory, it can have different behaviour between runs.

It is the thing that I was talking about (dos games problem). So you answered on my question. The differences between fpc and tp is in memory allocation. TP gets random state, and it may be unclean from last time, and fpc returns mem to OS (and it's clean) and on the next startup I have clean memory. I have somewhere some extras pointers and in fpc they point to free mem ('' strings) so I don't see the bad result of printing and in tp I see them.
Daniël, thank you for help! Now I understood the root of the problem.

This is okay if everything is 1-based. If it is 0-based, you need to be carefull in getmem to allocate memory for the 0th element too.

I will consider it.

Best regards E.I.

Reply by email