Friday, May 25, 2012

Sequence points in C


Sequence points in C.

Ever wondered what will be the value of the expression i++ * ++i in C/C++ when i = 1? If you have a compiler, try it once and do a mental exercise to find out the reason! But, before going to crack your head, check the same with more compilers and you may wonder why these compilers are giving different answers for the same expression! Now what is the reason behind this? Fortunately we have a written specification for the language from the standard committee of ISO. And they have clearly defined what should be the result of an expression in the language and all the modern compilers are following that standard. But still, somewhere something is missing with these tools or the expression evaluation? Before going to the details of this expression, we can look into some terms defined in the C standard.
Undefined Behavior: behavior, upon use of a non-portable or erroneous program construct or of erroneous data, for which the International Standard imposes no requirements.
An example of undefined behavior is the behavior on integer overflow.
Unspecified Behavior: use of an unspecified value, or other behavior where the International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance.
An example of unspecified behavior is the order in which the arguments to a function are evaluated.
And thus, the language can have some behavior which is undefined or unspecified. In such situations, the output of the program will depend on the implementation; in other words the compiler. The compiler developer has the liberty to choose how the program should behave in such situations. But as a good programming practice, a programmer should never code in such a way that your code will give different output on different implementation. Now the answer of the first question is somewhat clear. It might be an undefined or unspecified behavior and hence the result. And under which rule it produce this behavior? That is sequence points.
Sequence points
 According to the C standard a sequence point is,
Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.
In simple words, the side effect (change in execution environment) means, change of an object or file.  The changes are cumulative when the execution of instructions progress. Then there should be a mechanism which defines when and where all the changes in these objects will be stored back. This mechanism is known as sequence points. The change of a variable will not be written back to the memory whenever it happens, but only at the sequence points. That is all the side effects from the previous sequence point will be completed at the next sequence point. So, if the variable is changed multiple times between 2 sequence points, which value will it take for the next operation? The one, which got modified just now in the expression or the other, which has already stored back at the previous sequence point? This behavior is not defined in the standard. That means it’s an ‘Undefined Behavior’.
According to the C standard there are 7 sequence points and they are
Ø  The call to a function, after the arguments have been evaluated.
Ø  The end of the first operand of the following operators: logical AND '&&', logical OR '||', conditional '?', comma ','.
Ø  The end of a full declarator.
Ø  The end of a full expression: an initializer, the expression in an expression statement, the controlling expression of a selection statement (if or switch), the controlling expression of a ‘while’ or ‘do’ statement, each of the expressions of a ‘for’ statement, the expression in a return statement.
Ø  Immediately before a library function returns.
Ø  After the actions associated with each formatted input/output function conversion specifier.
Ø  Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call.
Each declarator declares one identifier and a full declarator is a declarator that is not part of another declarator. The last one deals with searching and sorting functions defined in C library. These comparison functions are passed as a function pointer and will be called from the search/sort function (call back functions). In general, this can be clubbed to the first one and make a more general point: “The call to a function or a call back function”.
A good coding practice: avoid more than one change in a single object, between 2 consecutive sequence points. The output will be compiler dependent and if you have any such undefined or unspecified behavior in your code, pray the compiler will never get changed or the new version of the compiler will not have a change in the behavior of the evaluation of the expression. Otherwise, you will end up in unexpected bugs from unexpected area!

Wednesday, May 9, 2012


A positive approach to Pandit effect!

A person has 2 choices. It is either follow or create a new path. Those who select the latter will always make history. This will have an impact in all the areas including marketing. Let us take the curious case of Santhosh Pandit. There are Discussions on whether this guy is a retard or if he is acting like one and making us retards. Neither! But he is acting like a retard and teaching us a lesson or two! His movie became successful in Kerala, during the Deepavali season and his competition was 3 super star movies: 2 from Tamil and 1 from Hindi. This negative publicity is not a novel idea. Many people have already benefited out of it. ‘Silsila’ by Harisankar is another famous (or notorious?) example from Malayalam. ‘Friday’ by Rebecca Black is an English album in the same genre. There is a Tamil movie ‘Yaruku Yaro’ acted by Sam Anderson, but failed to exploit the publicity and make money.

These successful people explored a new area in the market, and got the benefit they deserve. Here I am trying to present some market makers and their story in our Software industry. There is nothing in common between Pandit or his movies and the people in the below stories, except they exploited a new marketing technic and made their marks in the history.

