Set - 4

Question 1 :

What is the heap ?

Answer :

The heap is where malloc(), calloc(), and realloc() get memory.

Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such memory isn't deallocated automatically; you have to call free().

Recursive data structures are almost always implemented with memory from the heap. Strings often come from there too, especially strings that could be very long at runtime.

If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you put the data on the heap. Sometimes you can use a better algorithm if you use the heap faster, or more robust, or more flexible.

It's a tradeoff. If memory is allocated from the heap, it's available until the program ends. That's great if you remember to deallocate it when you're done. If you forget, it's a problem.

A memory leak is some allocated memory that's no longer needed but isn't deallocated. If you have a memory leak inside a loop, you can use up all the memory on the heap and not be able to get any more. (When that happens, the allocation functions return a null pointer.)

In some environments, if a program doesn't deallocate everything it allocated, memory stays unavailable even after the program ends.

Question 2 :

How do you use a pointer to a function ?

Answer :

The hardest part about using a pointer-to-function is declaring it. 
Consider an example. You want to create a pointer, pf, that points to the strcmp() function.

The strcmp() function is declared in this way: 
int strcmp(const char *, const char * )

To set up pf to point to the strcmp() function, you want a declaration that looks just like the strcmp() function's declaration, but that has *pf rather than strcmp: 
int (*pf)( const char *, const char * );

After you've gotten the declaration of pf, you can #include and assign the address of strcmp() to pf: pf = strcmp;

Question 3 :

What is the purpose of main( ) function ?

Answer :

The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution.

It is the starting function
It returns an int value to the environment that called the program
Recursive call is allowed for main( ) also.
It is a user-defined function
Program execution ends when the closing brace of the function main( ) is reached.
It has two arguments 1)argument count and 2) argument vector (represents strings passed).
Any user-defined name can also be used as parameters for main( ) instead of argc and argv

Question 4 :

Why n++ executes faster than n+1 ?

Answer :

The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.

Question 5 :

What will the preprocessor do for a program ?

Answer :

The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code.

The preprocessor is invoked as the first part of your compiler program's compilation step. It is usually hidden from the programmer because it is run automatically by the compiler.

The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments.

If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.