Board index » delphi » Delphi3: Works when stepping but not when running

Delphi3: Works when stepping but not when running

I have this function which is supposed to return a real. When I run my
application it returns 0 every time, except the first time, where it
returns the right value. When I step my way through the application (F7)
the function returns the right value every time. I have tried to insert
some breakpoints when ever the function is called, but it still returns 0.

This thing goes beyond my mind, so I would really appreciate some help on
this one.

Thanks in advance

 

Re:Delphi3: Works when stepping but not when running


On 17 Feb 1998 20:19:08 GMT, "Bent Jensen" <b...@post6.tele.dk> wrote:

Quote
>I have this function which is supposed to return a real. When I run my
>application it returns 0 every time, except the first time, where it
>returns the right value. When I step my way through the application (F7)
>the function returns the right value every time. I have tried to insert
>some breakpoints when ever the function is called, but it still returns 0.

>This thing goes beyond my mind, so I would really appreciate some help on
>this one.

That sounds interesting. Would you please post the text of the
function here, so we may diagnore the problem?

Re:Delphi3: Works when stepping but not when running


---------------------------------------------------------------
This is the function I call
---------------------------------------------------------------

Function Vali(streng:string):real;
var
 ix,ix2:integer;
 temp:string[10];
 tal:real;
 ok:integer;
begin
 ix2:=1;
 for ix:=1 to 10 do
  if streng[ix]<>'.' then
   Begin
    temp[ix2]:=streng[ix];
    inc(ix2);
   end;
  val(trim(temp),tal,ok);
  Vali:=tal;

end;

-----------------------------------------------
This is where I call it from
------------------------------------------------
   REPEAT  
     READLN(mytextfile,streng);
     .
     .

     for ix:=15 to 24 do
     nummer[ix-14]:=streng[ix];
     omstmp.oms[1]:=vali(nummer);
     for ix:=28 to 37 do
     nummer[ix-27]:=streng[ix];
     omstmp.oms[2]:=vali(nummer);
     .
     .
     .
  UNTIL eof(mytextfile)

The 2 pieces of code are from 2 different units, but I have also tried to
have them in the same, and that didn't work either.

What it is all intended to do is to take some numbers from a text file.
remove the dots (.) that seperates thousands and then validate them into a
real.

The first time the first loop runs (For IX:=15 to 24) the VALI-function
returns the right value. When the other loops run, the VALI-function
returns 0 every time. The second time  the REPEAT loop runs and hereafter
it also returns 0 in the first loop.

BUT NOT WHEN I TRACE MY WAY THROUGH.
ONLY WHEN I RUN THE DAMN PROGRAM!!!

Re:Delphi3: Works when stepping but not when running


In article <01bd3bea$dc321e20$0900005a@overkill>, "Bent Jensen"

Quote
<b...@post6.tele.dk> wrote:
>---------------------------------------------------------------
>This is the function I call
>---------------------------------------------------------------

>Function Vali(streng:string):real;
>var
> ix,ix2:integer;
> temp:string[10];
> tal:real;
> ok:integer;
>begin
> ix2:=1;
> for ix:=1 to 10 do
>  if streng[ix]<>'.' then
>   Begin
>    temp[ix2]:=streng[ix];
>    inc(ix2);
>   end;
>  val(trim(temp),tal,ok);
>  Vali:=tal;

>end;

I don't think that these are the reason for your problem, but a few
suggestions:

a) Don't use "Real", Double is better since Real is not CPU-supported
b) Instead of temp[ix2] := streng[ix] do temp := temp + streng[ix].
your implementation might cause trouble with the length-byte (IMO).

However, I'll have a closer look on it...

Andreas

--
helicon software development | Localization Tool for Borland Delphi 3
heli...@helicon.co.at        | helicon Translator for Delphi 3
http://www.helicon.co.at     | http://www.helicon.co.at/translator3

Re:Delphi3: Works when stepping but not when running


In article <01bd3bea$dc321e20$0900005a@overkill>, "Bent Jensen"

