31) What is virtual memory?
A paging system is quite similar to a paging system with swapping. When we want to execute a process, we swap it into memory. Here we use a lazy swapper called pager rather than swapping the entire process into memory. When a process is to be swapped in, the pager guesses which pages will be used based on some algorithm, before the process is swapped out again. Instead of swapping whole process, the pager brings only the necessary pages into memory. By that way, it avoids reading in unnecessary memory pages, decreasing the swap time and the amount of physical memory.
Paging is a memory management scheme by which computers can store and retrieve data from the secondary memory storage when needed in to primary memory. In this scheme, the operating system retrieves data from secondary storage in same-size blocks called pages. The paging scheme allows the physical address space of a process to be non continuous. Paging allows OS to use secondary storage for data that does not fit entirely into physical memory.
33) Can structures be passed to the functions by value?
Passing structure by its value to a function is possible, but not a good programming practice. First of all, if we pass the structure by value and the function changes some of those values, then the value change is not reflected in caller function. Also, if the structure is big, then passing the structure by value means copying the whole structure to the function argument stack which can slow the program by a significant amount.
34) Why cannot arrays be passed by values to functions?
In C, the array name itself represents the address of the first element. So, even if we pass the array name as argument, it will be passed as reference and not its address.
35) Advantages and disadvantages of using macro and inline functions?
The advantage of the macro and inline function is that the overhead for argument passing and stuff is reduced as the function are in-lined. The advantage of macro function is that we can write type insensitive functions. It is also the disadvantage of macro function as macro functions can't do validation check. The macro and inline function also increases the size of the executable.
36) What happens when recursive functions are declared inline?
Inlining an recursive function reduces the overhead of saving context on stack. But, inline is merely a suggestion to the compiler and it does not guarantee that a function will be inlined. Obviously, the compiler won't be able to inline a recursive function infinitely. It may not inline it at all or it may inline it, just a few levels deep.
37) #define cat(x,y) x##y concatenates x to y. But cat(cat(1,2),3) does not expand but gives preprocessor warning. Why?
The cat(x, y) expands to x##y. It just pastes x and y. But in case of cat(cat(1,2),3), it expands to cat(1,2)##3 instead of 1##2##3. That is why it is giving preprocessor warning.
38) ++*ip increments what?
It increments the value to which ip points to and not the address.
39) Declare a manifest constant that returns the number of seconds in a year using preprocessor? Disregard leap years in your answer.
The correct answer will be -
#define SECONDS_IN_YEAR (60UL * 60UL * 24UL * 365UL)
Do not forget to use UL, since the output will be very big integer.
40) Using the variable a, write down definitions for the following:
- An integer
- A pointer to an integer
- A pointer to a pointer to an integer
- An array of ten integers
- An array of ten pointers to integers
- A pointer to an array of ten integers
- A pointer to a function that takes an integer as an argument and returns an integer
- Pass an array of ten pointers to a function that take an integer argument and return an integer.
The correct answer is as follows -
- int a;
- int *a;
- int **a;
- int a;
- int *a;
- int (*a);
- int (*a)(int);
- int (*a)(int);
If you have problem understanding these, please read pointer section in the provide tutorial thoroughly.