Archive for the ‘Programming’ Category

GCC C __attribute__ feature

Most of compilers support optimization level in which each level has special significance to optimize the source in terms of size, performance etc. This can be enabled by specified command line arguments, mostly known as options.

Moreover GCC compiler has a special characteristic additionally – declaring attributes of a function. The source can be optimized at programming level. While writing the source, the programmer has a chance to instruct the compiler to what should be taken care.

__attribute__ keyword is used when a function (declared only) is a declared and the corresponding instruction/s is/are mentioned with it.  There are many ways to do nasty things with it found here.

In the embedded system, the code is nicely put together in different sections according to the design/requirements.  The code goes into the text section. It happens that while starting the OS, it is so better to gather the initialization code together that as soon as the initialization is over, the related code can be removed from the main memory.  This can be achieved by combining initialization code into init section i.e  __attribute__ section(“.init”). This is where my journey has started for attribute feature.

This function attibute has been widely used in L4 micro-kernel, which provides a secured approach for embedded systems facilitating the hardware manufactures to protect their patents/innovation without exposing to the outside world.

There are many things can be done using Linux and GCC pair, but today this is enough to start with.

WideStudio IDE

Today it happened to be to find out about WideStudio IDE primarily developed in Japan.  It’s an open source GUI based application development environment providing support for Windows, Linux, FreeBSD and Solaris. It has been developed in C++. The following features really attract the cross platform developers.

  • Multi platform application development. For example, if the application is developed in Windows, can also run in Linux
  • Multi Encoding support for Unicode (UTF8), EUCJP and SJIS. It handles encoding independently of the encoding of the underlying platform.
  • Freely available for personal and business use.  No need to publish the source
  • Application development is possible for Desktop, large server and even for embedded devices
  • Supports C, C++, Java, Perl, Ruby, Python, OCaml
  • Multi Widget Toolkit support

There are many good open source IDEs are available for applicatin development presently.  God bless open source.

Eclipse

Eclipse is open development platform. I have been trying to learn it for a long time as it facilitates Android platform application development. I have got hands on it at last. It seems the tool is excellent and provides a lot of feature which I used to dream about.

I started it with Welcome page and tried to explore the different features starting from the view to creating a new Hello World application. Yeah the same one which was created by Brian Kerningham and Dennis Ritchie in C programming. It’s fun to create it a step by step procedures for Java.

Eclipse is really well thought application development environment having the following features.

- Different versions of the same components are easily available

- Project creation step guides

- File movement within the workbench

- Perspective concept thought it’s similar, but really well arranged

- Refactor feature for creating new functions from existing code

- Quick outline view

- Highlighting errors while writing the codes

- Tool finding the required import packages

- Hierarchy view of the project

- Look and feel is decent too and so on..

My personal suggestion is that it’s worth to try it. I will surely write about it in the future.

When is a switch statement better than multiple if statements?

I read this question in some website today. A lot of people have tried to give the following answers

- if more than two condition expression for the same variable
- code readability
- complexity of code increases if there are more conditions used
- systematic and easy code to understand
- straight forward condition (no less than or greater than condition)

I was a bit satisfied with those answers. After some thoughts, I realized that I have used the following code in some cases which can facilitate the answer convincingly

There are some situations where we have many conditions based on the same variable and some require special processing and the rest as the same as of others. For example..

switch(variable)
{

case val1:

do something for val1;

break;
case val2:

do something for val2;

case val3:
do something for val3;
break;
default:
break;
}

Here we require to ‘do something for val2′ and we can reuse the ‘do something of val3′ code. We intentionally falling through the next switch because we feel that we can reuse that code. By doing so we can remove the redundant code.

I found one more interesting thing about how switch and if statements are converted to the assembly language.

switch statement performs all condition check in the beginning and jumps to the corresponding point and execute the bunch of processing statements.


if statement performs the check; on condition satisfied, execute the related processing statements. and go to the next statement after all if-else. If the condition is not satisfied, it jumps to the next condition check.


After analyzing the above disassembly code, it is better to use switch as it reduces the jump and redundant code.