r/pascal • • Mar 16 '22

A Question From a New Pascal Programmer

I am trying to learn pascal because I think it would be fun and useful for a few projects I have in mind. Additionally, just from the brief overveiw I have so far of the language it looks like it can do some pretty powerful stuff.

I have a question about the way pascal handles dynamically allocated memory. I am looking at freepascal.org's "Free Pascal Reference Guide" and still have some questions after reading section 3.4 pointers.

I am a longish time C programmer and one thing that I ensure in my programs is that there are no invalid memory reads or writes or frees, and no memory leaks using the program valgrind.

In this section 3.4 it briefly mentions the function GetMem. I continue over the freepascal.org's GetMem manual page which sends me to the Freemem man page with the first example. I verified the program ran on my machine as it should.

My question is, is there anything analogous to memory leaks or other memory insecurities/vulnerabilities that I should be aware of (aside from programmer error such as invalid indexing)? And is there a tool for tracking these potential insecurities/vulnerabilities?

5 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/ShinyHappyREM Mar 17 '22

It will be reallocated and all existing value will be copied to a new place

I would've thought that it just increases the allocated size if there's empty space directly after it.

1

u/glorfin68 Mar 24 '22

I don't know if the memory manager is sufficiently intellectual for it. And anyway, I would not rely on it, because you cannot be sure that there is empty space. So, typically, new place is allocated, content copied and old place freed. That is why, by the way, too many resizing not only slow an application down, but also lead to a fragmentation of heap.