Recent

Author Topic: Linked List Class  (Read 27794 times)

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Linked List Class
« Reply #15 on: March 21, 2012, 10:39:07 pm »

You managed to create a working program with no memory leaks.
That is an accomplishment in itself  :)

Thank you - but let me assure you there were several along the way:)

Quote
A useful next step would be to make use of what is available in the 'Lazarus toolbox'.
Instead of building a linked list (what you probably have done many times before) you could use a TObjectList (unit contnrs) to store the data.
Make a class of ItemRec with it's own constructor and destructor and the methods that do all data handling.

That makes a lot of sense.  I'll look at that at that as furthering my education :)

Quote
Indeed.  Most time is spent reading code, not writing it.
And the easier it is to read, the easier it is to understand (and spot possible errors or better, assess that the code is indeed correct).

While I agree with you but will make the point that I am coding for myself, and I will be the one who will be reading it the most (looking for errors).  It makes sense to me that I should code it in a way that I will find the most readable. I find the Delphi/Lazarus standard to be irritating.

Quote
'Data' is merely a placeholder to find the first memory location after the variables prev, next and ilen.
The type is irrelevant.

Exactly

Quote
Actually you declared a dynamic array which is not the most obvious choice;

To me it is an obvious choice.  The dynamic nature helps to remind me that its not a Static type that I may specifically use with the specific type specified.  (Probably just another way my twisted mind works)

Quote
TLinkedList.Curr should be declared as a local variable in the methods that use it, not an object attribute. It does not contain any information about the state of an object.

As the programs stands you are correct.  At the time of writing I was considering the option of not copying the data back to the application, but allowing the programmer access the current Item by this pointer.  Curr always points to the current item or is nil.  But you are correct because of a change I made (see comment on ItemSize)

Quote
TLinkedList.ItemSize should not be there because of the flexible way the list is built: the sizes of the elements in the linked list can all be different.

Initially ItemSize was a fixed size (e.g. the maximum size an item could be).  I changed this because I thought it maybe possible that my Move Data could get an access violation in some circumstances.  Having made this change, it then made it impractical to allow access via Curr as the programmer may inadvertently change the size of Data and cause an access violation, or something similar.

Thank you for your feedback.  I am feeling a lot more confident to continue with my learning.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Linked List Class
« Reply #16 on: March 22, 2012, 04:57:22 am »
Even if you're not using OOP, you can't define private types in implementation section. You can have an object instance of a class in implementation var section.

Quote
The problem with that is that the actual data disappears when 'item' has been assigned a new value for the next AddItem = the initial Item will now point to the new value, not the value it had at the time it was added.  Even worse, if the item is a long string, that pointer may now be to an unallocated part of memory and thus cause our favourite SIGSEGV error.  Sorry, but the data needs to be copied to the allocated memory and 'array of char' is perfect for copying unknown data types.
(Firstly, not saying you should do this, but just in general.)
That is not entirely true, but depends on the data types. I know string in this case would be problematic, but still possible. Maybe allocate PChar or something? If you create an object, or allocate memory in a procedure and don't free it, it will remain in the memory forever. Imagine a record that has 1 million bytes of data, pointer references and links to class objects. The list could have 100 of these records. If you regard them as pointers, the speed gain is huge. The time it takes to simply swap pointers instead of doing all memory copying, is much faster, especially on unknown data types.


I am curious as to what type of programs you have written where the values of variables don't change!

If that is the case, then these programs would not benefit by my control.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Linked List Class
« Reply #17 on: March 22, 2012, 04:49:54 pm »
I find the Delphi/Lazarus standard to be irritating.
If you are used to a different coding standard, it takes some getting used to  :D

On a sidenote: the Delphi/FPC standards are merely extensions of the older pascal coding standards.
I still own an original Turbo Pascal V3 manual and a couple of other Pascal books of the mid 80's where you can see this exact same coding standard being used.

Good luck in your OOP endeavours.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Linked List Class
« Reply #18 on: March 22, 2012, 04:56:46 pm »
Quote
I find the Delphi/Lazarus standard to be irritating
Well it's a matter of preference actually, but still required in a project where a lot of developers involved. Honestly, Lazarus coding standard is better than Free Pascal coding standard for me, as it's closer to the one issued by Embarcadero.

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Linked List Class
« Reply #19 on: March 24, 2012, 09:05:42 am »
I agree with the comments that formatting is a matter of personal preference/style.  I also agree that if working on a a development team, the team all needs to use the same style.

A reference to Turbo Pascal style from the 80's does not make the current convention 'more' acceptable.