When the programming language BASIC was developed and shipped with IBM machines, it was not separately sold. The contract was between Micro-Soft (now, Microsoft) and IBM. No users need to buy that language, instead IBM provided it. Then, the release of DOS revealed a new marketing area unknown to IBM and other hardware manufacturers: direct licensing by the software developers, rather than shipping it with a machine. Thereafter the contract was changed and it became directly between the users and the OS developers. And as history proves, Microsoft excels in that. What made them to think in a new way? They found a potential market in software industry, which no one else thought about till then.

But when Linus Torvalds developed (or ripped off from Minix) his Linux kernel and Richard Stallman founded his Free Software Foundation, they were creating a new era in the software marketing. In this new area, their major earning was not through license, but through support. And by doing this, they got some additional benefit for making the source open – some moral supports from the development community. Essentially, this marketing technic provided them a top position as well as a saint image for keeping the source open for free modification.

Another technic widely used in industry is the term licensing. Consider the conventional way i.e., perpetual licensing, we pay for software and the license is for a lifetime. But who will use that old one for all his ventures? In a year a new version will come out and at least after 2 versions, he has to renew it to prevent the kick out from his business. Now for a new version, he has to pay a big amount again. The term licensing will help the developer to purchase the current version for a year or so and he can go for a new version when the license expires (suppose at least one new version is available in next year). This is beneficial to both the customer and the company. Customer feels that the software is required for 6 months and he can pay only for that period. This will increase the number of purchased software as the cost comes down and increase the net revenue of the company. Most of the antiviruses are having term licensing.

Google’s business is another example. They started with a search engine. They have no ‘donate’ button as Wikipedia. Then how they earn money? Google’s real business is not searching, but advertising. Any site can be registered to Google AdSense and they get benefitted for the advertisement Google put up in their site. And Google is providing more options for the same. They say: “You create a free site/blog using our infrastructure; we put advertisement and give you money for making a successful site/blog”.  Almost all of their businesses are running in this way, viz. Google.com, YouTube, BlogSpot, Google Sites etc.

Another kind of business is offering other companies’ freeware or shareware. One such software is Adobe Flash player. Let me make this clear with an example. Adobe has software called Adobe Director (previously owned by Macromedia). The corresponding content player is Shockwave player. Adobe Director is licensed and Shockwave player is free. But the revenue of Shockwave player is more than 5 times of Director! The business is offering third party software. While installing Shockwave player, it will offer some third party software and get money from them. Currently almost all the freeware will ask to install ‘Free’ Google toolbar. Google will pay them for each installation of their toolbar. Google will get good penetration, so that they can ask more money from those who put ads. And the freeware which install this toolbar get money from Google. Google is not the only one with this offer. Norton will give money for offering their virus scanner, which will scan PC and give alert, but don’t cure. Likewise many companies are supporting well known freeware of other companies for offering their software.

We have many more examples. Amazon and eBay – online marketing, Flipkart – cash on delivery, Facebook – social network, twitter – micro blog, Justdial – B2C search engine etc. All marketing technics were new once and the inventors of these technics made and are still making good profits out of it. Those who opened new areas and create success stories rarely got expelled from that area. So, while thinking of a marketing idea, think whether you can find a good place, if you follow the current strategy else make a new one. The target is fixed, not the path! The already existing path may be congested, but not your new one. People may call you Santhosh Pandit in the beginning, but you will be a Bill Gates one day!

Microsoft, IBM, Adobe, Google and Norton are either their registered trademark or trademark in the US and/or other countries.
YouTube and BlogSpot are either Google’s registered trademark or trademark in the US and/or other countries.
Director, Flash and Shockwave are either Adobe’s registered trademark or trademark in the US and/or other countries

Wednesday, May 2, 2012

A brief History of C

The beginning

In 1966 Martin Richards of the University of Cambridge developed a programming language called Basic Combined Programming Language (BCPL).
In 1969 Kenneth Lane Thompson (Ken Thompson) developed a stripped-down version of the BCPL programming language called 'B' for Bell Laboratories. Currently Thompson is working with Google.
The 'C' Language has been developed by Dennis M. Ritchie inspired by the language B, for use with the UNIX operating system in 1972. He returned from the main function on October 12, 2011.
The language was originally designed to help the development of Unix Operating System. The UNIX is one of the first OS developed in a high level language.

The earlier version of C has been changed many times to incorporate powerful features of different computer architectures. One of such feature is the ‘struct’ type added in 1973.
By the first edition of the book, 'The C Programming Language' by Brian Kernighan and Dennis Ritchie in 1978, the language got its first written specification. The version of the language described in this book is known as K&R.
In the book, they have introduced many additional features to the language such as
-           I/O libraries
-          ‘long’ and ‘unsigned’ type modifiers.
ANSI/ISO C90