Quote
<b...@post6.tele.dk> wrote:
>---------------------------------------------------------------
>This is the function I call
>---------------------------------------------------------------

>Function Vali(streng:string):real;
>var
> ix,ix2:integer;
> temp:string[10];
> tal:real;
> ok:integer;
>begin
> ix2:=1;
> for ix:=1 to 10 do
>  if streng[ix]<>'.' then
>   Begin
>    temp[ix2]:=streng[ix];
>    inc(ix2);
>   end;
>  val(trim(temp),tal,ok);
>  Vali:=tal;

>end;

Hi,

Ok this seems to work:

=== SOURCE BEGIN===
program strange;

{$APPTYPE CONSOLE}

uses
  SysUtils;

{$R *.RES}

Function Vali(streng:string):real;
var
 ix : Integer;
 temp:string[10];
 tal:real;
 ok:integer;
begin
  try
    temp := '';
    for ix := 1 to Length (streng) do
    begin
      if (streng[ix] in ['0'..'9','+','-']) then
      begin
        temp := temp + streng[ix];
      end;
    end;
    Result := StrToFloat (Temp);
  except
    On Exception do result := 0;
  end;
end;

VAR dummy : String;

begin
  Writeln (Vali ('1234567890'):11:2);
  Readln (Dummy);
end.
=== SOURCE END ===

Hope this helps

Andreas

--
helicon software development | Localization Tool for Borland Delphi 3
heli...@helicon.co.at        | helicon Translator for Delphi 3
http://www.helicon.co.at     | http://www.helicon.co.at/translator3

Re:Delphi3: Works when stepping but not when running


