Sunday, April 15, 2012

Difference between blocking and non-blocking callbacks

As per wiki:
There are two types of callbacks: blocking callbacks (also known as synchronous callbacks or just callbacks) and deferred callbacks (also known as asynchronous callbacks). These two design choices differ in how they control data flow at runtime. While blocking callbacks are invoked before a function (in the example above: main) returns, deferred callbacks may be invoked after a function returns. The above is an example of a blocking callback. Deferred callbacks are often used in the context of I/O operations or event handling. While deferred callbacks imply the existence of multiple threads, blocking callbacks are often (but not always) relying on a single thread. Therefore blocking callbacks are no common cause for synchronization.

Difference between mallocing and local variables

It is interesting to see what is the difference between these two types of variable declarations
char* name = malloc(256*sizeof(char));
// more code
free(name);
 char name[256];
 Has been nicely explained on stackoverflow:
In the first code, the memory is dynamically allocated on the heap. That memory needs to be freed with free(). Its lifetime is arbitrary: it can cross function boundaries, etc.
In the second code, the 256 bytes are allocated on the stack, and are automatically reclaimed when the function returns (or at program termination if it is outside all functions). So you don't have to (and cannot) call free() on it. It can't leak, but it also won't live beyond the end of the function. Choose between the two based on the requirements for the memory.