Recent

Author Topic: TRY-EXCEPT doesnt work in fpc 3.0.0!!! [solved]  (Read 16552 times)

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #30 on: February 02, 2017, 11:47:18 am »
Im such a stupid Idiot sometimes,.........


Ofc he cant execute the code, because RangeCheckerror....


If i just write the "i" instead of "i*i it works even til 1000000 elements....


Now i get the error: (i*i) -> leads to more than the datatype integer by the value: 46341... Ofc thats more than integer is able to store...

Sometimes to solve a problem u need a rest and go out for a while to refresh the brain and then perhaps the solution comes along :D

Thanks still for your suggestions, commitments and help :)

And of course for your fast response, where i dont have to wait 1 week until someone post something :D
« Last Edit: February 02, 2017, 11:55:07 am by Magicus »

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!! [solved]
« Reply #31 on: February 02, 2017, 12:07:35 pm »
You should still check that f_capacity.

Although I don't get an error I still think that with
(f_arr_start + index)^ := Value;
where index = f_capacity, you access memory beyond what you reserved.

You do a block^.Data^.Start := GetMem(SizeOf(integer) * length);
So block^.Data^.Start itself is index 0 but your f_capacity is 1.
So you shouldn't do index <= f_capacity but index < f_capacity.

( or index <= f_capacity - 1 )

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!! [solved]
« Reply #32 on: February 02, 2017, 01:34:47 pm »
I did that :)

And all works fine :D

BUT:

I have another appeal to you guys^^:

Could you evaluate the unit i gave to you and say what could i do better, what should i avoid and so one...

Like: Do's and Dont's :)

Would be really nice when i could get a chance to learn good modelling from developer like you :)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #33 on: February 03, 2017, 12:03:12 am »
Im such a stupid Idiot sometimes,.........
Although that might perhaps be the case (i won't be the judge of that), realize that you are not the first (and certainly not he last) and fall for the same pitfalls as everyone else once did.

Also realize that (as shown in my example) using pointers is like driving without an instructor. It gives you lot of freedom but, what you do with that freedom is up to you. Use it wisely :-)

It doesn't matter if your code is shaky or otherwise needs to be kept in the dark but, especially when it comes to pointer arithmatic, be aware that things are very difficult to analyze without seeing some actual code.

Usually we are aware what it is you would like to achieve, and for sure i was not going to suggest using another approach (you wanted to learn about pointers so you can have it your way  :) ).

In hindsight, aren't you glad you shared your code ? It gave you the response you was looking for. Now we wasted a certain amount of posts, and those were initially adding to your frustration (which really is a bad starting point). Which btw does not mean, write a million lines of code, make it buggy and dump the whole lot on the forums with asking "where's Waldo"  ;D

Would be really nice when i could get a chance to learn good modelling from developer like you :)
rvk already did a very good job in explaning what did go wrong in your code

As of how you implemented things: There is no wrong and there is no right. In coding it all depends on what it is used for and what you'd like to do with your implementation. So judging your code would be something for the purists amongst us (i'm usually not one of them).

Truth be told, we have such nice classes/objects that encapsulate linked lists, doubly linked list and the sorts so that we never have to have a close look at those eeky pointers  :-X

Perhaps you can have a look in the sources of FPC and see for yourself how some things are implemented/solved ? I certainly learned a lot from that.

"Reading a unit from FPC source-tree a day keeps (embarrassing) questions on the forums away" :D
« Last Edit: February 03, 2017, 12:09:22 am by molly »

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!! [solved]
« Reply #34 on: February 03, 2017, 11:27:13 am »
I completely agree with molly.

Besides that... I had a very quick look at the code and feel it isn't finished.

What happens if I do this:
Code: Pascal  [Select][+][-]
  1. list := TDynamicArray.Create(50);
  2. list.Write(200, 1234);
In that case you want to write to 200.
You should create additional blocks but in the Write() I don't see anywhere where you write the Value after creating a block (let alone several blocks).

Have you tested it yourself already ??? For instance... the code below gives me this.... fun  %) %) %)
Before asking us to judge your code you should make sure it "kind of works"  ::)
Code: [Select]
0
1
2
3
4
5
6
7
8
9
10
1294385
23851156
0
-1163005939
-1163005939
-1163005939
-1163005939
-1163005939
-1163005939
-1163005939
done
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$H+}{$R+}
  3. uses mdynamicarray;
  4. var
  5.   list: TDynamicArray;
  6.   i: Integer;
  7. begin
  8.   list := TDynamicArray.Create(10);
  9.  
  10.   for i := 0 to 20 do
  11.     list.Write(i, i);
  12.  
  13.   for i := 0 to 20 do
  14.     writeln(list.Read(i));
  15.  
  16.   writeln('done');
  17.   readln;
  18. end.

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!! [solved]
« Reply #35 on: February 03, 2017, 12:41:09 pm »
First of all, thanks for your response:

1)
I should have post my code earlier :D, but i thought it could be solved without, but as u were correctly pointing out, dealing with questions about pointer-problems arn't really solveable without some code^^, got that.

2) Ok, i will take a deeper look in the fpc-sources :D, thanks by that.

And still thanks for all, even with perhaps the most annoying question in this forum :D :D

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!! [solved]
« Reply #36 on: February 03, 2017, 12:58:37 pm »
Oh, rvk, i will refactor the whole unit i think  :P :P

Your right, there still some problems in there, but that's how we learn^^

Thanks!

 

TinyPortal © 2005-2018