Category: c language

Pitfall of C array

CODE int main() { int my_array[4] = {11,12,13,14}; int next = 15; printf(“\nSurprise = %d”, my_array[4]); } What should it print? Well, although illegal, the above code does legal things. The array “my_array” has four integers allocated on the stack. The next integer “next” is allocated adjacent to the stack memory. So the address of…

A priority queue with O(1) operations: C, Linux implementation

I have implemented a priority queue that enable O(1) time complexity for major operations i.e. enqueue and dequeue. It is C, POSIX compliant code. Things to note – It uses buffer pools – Its behavior is to return the highest priority node from the queue – If queue is empty/full, we use conditional waits –…

Synchronize two threads to print ordered even and odd numbers in C

Problem You have two threads, that independently print even and odd values. The goal is to synchronize these threads so as to get a non-decreasing ordered set of numbers and order should preserve natural ordering of numbers. So the output should be 1,2,3,4,5,6,7,8….. It is an interesting problem and could be solved with a conditional…

Idempotent operators and application in computer programs

We have studied about idempotent matrices. How does it apply to computer programming? An idempotent operation can be defined as: a. Unary: f(f(x) => f(x) b. Binary: f(x,x) => x So, it effectively means that an operation leaves the operand in original /known form. Why would we need such behavior in our program? There are…

Android OS: Google DVM and Virtual machines

Published in LinuxForYou, Jun 2011 Abstract With the outburst of heterogeneous systems, the need for a scalable software system is very much required without compromising the cost of development and maintenance of the software. Virtual machine (VM) provides abstraction from the heterogeneity and presents a low cost and scalable software development environment. VM based modern…

>Best C coding practices: Saving many hours of efforts

>C code has potential to be very compact, often at a price of losing comprehensibility and maintainability. Let’s discuss a few tips that might put smile on your face in a distress time. o) Always save return value from a function, if available if(!myfun()) {…} This is very poor coding style. If myfun() can return…

Thoughts on malloc()

* Always check for equal number of malloc()/free() calls. • Use calloc() instead of malloc() + memset() • Instead of allocation memory in a piece-meal manner, try to allocate a bigger chunk with malloc() and keep on resizing it with realloc() if needed. • malloc() over-commits the memory i.e. it gives you an address which…

Unfolding 2-D array in C

Last weekend my friends and I had a discussion on 2-D array in C. Surprisingly it went long as everyone had some points and knowledge to share 🙂 Finally we could gather some important information and understood this pearl of C better. I’ll clear this idea with C code snippets. // Declare an arrayint arr[2][3]=…