The problem is you don't initialize your string variables both in Vali function
(temp) and in the caller function (nummer).This could be done in two ways:
  - fill the whole string with a char ( #0 for ex.) and then assign the first byte the
   length of the string, as declared ( str[0] := LengthOfString )  if you want to
   access (write) it  like an array , or, better,
  - make the string null ( str :=  '' ) and then add chars one by one
  (something like :  str := str + somechar);
 Working with  non-initialized variables leads to such a strange behavior.

Regards,
Florian

Re:Delphi3: Works when stepping but not when running


My apologies for the delay, but I've been rather busy and haven't had
the time to keep up with the newsgroups.

On 17 Feb 1998 21:23:04 GMT, "Bent Jensen" <b...@post6.tele.dk> wrote:

Quote
>---------------------------------------------------------------
>This is the function I call
>---------------------------------------------------------------

>Function Vali(streng:string):real;
>var
> ix,ix2:integer;
> temp:string[10];
> tal:real;
> ok:integer;
>begin
> ix2:=1;
> for ix:=1 to 10 do
>  if streng[ix]<>'.' then
>   Begin
>    temp[ix2]:=streng[ix];
>    inc(ix2);
>   end;
>  val(trim(temp),tal,ok);
>  Vali:=tal;

>end;

The problem is the initialization of "temp". When the function is
called, space for "temp" is allocated in memory, and you have no way
to tell what the contents of that space in memory might be. Since
"streng" rarely (if ever) contains ten digits, part of "temp" is never
initialized, and retains the values found in the memory space
allocated to it. While you do try to "trim(temp)", that does not work,
because the "trim" function only removes control characters up to the
first keyboard character.

Example:

streng = '1.234.500'
temp (when the function is called) = 'abcdefghij'

After having checked the characters,

temp = '1234500hij'

and

trim(temp) = '1234500hij'

which means that the trim operation didn't accomplish what you wanted
it to do.

In such a case, converting Temp to a value will cause an error, and
the result will be that Vali = 0 and ok = ix2+1, indicating that the
conversion ran into a problem on the character
temp[xi2+1]. You can check that, if you like.

The reason why the value turns out OK when you "step" through the code
is that, with 'temp' in your watch list, Delphi FORCES an
initialization of temp, which means that when you reach the first line
in the function, temp = 'xxxxxxxxxx' where every x is actually an
ascii zero, which is successfully removed by the trim operation.

- Show quoted text -

Quote
>-----------------------------------------------
>This is where I call it from
>------------------------------------------------
>   REPEAT  
>     READLN(mytextfile,streng);
>     .
>     .

>     for ix:=15 to 24 do
>     nummer[ix-14]:=streng[ix];
>     omstmp.oms[1]:=vali(nummer);
>     for ix:=28 to 37 do
>     nummer[ix-27]:=streng[ix];
>     omstmp.oms[2]:=vali(nummer);
>     .
>     .
>     .
>  UNTIL eof(mytextfile)

About your code: It's often easier to use the Copy command instead of
assigning a string character-by-character. Also, I tend to be careful
when working with files, and would much rather use a while...do loop,
instead of the repeat...until. If you use repeat..until and the file
you're accessing happens to be empty, your program is going to crash
when it tries to read a record that doesn't exist. A while..do won't,
as you can see from the following example.

while not EOF(MyTextFile) do
begin
        ReadLn(MyTextFile,Streng);
        ...
        Nummer := Copy(Streng,15,10);
        OmsTmp.Oms[1] := Vali(Nummer);
        Nummer:=Copy(Streng,28,10);
        OmsTmp.Oms[2]:=Vali(Nummber);
        ...
end;

As for the Vali function:

function Vali(const S: string): double;
var
        Temp: string;
        N: double;
        Code: integer;
begin
        Temp := S;
        for I:=1 to Length(S) do
            if S[I] = '.' then
                Delete(Temp,I,1);
        Val(Temp,N,Code);
        Vali := N
end;

In this case, Vali will only return zero if the string passes to this
function contained a character that was neither a digit (0-9) nor a
period (.).

Good luck.

Re:Delphi3: Works when stepping but not when running


Quote
In article <34ec603a.11695...@news.romania.eu.net>, nospam_flor...@radiotel.ro (Florian) wrote:
>The problem is you don't initialize your string variables both in Vali function
>(temp) and in the caller function (nummer).This could be done in two ways:
>  - fill the whole string with a char ( #0 for ex.) and then assign the first
> byte the length of the string, as declared ( str[0] := LengthOfString )  if you want to
>   access (write) it  like an array , or, better,
>  - make the string null ( str :=  '' ) and then add chars one by one
>  (something like :  str := str + somechar);
> Working with  non-initialized variables leads to such a strange behavior.

Delphi 3 picks up on these possibilities, too, as long as Hints and Warnings
are checked-off in the Project options.  I've noticed that quite a few people
seem to ignore the hints and warnings even when they do have them turned on -
but Delphi typically doesn't give them spuriously (though I know some people
would like a way to turn off the "blabla is declared but not used" hint - I
don't, though; I like making sure I only have the variables I need :)

  -- Ritchie Annand

Re:Delphi3: Works when stepping but not when running


[This followup was posted to comp.lang.pascal.delphi.misc and a copy was
sent to the cited author.]

In article <01bd3be1$edbb5700$0900005a@overkill>, b...@post6.tele.dk
says...

Quote
> I have this function which is supposed to return a real. When I run my
> application it returns 0 every time, except the first time, where it
> returns the right value. When I step my way through the application (F7)
> the function returns the right value every time. I have tried to insert
> some breakpoints when ever the function is called, but it still returns 0.

> This thing goes beyond my mind, so I would really appreciate some help on
> this one.

> Thanks in advance

I ran into a similar kind of problem with development in Delphi 2.  My
solution was to get hold of a utility that would allow debugging
statements to be written to a debugging window as the program was
running.  I currently use Gexperts which can be found either on DSP or
Torry's delphi pages.

This way I could let the program run flat out and watch the appropriate
results appear without the interference that seems to come from setting
breakpoints in the affected regions.  It was the only way I was able to
locate the bugs in the code.

--
remove the ".nothere" from my email address if you want
to email me

gtasker.noth...@globec.com.au

If Christians are good for Jesus,
   Bhuddists are good for Bhuddha,
   Moslems are good for Allah,
does that mean that
   Athiests are good for nothing?

Other Threads