I worked for IBM for 10 years using PL/1 (very Pascal like) - For another 10 years I was a contract programmer.  I used the standards they taught me and that were used in all the installations that I contracted to.  The standards I use today are ones taught to me by the then largest computer company in the world.

I don't object to people/companies having different conventions - in fact its simple logic, if I want to join their team, I will need to adapt.  By the same token, if people want something FREE from me, they will need to adapt.  OR - they can simply put my code through one of the many Pascal formatters that will reformat the code to their ugly (in my eyes)  preferred choice.

There is no comment on this forum that could induce me to change the formatting convention that I use.

I state again, I program for myself, and I am the one that has to read it over and over again to find any problem.  I assure all, I am not a masochist, I write code that is the most readable (and debugable) to me. Code  produced using the existing Delpi/Lazarus standard is far from readable (to me).  I can give small simple code snippets that highlight this, but the bias from some here would not be able to accept it.

I wish you all well, and thank all that helped me so much on my entry into OOP.  The help here got me over the first little hurdle - I am sure that I have more to come.

Thanks all.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Linked List Class
« Reply #20 on: March 24, 2012, 10:11:25 am »
I worked for IBM for 10 years using PL/1 (very Pascal like) - For another 10 years I was a contract programmer.  I used the standards they taught me and that were used in all the installations that I contracted to.  The standards I use today are ones taught to me by the then largest computer company in the world.

If you mean language like this, page 289:
http://publibfp.boulder.ibm.com/epubs/pdf/i1191451.pdf
I'd say that is just scripting on a completely different level than what pascal as language is  :)  By that language, while loop would look like this:
Code: [Select]
while a<10 do begin
a:=a+1;
end;
But also, the lines after the beginning line of a function go straight into the code contents, so naturally they indent straight away. I feel like you are still coding pascal in just your own way, just your adaptation.

Also, pascal is basically following indent rules that other programming languages do too. If you program with C/C++, Java etc, you will notice that pascal indentation is the same.

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Linked List Class
« Reply #21 on: March 24, 2012, 10:18:04 am »
Basically, you have no idea what you are talking about. You are attempting to justify a personal choice based on NO logic.  Thank you for the useless comment.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Linked List Class
« Reply #22 on: March 24, 2012, 12:18:37 pm »
You said IBM PL/1 programming style so that's exactly what i dig up. You tried to justify disregarding pascal style by that, so i wanted to see if that's actually true. Why would IBM code in a way that makes no logical sense. And this is not meant to offend your way of coding at all, i just find it illogical. A big corporation would do everything in its power to make a code as readable as possible for its employees. What i was guessing is that the scripting language is simpler, not comparable to pascal at all.

Quote
You are attempting to justify a personal choice based on NO logic.
I don't know what you mean, but i'll just give up.

Quote
Thank you for the useless comment.
No problem.

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Linked List Class
« Reply #23 on: March 25, 2012, 05:38:37 am »
You said IBM PL/1 programming style so that's exactly what i dig up. You tried to justify disregarding pascal style by that, so i wanted to see if that's actually true. Why would IBM code in a way that makes no logical sense. And this is not meant to offend your way of coding at all, i just find it illogical. A big corporation would do everything in its power to make a code as readable as possible for its employees. What i was guessing is that the scripting language is simpler, not comparable to pascal at all.

1.  PL/1 is NOT a scripting language. It is a multiple pass, optimizing compiler, that IBM used a variant of to write their own operating systems.

2.  There is nothing in the link you provided to even remotely suggest that PL/1 is a scripting language.

3.  Yes, a big company like that does all in its power to make code as readable as possible.  They achieved it, and all these years later I still use it.  It works great for me. 

To suggest that a convention that is different to the one you use is unreadable/makes no logical sense/ugly/etc is taking your own personal opinion and imposing it as FACT is nothing but arrogant.  I have never criticised Delphi/Lazarus convention beyond calling it irritating TO ME.

I thank you for your feedback but you have said nothing that would tempt me to a less readable format (for me) .

If you wish to actually see some of the PL/1 history, Wiki is your friend: try:
http://en.wikipedia.org/wiki/PL/I#Sample_programs
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Linked List Class
« Reply #24 on: March 25, 2012, 10:19:43 pm »
Look forward to any comments, suggestions etc.
It will be more productive for yourself if you would not fight every comment that is made but go with the flow from time to time.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Linked List Class
« Reply #25 on: March 26, 2012, 05:31:50 am »
It will be more productive for yourself if you would not fight every comment that is made but go with the flow from time to time.

Thank you for the feedback, however sweeping generalisations like: "if you would not fight every comment" are unjustified.  If you bother to check this thread again you will see that I have been very grateful for genuine assistance provided and taken several suggestions on board.  However I do draw the line with people that state personal opinion as fact, particularly when its done to attack.

