Sunday, 25 January 2015

C common questions

Callback function
- one that is not called explicitly by the programmer.
- Instead, there is some mechanism that continually waits for events to occur, and it will call selected functions in response to particular events.
- This mechanism is typically used when a operation(function) can take long time for execution and the caller of the function does not want to wait till the operation is complete, but does wish to be intimated of the outcome of the operation.
- Callback functions help implement such an asynchronous mechanism, wherein the caller registers to get intimated about the result of the time consuming processing and continuous other operations while at a later point of time, the caller gets informed of the result.
- An practical example: Windows event processing:
- virtually all windows programs set up an event loop, that makes the program respond to particular events (eg button presses, selecting a checkbox, window getting focus) by calling a function.
- The handy thing is that the programmer can specify what function gets called when (say) a particular button is pressed, even though it is not possible to specify when the button will be pressed. The function that is called is referred to as a callback.

- Volatile
-- tells the compiler not to optimize anything
Ex 1: Here in the example, optimizing compiler will notice that no other code can possibly change the value stored in foo, and will assume that it will remain equal to 0 at all times. The compiler will therefore replace the function body with an infinite loop similar to this:

static int foo;
void bar(void) {
   foo = 0;
    while (foo != 255)
        ;
}
void bar_optimized(void) {
   foo = 0;
    while (true)
        ;
}
Ex2. When two threads sharing information via global variable, they need to be qualified with volatile. Since threads run asynchronously, any update of global variable due to one thread should be fetched freshly by another consumer thread. Compiler can read the global variable and can place them in temporary variable of current thread context. To nullify the effect of compiler optimizations, such global variables to be qualified as volatile


- Ring buffer
-- A circular buffer, cyclic buffer or ring buffer is data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.
-- consequence is that when it is full and a subsequent write is performed, then it starts overwriting the oldest data.


- extern "C"
extern "C" is meant to be recognized by a C++ compiler and to notify the compiler that the noted function is (or to be) compiled in C style.
How to handle C symbols when linking from C++?
In C, names may not be mangled as C doesn’t support function overloading. So how to make sure that name of a symbol is not changed when we link a C code in C++. For example, see the following C++ program that uses printf() function of C.

// Save file as .cpp and use C++ compiler to compile
int printf(const char *format,...);
int main()
{
   printf("GeeksforGeeks");
   return 0;
}
// Save file as .cpp and use C++ compiler to compile
extern "C"
{
   int printf(const char *format,...);
}
int main()
{
   printf("GeeksforGeeks");
   return 0;
}
Output:
undefined reference to `printf(char const*, ...)'
       ld returned 1 exit status
Output:
GeeksforGeeks
The reason for compiler error is simple, name of printf is changed by C++ compiler and it doesn’t find definition of the function with new name.
The solution of problem is extern “C” in C++. When some code is put in extern “C” block, the C++ compiler ensures that the function names are unmangled – that the compiler emits a binary file with their names unchanged, as a C compiler would do.
Therefore, all C style header files (stdio.h, string.h, .. etc) have their declarations in extern “C” block.
#ifdef __cplusplus
extern "C" {
#endif
   /* Declarations of this file */
#ifdef __cplusplus
}
#endif



Extern vs static
- Static has internal linkage
- extern has external linkage
- extern will cause error if declared before static int
- to share extern you can use it in include file
-


Const Qualifier in C
- declaration of any variable to specify that its value will not be changed
- depends upon where const variables are stored, we may change value of const variable by using pointer
- The result is implementation-defined if an attempt is made to change a const


volatile vs const volatile

const volatile
- not be permitted to be changed by the code
- typically used to access hardware registers that can be read and are updated by the hardware, but make no sense to write to
- status register for a serial port
volatile
to ensure compile does not optimise.

No comments:

Post a Comment