Recent

Author Topic: My contribution to the community for beginners  (Read 9727 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: My contribution to the community for beginners
« Reply #15 on: June 07, 2019, 09:10:40 am »
Also, if you're using Integer remember that it may be 4 or 8 bytes, depending on whether the platform is 32 or 64 bits.
The size of Integer does not depend on the bitness. It only depends on the mode, namely it's 16 bit in modes fpc, tp and iso and 32-bit in the others.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: My contribution to the community for beginners
« Reply #16 on: June 07, 2019, 09:37:34 am »
Bookmarked  :)

kveroneau

  • Full Member
  • ***
  • Posts: 119
Re: My contribution to the community for beginners
« Reply #17 on: June 08, 2019, 06:43:01 pm »
I added a new document on a lesser used feature of computers these days, command parsing.  In Python I generally used both the shlex and cmd modules a lot to create interactive command-line tools which were also able to load external script files.  I was originally going to port shlex over to Object Pascal, but I managed to find an easier method by using just two pascal functions.  You can see the document and related example code here:

http://tech406.com/kdocs/parsing.xml

The second document of the day is on UNIX Signalshttp://tech406.com/kdocs/signals.xml

The first part of the document explains why you would want to trap these signals if you plan on writing a UNIX service, and explains the various use-cases for the most common signal types.

I just updated the UNIX Signals page with a fully working Pascal Unit called UnixSignals, if you wish to use the most common signals within your program, and this means any program type, except a shared library.  You can merely just check the included booleans within the Unit to see if a specific signal was sent to your program.  You should of course place this into your programs idle/event loop and check for the signals there and act upon them as needed.  This can enable either your GUI program or server process under UNIX-like systems to easily reload their configuration files from disk when the SIGHUP is sent to your process for example by just checking the HangUp boolean from the unit file.  Be sure to set the boolean back to false after handing the signal so it won't constantly trigger.  Enjoy!

Update to Pointers:  Need to work with large binary data in Pascal in a very specific way, and TMemoryStream just does not fulfill that requirement for you?  I ended up making a new class, which is similar to TMemoryStream, but does not expand or shrink, it is set to a constant allocation size defined by the class.  Why would you want to use such a class, you might be asking?  If you are writing a strict emulator or virtual machine with a very specific heap size requirement, that's why!  When writing the 6502 emulator I wrote, the CPU needs to see and have a constant 64k of memory, and the overhead of TMemoryStream constantly reallocating the pointer size just isn't ideal.  This new class simulates the flat memory model required to write emulators and virtual machines with a static amount of memory.  It has handy functions such as reading and writing both 8 and 16-bit integers from random address locations, the seek procedure is only used for the TStream compatible LoadFromStream and SaveToSteam procedures, other than that, your program is free to PEEK and POKE around at any memory location with ease.

http://tech406.com/kdocs/pointers.xml  Then click on Using flat memory in Pascal at the top.  Enjoy!

June 10th update: I added a new page on Checksums with examples on how to use the built-in crc unit to generate and store a checksum for CRC32, CRC64, and CRC128.  I still do not know how to display the u128 custom type as a GUID string, any ideas anybody?  I checked the system unit documentation where the custom type is defined to see if there are any functions or procedures to work with TGuid types, such as converting it to a GUID string, and back over to a u128, but I couldn't find anything.  If you know, please let me know and I will update the document to reflect that knowledge.  The really cool part about the custom u128 type is that it can be placed into a custom record, and easily written to disk.  The hex dump of the created file appears to have the whole 128-bit Integer intact as expected.
« Last Edit: June 11, 2019, 04:32:10 am by kveroneau »

alantelles

  • New Member
  • *
  • Posts: 21
Re: My contribution to the community for beginners
« Reply #18 on: June 09, 2021, 03:03:45 pm »
Just replying for saying the the http://tech406.com/kdocs/sockets.xml was really really useful!

Thanx for contributing!!
# compulsive coder
FPC 3.2.0, Lazarus 2.0.10

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: My contribution to the community for beginners
« Reply #19 on: June 09, 2021, 03:16:30 pm »
Good job!
... or maybe to export its content to the freepascal wiki, which will be a useful update for.
+1
As interesting another blog on Free Pascal is, the best location to share knowledge is the Wiki.
So it can be reviewed and contributed to, by others.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: My contribution to the community for beginners
« Reply #20 on: June 09, 2021, 05:01:36 pm »
Hey kveroneau,

This is a minor thing and it's just my unhealthy thing that I can't seem to shut up when I see failing things...

Welp, some of the code blocks have erratic syntax highlighting and I was wondering why is that?

Apart from this insignificant little mishap, the content is totally AWESOME and deserves a good pat on your back!!!!

Keep it on and this will be a very good repository of knowledge.

I also hope you manage to get it on GitHub so that the community at large can help you with some PR's

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Bi0T1N

  • Jr. Member
  • **
  • Posts: 85
Re: My contribution to the community for beginners
« Reply #21 on: June 26, 2021, 05:10:32 pm »
The problem with
Code: Pascal  [Select][+][-]
  1. type
  2.   TRequest = record
  3.     id: Integer;
  4.     op: byte;
  5.     data: string[80];
  6.   end;
  7.  
  8.   TResponse = record
  9.     id: Integer;
  10.     status: byte;
  11.     data: string[80];
  12.   end;
and
Quote
I am not entirely sure why, but the structure in Python has a size of 85, but the same structure in Object Pascal has a size of 88.
is due to data alignment. To align the memory there are three hidden bytes added (padding) after the status byte: 4+1+3+80=88. If you want to avoid that, you need to use packed record and then the record will have a size of 4+1+80=85 as in Python.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: My contribution to the community for beginners
« Reply #22 on: June 26, 2021, 05:38:14 pm »
Hi!

If you use the packed record it will use 86 bytes:

The string[80] is a shortstring, a so called "Pascal string".
It carries in the byte zero the length, so the string[80] occupies in the packed mode 81 byte.

Winni

Bi0T1N

  • Jr. Member
  • **
  • Posts: 85
Re: My contribution to the community for beginners
« Reply #23 on: June 29, 2021, 11:11:33 pm »
The string[80] is a shortstring, a so called "Pascal string".
It carries in the byte zero the length, so the string[80] occupies in the packed mode 81 byte.
Indeed, wasn't aware of that. :-[
However, for such things it's better to use arrays with fixed length like array[0..79] of byte. The same applies for the integer variable since it might not have the same amount of bytes on all platforms. Better prefer fixed width integers like Int32 (will always have 4 byte).

Code: Pascal  [Select][+][-]
  1. type
  2.   TRequest = packed record
  3.     id: Int32;
  4.     op: byte;
  5.     data: array[0..79] of byte;
  6.   end;
  7. type
  8.   TResponse = TRequest;

 

TinyPortal © 2005-2018