If you really want to save your OS having forgotten the password it can be pretty easy. All you need to do is edit the /etc/shadow file of the root partition of your SD card. The shadow file contains encrypted user passwords.
How are you going to edit the shadow file? Good question:
1) Put the SD card into a card reader of some other Linux machine. Perhaps another Pi. Now you can see and edit any and all files on the partitions of that SD card.
2) If you have a Windows machine with a card reader install the EXT file system driver from Paragon
https://www.paragon-software.com/home/linuxfs-windows/. Put your SD card into the Windows card reader. Now you can see and edit any and all files on the partitions of that SD card.
3) If you are unlucky enough to only have a Mac available I have no idea. Perhaps somebody else can advise.
What are you going to put into the /etc/shadow file now that you can edit it? Good question:
1) Copy the encrypted password string, for the right username. A username you know the password of.
2) Copy an entire /etc/shadow file from a fresh Raspbian image thus resetting the password for pi back to it's default.
Note: If you have been adding and removing users from your original system before forgetting the password you may need to copy the /etc/passwd file over as well.
Memory in C++ is a leaky abstraction .