Category Archives: C/C++ Puzzles

Zombie and Orphan Processes in C

Prerequisite: fork() in C Zombie Process: A process which has finished the execution but still has entry in the process table to report to its parent process is known as a zombie process. A child process always first becomes a zombie before being removed from the process table. The parent process reads the exit status… Read More »

Opaque Pointer

What is an opaque pointer? Opaque as the name suggests is something we can’t see through. e.g. wood is opaque. Opaque pointer is a pointer which points to a DS whose contents are not exposed at the time of its definition. Following pointer is opaque. One can’t know the data contained in STest structure by… Read More »

Converting Strings to Numbers in C/C++

There are two common methods to convert strings to numbers: Using stringstream class or sscanf() stringstream() : This is an easy way to convert strings of digits into ints, floats or doubles. Following is a sample program using a stringstream to convert string to int. Output: Value of x : 12345 // A stringstream is… Read More »

Functors in C++

Please note that the title is Functors (Not Functions)!! Consider a function that takes only one argument. However, while calling this function we have a lot more information that we would like to pass to this function, but we cannot as it accepts only one parameter. What can be done? One obvious answer might be… Read More »

How to quickly reverse a string in C++?

One simple solution is two write our own reverse function to reverse a string in C++. Output : skeegrofskeeg There is a direct function in “algorithm” header file for doing reverse that saves our time when programming. // Reverses elements in [begin, end] void reverse (BidirectionalIterator begin, BidirectionalIterator end); Below is simple C++ program. Output… Read More »

Understanding constexpr specifier in C++

constexpr is a feature added in C++ 11. The main idea is performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized be developer, it is run multiple times by users. The idea is to spend time in compilation and save time… Read More »

Namespace in C++ | Set 1 (Introduction)

Consider following C++ program. Output : Compiler Error: ‘value’ has a previous declaration as ‘int value’ In each scope, a name can only represent one entity. So, there cannot be two variables with the same name in the same scope. Using namespaces, we can create two variables or member functions having the same name. Output:… Read More »