Sunday, July 22, 2018

C Questions And Answers – 2018A22

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

 

-------------------------------------------------------------------------------------------------

 

System Utility

What is garbage collection?

 

Ans:

Suppose some memory space becomes reusable because a node is released from a linked list.

 

Hence, we want the space to be available for future use. One way to bring this about is to immediately reinsert the space into the free-storage list. However, this method may be too time-consuming for the operating system. The operating system may periodically collect all the deleted space onto the freestorage list. The technique that does this collection is called Garbage Collection. Garbage Collection usually takes place in two steps: First the Garbage Collector runs through all lists, tagging whose cells are currently in use, and then it runs through the memory, collecting all untagged space onto the freestorage list. The Garbage Collection may take place when there is only some minimum amount of space or no space at all left in the free-storage list, or when the CPU is idle and has time to do the collection. Generally speaking, the Garbage Collection is invisible to the programmer.

 

-------------------------------------------------------------------------------------------------

 

How do I get the time elapsed between two function calls?

 

Ans:

The function difftime( ) finds the difference between two times. It calculates the elapsed time in seconds and returns the difference between two times as a double value.

 

#include <time.h>

#include <stdio.h>

#include <dos.h>

main( )

{

int a[] = {2, -34, 56, 78, 112, 33, -7, 11, 45, 29, 6};

int s ;

time_t t1, t2 ; // time_t defines the value used for time function

s = sizeof ( a ) / 2 ;

t1 = time ( NULL ) ;

sel_sort ( a, s ) ; // sort array by selection sort

bub_sort ( a, s ) ; // sort array by bubble sort method

t2 = time ( NULL ) ;

printf ( "\nThe difference between two function calls is %f", difftime (t2, t1 )) ;

}

 

In the above program we have called difftime( ) function that returns the time elapsed from t1 to t2.

 

-------------------------------------------------------------------------------------------------

 

General

 

main( )

{

char *s;

s = fun ( 128, 2 ) ;

printf ( "\n%s", s ) ;

}

fun ( unsigned int num, int base )

{

static char buff[33] ;

char *ptr ;

ptr = &buff [ sizeof ( buff ) - 1 ] ;

*ptr = '\0' ;

do{

*--ptr = "0123456789abcdef"[ num % base ] ;

num /= base ;

} while ( num != 0 ) ;

return ptr ;

}

 

The above program would convert the number 128 to the base 2. You can convert a number to a hexadecimal or octal form by passing the number and the base, to the function fun( ).

 

----------------------------------

 

Data Structures

What is a priority queue?

 

Ans:

As we know in a stack, the latest element is deleted and in a queue the oldest element is deleted. It may be required to delete an element with the highest priority in the given set of values and not only the oldest or the newest one. A data structure that supports efficient insertions of a new element and deletions of elements with the highest priority is known as priority queue. There are two types of priority queues: an ascending priority queue is a collection of items into which items can be inserted arbitrarily and from which only the smallest item can be removed. A descending order priority queue is similar but allows only the largest item to be deleted.

 

-------------------------------------------------------------------------------------------------

 

What is the difference between const char *p, char const *p, and char* const p?

 

'const char *p' and 'char const *p' are the same, i.e. p points to a constant character. On the other hand, 'char* const p' means p is a constant pointer pointing to a character which means we cannot change the pointer p but we can change the character which p is pointing to.

 

-------------------------------------------------------------------------------------------------

 

What's the difference between a null pointer, a NULL macro, the ASCII NUL character and a null string?

 

Ans:

A null pointer is a pointer which doesn't point anywhere. A NULL macro is used to represent the null pointer in source code. It has a value 0 associated with it. The ASCII NUL character has all its bits as 0 but doesn't have any relationship with the null pointer. The null string is just another name for an empty string "".

 

--------------------------------

 

System Utility

Sparse Matrix...

A sparse matrix is one where most of its elements are zero. There is no precise definition as to know whether a matrix is sparsed or not, but it is a concept which we all can recognize intuitively. The natural method of representing matrices in memory as two-dimensional arrays may not be suitable for sparse matrices. That is one may save space by storing only those entries which may be nonzero. If this is done, then the matrix may be thought of as an ordered list of non-zero elements only. Information about a non-zero element has three parts:

an integer representing its row,

an integer representing its column and

the data associated with this element.

That is, each element of a matrix is uniquely characterized by its row and column position, say i, j. We might store that matrix as a list of 3-tuples of the form (i, j, data), as shown below,

Although the non-zero elements may be stored in the array in any order, keeping them ordered in some fashion may be advantageous for further processing. Note that above array is arranged in increasing order of the row number of non-zero elements. Moreover, for elements in the same row number, the array is arranged in order of increasing column number.

 

-------------------------------------------------------------------------------------------------

 

Pointers

What does the error "Null Pointer Assignment" means and what causes this error?

 

Ans:

The Null Pointer Assignment error is generated only in small and medium memory models. This error occurs in programs which attempt to change the bottom of the data segment. In Borland's C or C++ compilers, Borland places four zero bytes at the bottom of the data segment, followed by the Borland copyright notice "Borland C++ - Copyright 1991 Borland Intl.". In the small and medium memory models, a null pointer points to DS:0000. Thus assigning a value to the memory referenced by this pointer will overwrite the first zero byte in the data segment. At program termination, the four zeros and the copyright banner are checked. If either has been modified, then the Null Pointer Assignment error is generated. Note that the pointer may not truly be null, but may be a wild pointer that references these key areas in the data segment.

 

--------------------------------------------------

 

Data Structures

How to build an expression trees?

Ans: An expression tree is a binary tree which is built from simple operands and operators of an (arithmetic or logical ) expression by placing simple operands as the leaves of a binary tree and the operators as the interior nodes. If an operator is binary, then it has two nonempty subtrees that are its left and right operands (either simple operands or sub expressions). If an operator is unary, then only one of its subtrees is nonempty, the one on the left or right according as the operator is written on the right or left of its operand. We traditionally write some unary operators to the left of their operands, such as "-" (unary negation) or the standard functions like log( ), sin( ) etc. Others are written on the right, such as the factorial function ()!. If the operator is written on the left, then in the expression tree we take its left subtree as empty. If it appears on the right, then its right subtree will be empty. An example of an expression tree is shown below for the expression (-a < b ) or ( c + d ).

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment