Sunday, March 20, 2022

GitHub: Go back to a previous commit

 You can see commit history by clicking the commits link:



You copy the SHA of the commit you want to revert to, open a terminal, go to source folder and type

git reset --hard <SHA>

Tuesday, March 15, 2022

C++: Dynamically create array of objects

Dynamically creating array of object on the heap:

MyClass** objects;

arrSize = 100;

objects = new MyClass*[arrSize]; //Note the '*' before arrSize

for (size_t i = 0; i < arrSize; i++) {

objects[i] = new MyClass();

}

Freeing up heap memory allocated to object array:

for (size_t i = 0; i < arrSize; i++) {

delete objects[i];

}

delete[] objects;

Tip: You can use Visual Studio's Memory Usage diagnostic tool to check heap memory changes which gives clues about memory leaks. To get the function names where memory is allocated but then never released, use valgrind on WSL (Linux) as follows:

valgrind --tool=memcheck --leak-check=yes ./<app name>

Sunday, March 13, 2022

Generating Linux programs in Windows Visual Studio C++

You can use Windows 10 and Visual Studio 2022 to generate, debug and analyze Linux binaries:

  1. Install WSL, restart computer
  2. Open cmd, type wsl to enable linux prompt, install tools
    1. sudo apt-get update
    2. sudo apt install g++ gdb make ninja-build rsync zip
  3. Open Visual Studio, on menu Tools - Get Tools and Features, add "Linux development with C++" if you haven't done already:
  4. In Visual Studio, 
    1. Create new project and select CMake project:
    2. After project is created, change Local Machine to WSL:Ubuntu. Change Startup item to your project name:
    3. If Visual Studio notifies you that CMake needs to be updated/installed, let it do it.
    4. Press debug button to build and run:
    5. At the bottom of the screen, in Output window, change it to CMake:
    6. Visual Studio tells you where it has generated the Linux binary. Copy this folder to clipboard:
    7. Go to View - Terminal. In the opened PowerShell terminal at the bottom, type wsl and press enter, the prompt will change to linux :
    8. Type cd and paste the folder you copied:
    9. Now you can also run your Linux binary from terminal:
    10. To use valgrind, first install it in PowerShell by typing sudo apt install valgrind
    11. Now you can use valgrind on your binary without leaving Visual Studio by typing (replace ./CMakeProject4 with your own project name): valgrind --tool=memcheck --leak-check=yes ./CMakeProject4:
    12. To copy the binary to a Windows folder, you can open File Explorer from Linux prompt by typing explorer.exe .:
    13. Congratulations!

Thursday, March 10, 2022

C++: Using arrays with threads

The following C++ code demonstrates how to safely use an array that is written to by one thread and read from in another thread:

Tuesday, March 8, 2022

Java: Get raw text in JFormattedTextField

You could define a custom class that derives from JFormattedTextField and use KeyListener to get the raw text that the user is typing:

Monday, March 7, 2022

Linux cheat sheet

Linux commands I frequently use:
  1. Change to root (on Ubuntu): sudo -i
  2. uname -r 
  3. sudo apt update && sudo apt upgrade
  4. Show processes whose name contains a specific string: ps -ef | grep <string>
  5. Forcefully kill process: pkill -f <process name>
    • Example: pkill -f update-notifier
  6. Run process in background: ./<process name> &
  7. Show network card info: ifconfig
  8. Show resource usage of running processes: top, htop
  9. Reverse-i-search with ctrl+r
  10. Change MAC address: sudo ifconfig enp2s0 hw ether 64:00:6a:28:fa:ac
  11. Ubuntu: Allow GUI root login
  12. Enable SSH to root user by adding the line PermitRootLogin yes to /etc/ssh/sshd_config
  13. Enable receiving UDP packets 
    1. Disable firewall completely: sudo ufw disable
    2. Or you can allow for example on port 5000: sudo ufw allow from any to any port 5000 proto udp
  14. Create image of USB with size of 8GB: dd if=/dev/sdb of=/home/myimage.img bs=1G count=8 status=progress
Tools:
  1. sudo apt install net-tools
  2. sudo apt install build-essential
    • gcc --version
    • make --version
  3. Eclipse IDE for C++
    1. In a project, when you remove a folder and copy another version of the same folder but with fewer files, during build, you might get the error "No rule to make target…". Right click on the project and select refresh, this rebuilds the index. Now you can build successfully.
  4. To use psftp from another Windows computer for file transfer: sudo apt install openssh-server
  5. sudo apt install rt-tests
    • Sample usage: cyclictest l100000 -t 8 p95
  6. sudo apt install htop
  7. For remote connection from Windows: 
    • sudo apt install xrdp
    • sudo systemctl restart xrdp
    • Note that you have to be logged out from Ubuntu for remote from Windows to work.