What I have learned from this experience is that if I don't comply to the Delphi/Lazarus coding conventions there is a minority here that will attack, rather than providing any worthwhile assistance.

Should I require assistance from this forum in the future I will try to remember to use a formatter to so that I can present my problem in a way that is pleasing to the eye of the single minded minority.

Best Regards
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Linked List Class
« Reply #26 on: March 26, 2012, 09:47:07 am »
What I have learned from this experience is that if I don't comply to the Delphi/Lazarus coding conventions there is a minority here that will attack, rather than providing any worthwhile assistance.
What I learned in several internet fora is that a minority is dumb/lazy/arrogant/aggressive anyway and will attack/cloud the issue etc.  However, I suspect this set of people is different for each of us: some people just rub you the wrong way.

Additionally, English is not the native language of all of us, and we're from different cultures (with various levels of politeness/directness).

To me, it pays off to assume people mean the best, and if I can't learn from their contributions, I ignore them.

Should I require assistance from this forum in the future I will try to remember to use a formatter to so that I can present my problem in a way that is pleasing to the eye of the single minded minority.

Well... that actually might be a good idea.
Why? Because people learning Object Pascal (like me) do it from the Delphi documentation/snippets on the internet/forum posts etc.
De facto, there is more or less a standard coding formatting style that might indeed be ugly to some people, but it is a style that most of us are used to.
Presenting your code in this format may lower the difficulty people have understanding it and therefore increase the chances of useful feedback.
Who knows though, your current style might be perfectly comprehensible to others as well...

Hoping to see you here on the forums and hopefully on the wiki a lot - both being helped and helping others.

Thanks,
BigChimp
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Linked List Class
« Reply #27 on: March 26, 2012, 11:03:57 am »
What I learned in several internet fora is that a minority is dumb/lazy/arrogant/aggressive anyway and will attack/cloud the issue etc.  However, I suspect this set of people is different for each of us: some people just rub you the wrong way.
Wow = you got that right.

Curious now is 'internet fora' the plural of Forum = as in Forums? Please dont misunderstand, just asking as I haven't seen 'fora' before.
Quote
Additionally, English is not the native language of all of us, and we're from different cultures (with various levels of politeness/directness).

I do understand this one.  I have traveled extensively and have come across this frequently.  I believe that most of the time I can recognise an attack versus a helpful suggestion.

Quote
To me, it pays off to assume people mean the best, and if I can't learn from their contributions, I ignore them.

I like to acknowledge and thank people for their suggestions and ideas, even if not helpful - i just get annoyed at people who obviously are too inexperienced to help find ways to attack the author so that they can justify their lack of knowledge by attacking.


Quote
Well... that actually might be a good idea.
Why? Because people learning Object Pascal (like me) do it from the Delphi documentation/snippets on the internet/forum posts etc.
De facto, there is more or less a standard coding formatting style that might indeed be ugly to some people, but it is a style that most of us are used to.
Presenting your code in this format may lower the difficulty people have understanding it and therefore increase the chances of useful feedback.

Of course, people will not get to see a format that is even more readable than Delphi standard and endorsed by a far larger company - IBM.

Quote
Who knows though, your current style might be perfectly comprehensible to others as well...
See above ...  Probably far more users use it than Delphi/Lazarus users

Quote
Hoping to see you here on the forums and hopefully on the wiki a lot - both being helped and helping others.

Thank you for your encouraging words BigChimp. 

I think people should realise that when a user is being frustrated by a coding issue, the last thing they need to hear is that there was 2 extra spaces before a 'Begin'




Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Linked List Class
« Reply #28 on: March 26, 2012, 12:00:13 pm »
I am curious as to what type of programs you have written where the values of variables don't change!

If that is the case, then these programs would not benefit by my control.
Spent a few minutes to write a simple object list class. See source code there:
http://pastebin.com/xp8eVvxz

There is a button and memo on a form. When user presses button, it adds a instance of random class object into TMyList. I'm creating a combination of TObject, TMyList2, TBitmap and TStringList, but the list can hold any object types. This class is automatically freeing type, although it wouldn't need to be if programmer wants to free them himself.

But i hope there is something to be learned or compared from...

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: Linked List Class
« Reply #29 on: March 26, 2012, 03:05:19 pm »
I am curious as to what type of programs you have written where the values of variables don't change!

If that is the case, then these programs would not benefit by my control.
Spent a few minutes to write a simple object list class. See source code there:
http://pastebin.com/xp8eVvxz

@User137 Sorry but your link doesn't work. Do you have another link?
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

 

TinyPortal © 2005-2018