In 1983, the American National Standards Institute (ANSI) formed a committee, to establish a standard specification of C. And in 1989, the first draft of the standard was published. This version often referred to as ANSI C or C89.
In 1990, International Organization for Standardization (ISO) adopts the ANSI standard of C language as ISO/IEC 9899:1990 and this is referred to as C90. (Both C89 and C90 are the same language). ANSI no longer develops the C standard independently, but defers to the ISO C standard.

ISO C99


The C standard was further revised and lead to the publication of ISO/IEC 9899:1999 in 1999, which is commonly referred to as "C99". C99 introduced several new features in the language and some of them are
-          inline functions
-          variable-length arrays
-          variadic macros
Some of these features are borrowed from other languages like C++, but After C99, C is no more considered as a subset of C++. C supports many features not included in C++.

-          ‘long long’ type modifier
-          ‘restrict’ keyword
-          Variadic macros
-          Variable length arrays
-          Flexible array members
-          Designated initializers
-          Compound literals

Links


The C99 standard is available here or here.


Most used free compilers of C/C++: MingW and Visual C++ Express Edition.

Some FOSS IDEs for MingW: CodeBlocks, Dev-C++, NotePad++ (More like an editor) and TextPad (my favorite, and again an editor, you may read this: How to configure TextPad as MingW IDE)

Comparison of different Visual Studio editions.

C1X


In 2007, the ISO started revising the C standard, informally called "C1X".
For ISO, the major guiding principles for drafting the standard are

-          Existing code is important, existing implementations are not.
-          C code can be portable.
-          C code can be non-portable.
-          Avoid "quiet changes."
-          A standard is a treaty between implementer and programmer.
-          Keep the spirit of C. And below are some of the facets of the spirit of C:
ü  Trust the programmer.
ü  Don't prevent the programmer from doing what needs to be done.
ü  Keep the language small and simple.
ü  Provide only one way to do an operation.
ü  Make it fast, even if it is not guaranteed to be portable.

Examples

The examples given below are linked from the above sessions. Make use of those links for better understanding. Avoidance of these examples don’t affect the flow of the article and can be skipped if found difficult.

inline functions


Actual source code
After inline expansion

  


Inline is exactly a request to the compiler for expanding the code inline; it may or may not do it according to the implementation. ‘inline’ functions are not same as macros. Macros are handled by the preprocessor and they do not have a type check.

long long int complex data types

 









Output: 1, 2, 4, 4, 8
A simple rule of the size of the integer types is

All sizes are in bytes.

Variable-length arrays


This is, what exactly the name suggest



Is valid in C!

variadic macros


A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function.



 

Support for one-line comments



Implicit casting of ‘void’ pointer


The void* can be converted to any one without a typecast. Example of an impact is ‘malloc’. The old return type of the same function was char* and after the introduction of void in C, it is changed to void*. Now the usage should be:


And not


Calling ‘main’ function recursively


This is not allowed in C++. Even there is little usage, it is supported in C.




The above code will just make an infinite loop.
The major use of this feature is in obfuscation.

‘restrict’ keyword


This keyword is used to tell the compiler that an object is accessed only through the ‘restrict’ qualified pointer. This allows the compiler to perform certain optimizations. And accessing such an object using another pointer will cause undefined behavior.

Flexible array members


A structure can have an array member at the end of it without specifying a size. While allocating memory for the structure object, by allocating more memory for this array member one can extend the size of it.








Here, the array member ‘data’ of object ‘p’ will have 10 characters length and that of ‘q’ will have 15 characters length.
Designated initializers

This feature allows initializing the members of an array or a structure in any order.



Both the above statements are equivalent initializations.

Similarly for a structure



are equal.
Compound literals

A compound literal looks like a cast containing an initializer. Its value is an object of the type specified in the cast, containing the elements specified in the initializer


Is equivalent to:


Future


The language has done its role to build and shape the computer era. Born in Bell and Brought up by Brilliants! The beauty of C is it believes her programmers. So, if anyone feels that C lacks a feature which helps you from doing something nasty, woke up, you are a programmer, not a school kid! And C is a real programmer's language.

Monday, April 30, 2012

Tech Me

An Engineer by profession.
Like to learn and share anything new in computer.
My dream profession is Teaching!

I like to think in C and dream in C++.
Like the beauty of FOSS but I code for my bread and butter.

Sharing is earning in learning. :)