Set - 2

Question 6 :

Why does malloc(0) return valid memory address ? What's the use ?

Answer :

malloc(0) does not return a non-NULL under every implementation.
An implementation is free to behave in a manner it finds suitable, if the allocation size requested is zero. The implmentation may choose any of the following actions:
* A null pointer is returned.
* The behavior is same as if a space of non-zero size was requested. In this case, the usage of return value yields to undefined-behavior.

Notice, however, that if the implementation returns a non-NULL value for a request of a zero-length space, a pointer to object of ZERO length is returned! Think, how an object of zero size should be represented?

For implementations that return non-NULL values, a typical usage is as follows:

void
func ( void ) {
	int *p; /* p is a one-dimensional array, whose size will vary during the the lifetime of the program */
	size_t c;
    p = malloc(0); /* initial allocation */
	if (!p) {
		perror ("FAILURE" );
		return;
	}
	/* … */

	while (1) {
		c = (size_t) … ; /* Calculate allocation size */
		p = realloc ( p, c * sizeof *p );
		/* use p, or break from the loop */
		/* … */
	}
	return;
}

Notice that this program is not portable, since an implementation is free to return NULL for a malloc(0) request, as the C Standard does not support zero-sized objects.


Question 7 :

Difference between const char* p and char const* p ?

Answer :

In const char* p, the character pointed by 'p' is constant, so u cant change the value of character pointed by p but u can make 'p' refer to some other location.
in char const* p, the ptr 'p' is constant not the character referenced by it, so u cant make 'p' to reference to any other location but u can change the value of the char pointed by 'p'.


Question 8 :

What is the result of using Option Explicit ?

Answer :

When writing your C program, you can include files in two ways. 
The first way is to surround the file you want to include with the angled brackets < and >. 
This method of inclusion tells the preprocessor to look for the file in the predefined default location. 
This predefined default location is often an INCLUDE environment variable that denotes the path to your include files.
For instance, given the INCLUDE variable 
INCLUDE=C:\COMPILER\INCLUDE;S:\SOURCE\HEADERS; 
using the #include version of file inclusion, the compiler first checks the 
C:\COMPILER\INCLUDE 
directory for the specified file. If the file is not found there, the compiler then checks the 
S:\SOURCE\HEADERS directory. If the file is still not found, the preprocessor checks the current directory. 
The second way to include files is to surround the file you want to include with double quotation marks. This method of inclusion tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations you have set up. Using the #include file version of file inclusion and applying it to the preceding example, the preprocessor first checks the current directory for the specified file. If the file is not found in the current directory, the C:COMPILERINCLUDE directory is searched. If the file is still not found, the preprocessor checks the S:SOURCEHEADERS directory. 
The #include method of file inclusion is often used to include standard headers such as stdio.h or
stdlib.h. 
This is because these headers are rarely (if ever) modified, and they should always be read from your compiler's standard include file directory. 
The #include file method of file inclusion is often used to include nonstandard header files that you have created for use in your program. This is because these headers are often modified in the current directory, and you will want the preprocessor to use your newly modified version of the header rather than the older, unmodified version.


Question 9 :

What is the benefit of using an enum rather than a #define constant ?

Answer :

The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.
1) The first advantage is that enumerated constants are generated automatically by the compiler. Conversely, symbolic constants must be manually assigned values by the programmer.
For instance, if you had an enumerated constant type for error codes that could occur in your program, your enum definition could look something like this:

enum Error_Code{
	OUT_OF_MEMORY,
	INSUFFICIENT_DISK_SPACE,
	LOGIC_ERROR,
	FILE_NOT_FOUND
};

In the preceding example, OUT_OF_MEMORY is automatically assigned the value of 0 (zero) by the compiler because it appears first in the definition. The compiler then continues to automatically assign numbers to the enumerated constants, making INSUFFICIENT_DISK_SPACE equal to 1, LOGIC_ERROR equal to 2, and FILE_NOT_FOUND equal to 3, so on.

If you were to approach the same example by using symbolic constants, your code would look something like this:

#define OUT_OF_MEMORY 0
#define INSUFFICIENT_DISK_SPACE 1
#define LOGIC_ERROR 2
#define FILE_NOT_FOUND 3

values by the programmer. Each of the two methods arrives at the same result: four constants assigned numeric values to represent error codes. Consider the maintenance required, however, if you were to add two constants to represent the error codes DRIVE_NOT_READY and CORRUPT_FILE. Using the enumeration constant method, you simply would put these two constants anywhere in the enum definition. The compiler would generate two unique values for these constants. Using the symbolic constant method, you would have to manually assign two new numbers to these constants. Additionally, you would want to ensure that the numbers you assign to these constants are unique. 

2) Another advantage of using the enumeration constant method is that your programs are more readable and thus can be understood better by others who might have to update your program later.

3) A third advantage to using enumeration constants is that some symbolic debuggers can print the value of an enumeration constant. Conversely, most symbolic debuggers cannot print the value of a symbolic constant. This can be an enormous help in debugging your program, because if your program is stopped at a line that uses an enum, you can simply inspect that constant and instantly know its value. On the other hand, because most debuggers cannot print #define values, you would most likely have to search for that value by manually looking it up in a header file.


Question 10 :

What is the quickest sorting method to use ?

Answer :

The answer depends on what you mean by quickest. For most sorting problems, it just doesn't matter how quick the sort is because it is done infrequently or other operations take significantly more time anyway. Even in cases in which sorting speed is of the essence, there is no one answer. It depends on not only the size and nature of the data, but also the likely order. No algorithm is best in all cases. 
There are three sorting methods in this author's toolbox that are all very fast and that are useful in different situations. Those methods are quick sort, merge sort, and radix sort.

The Quick Sort
The quick sort algorithm is of the divide and conquer type. That means it works by reducing a sorting problem into several easier sorting problems and solving each of them. A dividing value is chosen from the input data, and the data is partitioned into three sets: elements that belong before the dividing value, the value itself, and elements that come after the dividing value. The partitioning is performed by exchanging elements that are in the first set but belong in the third with elements that are in the third set but belong in the first Elements that are equal to the dividing element can be put in any of the three setsthe algorithm will still work properly.

The Merge Sort
The merge sort is a divide and conquer sort as well. It works by considering the data to be sorted as a sequence of already-sorted lists (in the worst case, each list is one element long). Adjacent sorted lists are merged into larger sorted lists until there is a single sorted list containing all the elements. The merge sort is good at sorting lists and other data structures that are not in arrays, and it can be used to sort things that don't fit into memory. It also can be implemented as a stable sort. 
The Radix Sort
The radix sort takes a list of integers and puts each element on a smaller list, depending on the value of its least significant byte. Then the small lists are concatenated, and the process is repeated for each more significant byte until the list is sorted. The radix sort is simpler to implement on fixed-length data such as ints.