Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, July 9, 2012

Tip: traversing arrays in C-Shell

Below I have described two methods to traverse arrays in C-Shell - one uses the foreach loop and other while loop.
In both cases, the important thing to note is that array index in C-shell starts with '1' instead of '0' as in most programming languages, like C, C++, Java, Perl.


set a = (1 2 3 4)
set b = (5 6 7 8)
 

Method 1 - using the foreach loop, iterating on one array as the foreach index, and the accessing the other array inside the loop body using the index operator "[]"
set i = 1
foreach x ( `echo $a` )
  echo "x = $x b = $b[$i]"
  @ i = $i + 1
end
 

Output: 
x = 1 b = 5
x = 2 b = 6
x = 3 b = 7
x = 4 b = 8

Method 2 - using the while loop. Iterating on the size of array and accessing both the arrays inside the loop body using the index operator "[]"


set i = 1
while ($i <= 4)
  echo "a = $a[$i] b = $b[$i]"
  @ i = $i + 1
end
 

Output:
a = 1 b = 5
a = 2 b = 6
a = 3 b = 7
a = 4 b = 8




Wednesday, February 15, 2012

gdb stops at SIGPIPE



By default, gdb captures SIGPIPE of a process and pauses it. However, some program ignores SIGPIPE. So, the default behavour of gdb is not desired when debugging those program. To avoid gdb stopping in SIGPIPE, use the folloing command in gdb:

handle SIGPIPE nostop noprint pass

Wednesday, July 14, 2010

using sed in alias in C shell

Using sed commands in your .alias file in C shell is always tricky. One example of the same is:

alias grf 'set fileNline = `echo "\!*" | sed -e "s/\([^ ]*\):\([0-9]*\):.*/+\2 \1/"`;gvim $fileNline'

This alias is called as:

grf file:line_num:

and then it open the file "file" in gvim with cursor at line "line_num".

Saturday, April 25, 2009

Ways to extract data from a space delimited string

Ways to tokenize a string if you cannot use the space character as a field delimiter in an input where the fields can have spaces:

1. create the input with the delimiter as some other character as the delimiter. The delimiter character should be non-printable. This would reduce its chances of occuring in the input and thus reduce handling of special cases.

2. If the input was generated with space as the delimiter then we have a problem at hand. For such cases there are two approaches and both require the knowledge of the format of the input.

If the format of the input is known then one can use regular expressions to search for the tokens in the string. (Typical scripting languages, like Perl, TCL support regular expressions. C++ user can use the Boost library for regular expression support).

However using regular expressions can be expensive if the number of searches during the program execution are large. So these can be used only when the number of searches are small.

For programs that do such search more often, let us understand the other approach using an example:

// input format:

char* inputStr = "12 abc def 14";
char* firstSpaceChar = strchr(inputStr, ' ');
int firstInt = 0;
int lastInt = 0;
string midStr = "";
if (firstSpaceChar != NULL) {
*firstSpaceChar = '\0';
firstInt = atoi(inputStr);
char* lastSpaceChar = strrchr(firstSpaceChar+1, ' ');
if (lastSpaceChar != NULL) {
lastInt = atoi(lastSpaceChar+1);
*lastSpaceChar = '\0';
}
midStr = firstSpaceChar+1;
} else {
midStr = inputStr;
}



Friday, October 31, 2008

Tech: How to find the existence of a thread

To find if a thread created using pthread_create exists or not use phread_kill with signal number 0
on the thread that needs to be tested. If the return value is 0, then the thread is alive.
If the return value is ESRCH, then the thread is not alive in the system.

Sample code may look like:

bool is_thread_alive(pthread_t thread_id)
{
int status = pthread_kill(thread_id, 0);
if (status == ESRCH)
return true;
else
return false;
}

Tuesday, March 25, 2008

STL: find in a list of pointers

Most STL tutorials will not help you solve this problem:
How to search in a list of pointers?
e.g vector<'obj*'>myList;
The find alogrithm on this will match only the pointer address and if you do not share your objects between classes then your find will always fail as it matches only the pointer addresses and not the value to which it points.
To fix this problem, the solutions that can be considered are:
1. store objects instead of pointers in the list. (huge memory overhead!!)
2. use find_if instead of find and write a functor which compares value instead of pointer address
3. Overload find and implement your own comparator in it. This comparator again compares the value instead of pointer address.