Programming
Array Indexing in C
I found this interesting thing about array indexing in C somewhere in the web:
Suppose that “a” is an array. Then, a[5] and 5[a] are quivallent. Both are interpreted as *(5 + a) or *(a + 5) which is the same
Also: “Hello World”[3] and 3["Hello World"] are the same. Try and C
Tools to Improve the Quality of Your Code
Just found a nice link at Wikipedia with a collection of tools to help analyzing the quality of your code.
One interesting one is PMD (for Java), which is opensource. It statically analyses your code and alerts for potential problems.
For C, there is splint (opensource as well), which statically analyses your code for potential coding mistakes and security vulnerabilities.
The full list at Wikipedia is here.
Another great tool, which is not directly related to the above, is Valgrind. While the above statically analyze your code (i.e., they find potential problems by “just looking at the code”), Valgrind is a tool for dynamic analysis, which means, it analyses the code by monitoring the way it runs. It can report possible memory leaks, possible deadlooks, and many other information. (The full tool suite description can be found here). The output of Valgrind is not very easy to read, but with a little practice one can feel more comfortable with it.
There is also a list of tools for dynamic analysis at Wikipedia. It’s located here and may be worths a glance.
Enjoy coding
A Bash Script to Convert brainfuck Code Into C
Brainfuck is a minimalist Turing Complete language. You can read about it here.
The following bash script takes a file with brainfuck code (legal characters are >< ,.+-[] any other characters are ignored), and generates its C equivalent code, which can then be compiled with a C compiler and executed.
The output of this script lacks indentation. If you insist on having the C code indented you can achieve this by passing the output through a pipe to “indent”, “astyle” or similar programs.
The script should be called in the following way:
./bf.sh code.bf > code.c
Where code.bf is the file containing the brainfuck code.
Share Code Snippets Easily From the Command Line
This is a great tip I found at commandlinefu to easily share code snippets write from the command line.
First, you may want to add this to your aliases file:
1 | alias share='curl -F "sprunge=<-" http://sprunge.us | xclip' |
(If you don’t have the xclip program, you can easily install it from your package manager.)
Now, say that you have a c program named “hello.c”, to share it, type this (after reloading your aliases file of course):
cat hello.c | share
Now, the code is already in the internet, and the URL is in your clipboard. Go to firefox and click the middle mouse button in the URI area. You’ll see a URL similar to this:
http://sprunge.us/BaFS
To view the code with syntax highlighting and line numbers, just append “?c? (or “?lang” where ‘lang’ is the relevant programming language). So the final URL should look like this:
http://sprunge.us/BaFS?c
Enjoy
Sending Emails with Matlab
This method is (as far as I know) not found in the official Matlab’s documentation. I found it a long time ago somewhere in the internet (don’t remember where, sorry). It’s intended for sending Emails using Matlab.
In order to use it, you should have an Email account which supports SMTP (eg. Gmail). This account will be the account we’re sending the messages through.
The script assumes you’re using Gmail, but it can be easily modified to use other Email suppliers (are there any?)
How to Convert a Number into a String in C++
I found a few ways to convert a number into C++, some of them are C style (like using sprintf), and some are ugly (i.e non-standard, like using itoa), and there some nice way which uses string streams to achieve this.
Where going to define and implement a function called Stringify, which accepts an integer and returns a string (this can be extended to accept any numeric value).
The Power of Polymorphism (Video)
This video is part of “The Clean Code Talks” series, which is itself a part of the great “Google TechTalks”.
The video shows the strength of using polymorphism instead of switch statements and “if” conditionals, to get a much cleaner cleaner code, allowing it to be more readable, testable and maintainable.
One of the best videos I watched in this series.
Easily Create Code Snippets with Bonetree Box
Update: Looks like the link is not valid anymore
Anyhow, one always can use http://pastebin.com
Have you ever needed to show a code to a friend? If yes, I have no doubt that you will love Bonetree Box.
This web tool has a very simple and well-designed interface, and working with it is with no doubt a pleasant experience.
The tool has support for a set of popular programming languages, such as C, C++, Bash and more (I believe that this set will be extended over time), and allows you to create a syntax highlighted code snippets with zero effort.
How to Declare an Array of Strings in C
Ok, I know this is not a big deal, I just hate to deal with strings in C, so I’ll put the code here so I can copy-paste-modify whenever I need to do such things.
Purpose: declare an array of strings.
There are two cases here:
Using Macros to Print Debug Messages in C
I have to confess: It’s my first time using macros for debug, I used to just add printf-s when developing and remove them when the code is ready – Which is and ugly and very unprofessional in my opinion.
So, I decided to start caring about how my code looks, even in development stage. Here how I use macros for debug messages. (I needed the help of a friend to get this work).
first of all, define this:
About Me
Tags
My Twitter
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Science
- Security
- Shell Scripts
- Short Posts
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Archives
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)

Me @ Social Media