Saturday 28 January 2017

How to clean up workspace after compiling and running C/C++ programs in windows

Hi Cpp Programmers,

If you are tired of removing the *.o and *.exe files generated after compiling and running c/cpp files then you have come to right place.

This blog explains how to remove such files in Windows Platform. Later script for Linux version will also be added.

Suppose you have a parent directory called MyCppCodeRepo within which you might have children directory e.g. LinkedList, Trees, etc.

Here are the steps :


  1. Go to MyCppCodeRepo e.g. D:/myfile/MyCppCodeRepo/
  2. Create a file cleanUp.txt.
  3. Paste the following code : del /S *.o *.exe
  4. (OPTIONAL) If you want to see the logs (i.e which files are being deleted) add an additional line pause .
  5. Save and Exit.
  6. Rename the file to cleanUp.bat . This transforms the text file to a batch file.
  7. Now whenever you want to delete files of type *.o and *.exe in the current directory or any nested folder just double click it.




Explanation : del : deletes file supplied to it.
                      /S   : looks for nested folder (recursively)
                     *.o and *.exe : * is for file name of any length. .o and .exe file types which we want to delete 


You can copy and paste this small batch file in any directory and remove the desired file types. Suppose, after few months you start Java Programming then compiling Java files (*.java) generates java bytecodes which have *.class extension. If you want to delete these class files then just append *.class in line 3 of above steps.

Git provide .gitignore for this specific purpose - to exclude such binaries and other files generated at compile time. But writing your scripts to achieve these small tasks has its own satisfaction. Hope it helps you in keeping your work-space clean.

HAPPY PROGRAMMING. 

Understanding the immutable nature of java.lang.String

public class ImmutableStringDemo {

public static void main(String[] args) {

String sl = "First Name";  // a string literal
String so = new String("First Name"); // a string object
StringBuffer sb = new StringBuffer("First Name"); // a stringBuffer object


System.out.println("s1 = " + sl + " hashCode = "+ mimicObjectToString(sl));
sl += "Kumar"; // appended ,
System.out.println("s1 = " + sl + " hashCode = "+ mimicObjectToString(sl)+ "\n");
// if the output differs it means a new reference has been created during appending

System.out.println("so = " + so + " hashCode = "+ mimicObjectToString(so));
so += "Kumar";
System.out.println("so = " + so + " hashCode = "+ mimicObjectToString(so)+ "\n");

System.out.println("sb = " + sb + " hashCode = "+ mimicObjectToString(sb));
sb.append("Kumar");
System.out.println("sb = " + sb + " hashCode = "+ mimicObjectToString(sb)+ "\n");

System.out.println("Changed hashCode denotes different reference has been created, making it \"immutable\"");
}

public static String mimicObjectToString(Object o)
    {
        //prevent a NullPointerException by returning null if o is null
        String result = null;
        if (o !=null)
        {
            result = o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
        }
        return  result;
    }
}
 
Click here for running code : https://ideone.com/llyFaU  

Oracle docs defines StringBuffer as "A thread-safe, mutable sequence of characters"