Recent

Author Topic: [Solved] A Tip for beginners  (Read 3958 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #30 on: January 20, 2025, 01:21:22 pm »
Almost Correct: it has to do with memory allocation, because traversing downwards keep the location itself in place ( same pointer) traversing upwards does not do that, so you will loose speed by reallocmem. You can test that by tracking the pointer(s).
« Last Edit: January 20, 2025, 01:25:02 pm by Thaddy »
But I am sure they don't want the Trumps back...

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1326
Re: A Tip for beginners
« Reply #31 on: January 20, 2025, 01:28:42 pm »
Well obviously less memory is allocated as things are deleted. But for explaining to a newbie I’d try to explain it in a less technical terminology.  :)
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

MarkMLl

  • Hero Member
  • *****
  • Posts: 8221
Re: A Tip for beginners
« Reply #32 on: January 20, 2025, 02:22:14 pm »
Well obviously less memory is allocated as things are deleted. But for explaining to a newbie I’d try to explain it in a less technical terminology.  :)

The thread's been thoroughly Thaddy'd.

Don't laugh, since I'm quite likely to refer to other threads as being Joanna'd: the difference is that you introduce wibble rather than otiose complexity.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Hero Member
  • *****
  • Posts: 666
  • Retired programmer
Re: A Tip for beginners
« Reply #33 on: January 20, 2025, 02:32:33 pm »
It can work moving forwards ... :)

Nice. Although, this one would be hard to adapt to work with UTF.
Wouldn't change anything difficulty wise. Delete deletes 1 byte, so same problem.

If related to silvercoder70 :
It is plain wrong, utf8 or not. the setlength causes a relocation.
No it doesn't, the final setlength just changes the internal string length without reallocating memory.

I’m surprised to see people arguing over something so basic.
Downto , {descending index} is the correct way to approach anything that might be reduced in length. Only a genuine newbie would not know this.

It has nothing to do with memory allocation , it has to do with the items referenced by indices changing when an item is deleted. In fact if anything is removed your program will probably crash when you reach the last index and try to reference something that is no longer there.
Nonsense generalization. In this case, delete internally calls SetLength for each deleted character.

Almost Correct: it has to do with memory allocation, because traversing downwards keep the location itself in place ( same pointer) traversing upwards does not do that, so you will loose speed by reallocmem. You can test that by tracking the pointer(s).
Nonsense, silvercoder70's code doesn't touch the location. You didn't trace the code, the pointer to the AnsiRec doesn't change.

Well obviously less memory is allocated as things are deleted. But for explaining to a newbie I’d try to explain it in a less technical terminology.  :)
Just repeating the nonsense of Thaddy in the case of the original code doesn't make you look very helpful.




Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1326
Re: A Tip for beginners
« Reply #34 on: January 20, 2025, 03:02:23 pm »
I don’t understand how a simple question about which algorithm is better turned into all this drama.  :D
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

dseligo

  • Hero Member
  • *****
  • Posts: 1461
Re: A Tip for beginners
« Reply #35 on: January 20, 2025, 03:05:16 pm »
It can work moving forwards ... :)

Nice. Although, this one would be hard to adapt to work with UTF.
Wouldn't change anything difficulty wise. Delete deletes 1 byte, so same problem.

Silvercoder70's solution doesn't use delete.

BrunoK

  • Hero Member
  • *****
  • Posts: 666
  • Retired programmer
Re: A Tip for beginners
« Reply #36 on: January 20, 2025, 03:05:37 pm »

Don't laugh, since I'm quite likely to refer to other threads as being Joanna'd: the difference is that you introduce wibble rather than otiose complexity.

MarkMLl
Joanna'd, good and accurate :-)

BrunoK

  • Hero Member
  • *****
  • Posts: 666
  • Retired programmer
Re: A Tip for beginners
« Reply #37 on: January 20, 2025, 03:13:22 pm »
Silvercoder70's solution doesn't use delete.
I know it doesn't use delete. It use pull down with a single final length actualization. Nearly the same code I would write.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8221
Re: A Tip for beginners
« Reply #38 on: January 20, 2025, 03:17:11 pm »
I don’t understand how a simple question about which algorithm is better turned into all this drama.  :D

It's not.

OP made a good point, based on elementary Pascal, relating to a problem that I'm damn well sure has caught absolutely everybody at some point.

All this introduction of memory allocation etc., i.e. much deeper than is considered in elementary Pascal, has definitely made it worser.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Hero Member
  • *****
  • Posts: 666
  • Retired programmer
Re: A Tip for beginners
« Reply #39 on: January 20, 2025, 03:27:09 pm »
OP made a good point, based on elementary Pascal, relating to a problem that I'm damn well sure has caught absolutely everybody at some point.
So true !

For example, the question is relevant when removing objects from Lists and many other situations.

Do we remove from the tail or from the head, and if done from the head how to pack without doing to many memory moves etc... Shall one mark a released item with nil and other points that any average programmer should be able to handle.


Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 95
Re: A Tip for beginners
« Reply #40 on: January 20, 2025, 03:55:00 pm »
Thank you everyone. I always learn something from all of you.
The only thing that I think "some" (not everyone) of you missed is this sentence from my OP

"Yes; I know that there are a myriad of ways to attack this problem. This example is for newbies."

Although many superior examples were demonstrated I posted this "Tip" in the Beginners section
for the purpose of
 1. Showing the BEGINNER how iterating over a string via a loop can go mysteriously(for him) wrong.
 2. Challenging the BEGINNER to discover why the unexpected (for him) error occurs.

As regards the sometimes heated debates here I think it is good to remember that
 1. English is not everyone's first language and shades of meaning as well as humor can be missed
 2. If we weren't pedantic before starting programming we certainly became so afterwards. ::)

Again; thank you everyone who contributed to the conversation. I will mark it as solved.

Klaatu Barada Nikto
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

silvercoder70

  • Full Member
  • ***
  • Posts: 138
    • Tim Coates
Re: [Solved] A Tip for beginners
« Reply #41 on: January 20, 2025, 10:15:43 pm »
The code I posted was only to show there is nothing inherently (?) wrong with using "to" to perform an operation.

The original poster was correct in respect to using Delete() when moving up vs down in a string. All I will say is ...

there are many ways to skin the [proverbial] cat (as the saying goes).
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

 

TinyPortal © 2005-2018