Recent

Author Topic: Mysterious problems with a linear search.  (Read 10013 times)

benwrcoder

  • New member
  • *
  • Posts: 7
Mysterious problems with a linear search.
« on: January 29, 2016, 12:19:31 pm »
Code: Pascal  [Select][+][-]
  1. function findPID_real(target:integer):integer;
  2. var
  3.   x,idgotten:integer;
  4. begin
  5.   findPID_real:=-1;
  6.   for x:= 0 to mainlist.count-1 do
  7.   begin
  8.      idgotten:=mainList[x].GetID;
  9.      showmessage('target='+IntToStr(target));
  10.      showmessage('GetID '+IntToStr(x)+'= '+IntToStr(mainlist[x].GetID));
  11.      if ( idgotten = target) then
  12.         findPID_real:=target
  13.  
  14.  
  15.   end;
  16. end;    

The showmessages say that the ID is anywhere from 100,000 to 600,000, when the only entries are 1,2,3,4,5,6,7,9

Has anyone ever encountered somethin similar?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Mysterious problems with a linear search.
« Reply #1 on: January 29, 2016, 01:09:27 pm »
Of course. Every programmer writes buggy and mistake-ridden code (inadvertently, of course).
Perhaps the first place to look for the problem is the GetID routine.

benwrcoder

  • New member
  • *
  • Posts: 7
Re: Mysterious problems with a linear search.
« Reply #2 on: January 29, 2016, 01:27:16 pm »
Thanks for the quick reply, but getID is a simple one-line getter

Code: Pascal  [Select][+][-]
  1. Function Poultryobj.GetID():integer;
  2. begin
  3.          GetID:=ID;
  4. end;          

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: Mysterious problems with a linear search.
« Reply #3 on: January 29, 2016, 01:39:09 pm »
I think it is a good idea to write a little compilable example and place it here.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9865
  • Debugger - SynEdit - and more
    • wiki
Re: Mysterious problems with a linear search.
« Reply #4 on: January 29, 2016, 04:52:07 pm »
Code: Pascal  [Select][+][-]
  1. function findPID_real(target:integer):integer;
  2.   for x:= 0 to mainlist.count-1 do
  3. ...
  4.      if ( idgotten = target) then
  5.         findPID_real:=target
  6. end;    

I dont know if it is related to your issue, but the last line probably is meant to be
Code: Pascal  [Select][+][-]
  1.      if ( idgotten = target) then
  2.         findPID_real:= x;
  3.  

Currently you return "target" (the value of the PID, not it's index), that makes no sense as you already know it (you passed it in).



benwrcoder

  • New member
  • *
  • Posts: 7
Re: Mysterious problems with a linear search.
« Reply #5 on: February 10, 2016, 10:20:46 am »
I realised that it was passing the wrong number, thanks, but the bug is still there.

I can't really upload a "small executable" because A) This is the only place in the entire program where the bug occurs, and B) This is part of a piece of coursework and I'd really like it to not be on the web in entirety.

More info:

mainlist[whatever].GetID is used in other places and returns the real values just fine there. However, I have an identical procedure using a different list, and it experiences the same problem

It's not a bug with the IntToStr , since
Code: Pascal  [Select][+][-]
  1.      if( mainlist[x].getID>1000)then
  2.          showmessage('Very bad!');    
triggers the condition,

when IntToStr(x) is returned, x is exactly what it should be

and it's not to do with the For loop, since the same issue occurs after changing it to a Repeat Until loop

Indexmainlist[index].getID
01
12
23
34
45
56
67
78
89
911
1012
« Last Edit: February 10, 2016, 10:53:20 am by benwrcoder »

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: Mysterious problems with a linear search.
« Reply #6 on: February 10, 2016, 05:03:53 pm »
Show how you fill mainlist

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Mysterious problems with a linear search.
« Reply #7 on: February 10, 2016, 05:39:39 pm »
This might do the trick.
Now on discount.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Nitorami

  • Sr. Member
  • ****
  • Posts: 496
Re: Mysterious problems with a linear search.
« Reply #8 on: February 10, 2016, 06:45:20 pm »
benwrcoder

You are stealing people's time by your secretiveness about the code, and you seem to insinuate there is a bug in the compiler others have encountered before. Of course this cannot entirely be excluded but the problem is much more likely on your side. If you want help, you really should provide a small working program which demonstrates the problem.

Other than that, we can only guess, and the only advice I have is to compile the program will range check etc. set to ON, to check whether you have a range overflow somewhere.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Mysterious problems with a linear search.
« Reply #9 on: February 10, 2016, 07:02:34 pm »
Put a watch on the respective variable then run your program step by step. I can't debug what I can't see and examine.

benwrcoder

  • New member
  • *
  • Posts: 7
Re: Mysterious problems with a linear search.
« Reply #10 on: February 22, 2016, 01:12:29 pm »
I tried to upload the code but the forum complained it exceeded the 20,000 character limit

Nitorami

  • Sr. Member
  • ****
  • Posts: 496
Re: Mysterious problems with a linear search.
« Reply #11 on: February 22, 2016, 02:25:03 pm »
Well, what a luck, because who would want to read through that ?
It would be most useful of you try to strip the problem down, by removing unnecessary procedures one by one until a comprehensive rest remains which still shows the problem. Not > 20.000 characters please.

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Mysterious problems with a linear search.
« Reply #12 on: February 22, 2016, 03:31:37 pm »
That why the forum support compressed files.
also you can link to external site like http://pastebin.com/.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Mysterious problems with a linear search.
« Reply #13 on: February 22, 2016, 03:46:51 pm »
That why the forum support compressed files.
also you can link to external site like http://pastebin.com/.
Well even if he provides the full source, I wouldn't want to check that >20k chars code. He must be able to trim down the program while maintaining the problem he describes.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Mysterious problems with a linear search.
« Reply #14 on: February 22, 2016, 07:26:09 pm »
He must be able to trim down the program while maintaining the problem he describes.
And while doing that he might even find the problem himself  :)
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018