Programmer's Blog

Programmer's reference

[SQL] delete duplicated rows

DELETE FROM table
WHERE rowid not in
(SELECT MIN(rowid)
FROM table
GROUP BY column1, column2);

[C] Returnable 2D array and readfile

char** create2DCharArray(int m, int n)
{
   char* values = calloc(m*n, sizeof(char));
   char** rows = malloc(n*sizeof(char*));
   int i;
   for (i=0; i<n; ++i)
   {
       rows[i] = values + i*m;
   }
   return rows;
}

void destroy2DCharArray(char** arr)
{
   free(*arr);
   free(arr);
}

char** readFileInto2DArray ( const char* fileName )
{
   FILE *f;
   f = fopen(fileName,"r");
   char ch, strr[32767], *str;
   int row = 0, column = 0, i = 0, j = 0;
   while(fgets(strr, sizeof strr, f))
   {
      row++;
      if(column < strlen(strr) )
           column = strlen(strr);
   }
   rewind(f);
   char** arr = create2DCharArray(column,row);
   while(i < row)
   {
     do
     {
        ch = fgetc(f);
        if ( ch == '\r')
        ch = fgetc(f);
        arr[i][j] = ch;
        j++;
     }
     while( !( ch == '\n' || ch == EOF ) );
     arr[i][--j] = '\0';
     i++;
     j = 0;
   }
   fclose(f);
   return arr;
}

[regex] inverse selection

/\<\(THEWORD\>\)\@!\k\+\>

[Linux] Useful commands

Execute commands on files found by the find command
$ find -iname "MyCProgram.c" -exec md5sum {} \;

Find all empty files in home directory
# find ~ -empty

converts the DOS file format to Unix file format using sed command.
$sed 's/.$//' filename

Print only specific field from a file.
$ awk '{print $2,$5;}' employee.txt

Go to the 143rd line of file
$ vim +143 filename.txt

Open the file in read only mode.
$ vim -R /etc/passwd

[C++] check hardware concurrency

unsigned int n = std::thread::hardware_concurrency();

[C++] Scoped thread class

class scoped_thread
{
  std::thread t;
public:
  explicit scoped_thread(std::thread t_) : t(std::move(t_))
  {
    if(!t.joinable())
      throw std::logic_error(“No thread”);
  }

  ~scoped_thread()
  {
    t.join();
  }
  scoped_thread(scoped_thread const&)=delete;
  scoped_thread& operator=(scoped_thread const&)=delete;
};

[C++] RAII thread guard class

 

class thread_guard
{
  std::thread& t;
public:
  explicit thread_guard(std::thread& t_) : t(t_) {}
  ~thread_guard()
  {
    if(t.joinable())
    {
      t.join();
    }
  }
  thread_guard(thread_guard const&)=delete;
  thread_guard& operator=(thread_guard const&)=delete;
};

 

[Java] Simple thread-safe singleton class

public class threadSafeSingleton {

  //objects in singleton
  private HashMap<String, String> myHashMap = new HashMap<String, String>();
  private static threadSafeSingleton singletonInstance = null;

  protected threadSafeSingleton() {
  }
  
  public static threadSafeSingleton getSingleton() {
    if (singletonInstance == null) {
      singletonInstance = new threadSafeSingleton();
    }
    return singletonInstance;
  }

}

[C++] Check memory leaks by top

top -b -n 1

To see applications which are leaking memory, look at the following columns:

RPRVT – resident private address space size
RSHRD – resident shared address space size
RSIZE – resident memory size
VPRVT – private address space size
VSIZE – total memory size

[RHEL] update dev packages

yum update

yum install libtool gcc-c++ glib*

# This installs autoconf, automake, cloog-ppl, cpp, gcc, mpfr, ppl

yum groupinstall "development tools"