1. Difference between array and linked list.
l 1. Access :Random / Sequential
l Stack elements can be randomly Accessed using Subscript Variable
l e.g a[0],a[1],a[3] can be randomly accessed
l While In Linked List We have to Traverse Through the Linked List for Accessing Element. So O(n) Time required for Accessing Element .
l Generally In linked List Elements are accessed Sequentially.
l 2 . Memory Structure :
l Stack is stored in contiguous Memory Locations , i.e Suppose first element is Stored at 2000 then Second Integer element will be stored at 2002 .
l But It is not necessary to store next element at the Consecutive memory Location .
l Element is stored at any available Location , but the Pointer to that memory location is stored in Previous Node.
l 3 . Insertion / Deletion
l As the Array elements are stored in Consecutive memory Locations , so While Inserting elements ,we have to create space for Insertion.
l So More time required for Creating space and Inserting Element
l Similarly We have to Delete the Element from given Location and then Shift All successive elements up by 1 position
l In Linked List we have to Just Change the Pointer address field (Pointer),So Insertion and Deletion Operations are quite easy to implement
l 4 . Memory Allocation :
l Memory Should be allocated at Compile-Time in Stack . i.e at the time when Programmer is Writing Program
l In Linked list memory can be allocated at Run-Time , i.e After executing Program
l Stack uses Static Memory Allocation and Linked List Uses Dynamic Memory Allocation
Dynamic Memory allocation functions – malloc,calloc,delete etc…
2. What are enumerations?
An enumeration is a user-defined data type consists of integral constants and each integral constant is give a name. Keyword enum is used to defined enumerated data type.
enum type_name{ value1, value2,...,valueN };
Here, type_name is the name of enumerated data type or tag. And value1, value2,....,valueN are values of type type_name.
By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change the default value.
3. Describe about storage allocation and scope of global, extern, static, local and regular variables.
The storage allocation / class determine the memory part where the storage space is allocated for variables, functions and how long the allocation of storage continues to exist.
The scope of a variable is specified by its storage allocation. This is specified by the keywords – auto, extern, static and register.
- ‘auto’ variables stores in the memory storage. Majority of the variables in a program / application are of type ‘auto’. Their scope might be local or global.
- ‘extern’ variables are of global variables and can be declared in another source file which could be external / outside the current program scope,
- ‘register’ variables are allocated in the CPU registers. These variables storage and accessibility is much faster than other variables, being they are stored in CPU itself. The variables of repeated usage or access time is critical, can be declared as register variables.
- ‘static’ variables provides a lifetime over the program, and provides a way for limiting the scope of such variables. These variables are automatically initialized to zero and could be specified for ‘auto’ and ‘extern’ variables. The values are retained, even though they are declared in local scope, between the repeated function calls to the same function.
4. What is the use of typedef ?
typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.
And:
K&R states that there are two reasons for using a typedef. First, it provides a means to make a program more portable. Instead of having to change a type everywhere it appears throughout the program's source files, only a single typedef statement needs to be changed. Second, a typedef can make a complex declaration easier to understand.
5. Can we specify variable field width in a scanf() format string? If possible how?
It is possible to specify variable field width in a scanf() format string. By using %s control string. This character reads a string of variable field width up to the first white space character.
Ex: scanf(“%s”, name); // where name is a character array
6. Out of fgets() and gets() which function is safe to use and why?
The function fgets() function is safer to use. It checks the bounds, i.e., the size of the buffer and does not cause overflow on the stack to occur. gets() does not check the bounds.
7. Difference between strdup and strcpy.
The function strcpy() will not allocate the memory space to copy. A pointer to the string to copy and a pointer to place to copy it to should be given.
The function strdup() will occupy / grab itself the memory space for copying the string to. This memory space needs to be freed up later when it is of no use anymore.
8. What is recursion?
Recursion is an approach in which a function calls itself with an argument. Upon reaching a termination condition, the control returns to the calling function.
9. Write down the equivalent pointer expression for referring the same element a[i][j][k][l] ?
It is a[i][j][k][l]; so in pointer reference:
(*(*(*(*(a+i)+j))+k)+l)
10. What is difference between Structure and Unions?
Structure
|
Union
|
1.The keyword struct is used to define a structure
|
1. The keyword union is used to define a union.
|
2. When a variable is associated with a structure, the compiler allocates the
memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes. |
2. When a variable is associated with a union, the compiler allocates the
memory by considering the size of the largest memory. So, size of union is equal to the size of largest member. |
3. Each member within a structure is assigned unique storage area of location.
|
3. Memory allocated is shared by individual members of union.
|
4. The address of each member will be in ascending order This indicates that
memory for each member will start at different offset values. |
4. The address is same for all the members of a union. This indicates that
every member begins at the same offset value. |
5 Altering the value of a member will not affect other members of the structure.
|
5. Altering the value of any of the member will alter other member values.
|
6. Individual member can be accessed at a time
|
6. Only one member can be accessed at a time.
|
7. Several members of a structure can initialize at once.
|
7. Only the first member of a union can be initialized.
|
11. What are the advantage of using Unions?
Unions are useful (often in embedded programming) when it is needed to refer to a data item as two or more different types: e.g. it may be used as an integer for math, but an array of bytes for a serial transmission or a checksum calculation.
For memory preservation, since the space allocated for the union is as big as its biggest member.
For memory preservation, since the space allocated for the union is as big as its biggest member.
12. What are the advantages of using pointers in a program?
pointers are generally useful in the context where we need a continuous memory allocation. Using pointers dynamic allocation of memory is achieved
pointers basically hold the address of a variable. they are mainly used as function parameters to pass values of parameters as references rather than values
pointers basically hold the address of a variable. they are mainly used as function parameters to pass values of parameters as references rather than values
14. What is a far pointer? Where we use it?
Far pointer: it will access the total memory of the system and can be use to point to every object used in the memory.
15. What is NULL pointer? Whether it is same as an uninitialized pointer?
An uninitialized pointer is a pointer that has not been assigned a value; it's uninitialized.
The value of an uninitialized pointer is undefined (it could be anything).
A null pointer is simply a pointer that has a value of NULL.
A word of advice: initialize your pointers to NULL.
This allows you to see that the pointer is invalid; to check if an uninitialized pointer is valid is quite impossible.
The value of an uninitialized pointer is undefined (it could be anything).
A null pointer is simply a pointer that has a value of NULL.
A word of advice: initialize your pointers to NULL.
This allows you to see that the pointer is invalid; to check if an uninitialized pointer is valid is quite impossible.
16. What does the error ‘NULL pointer assignment’ means and what causes this error?
A NULL pointer assignment is a runtime error It occurs due to various reasons one is that your program has tried to access an illegal memory location. Illegal location means either the location is in the operating systems address space or in the other processes memory space. In stdio.h NULL is defined as 0 So whenever your program tries to access 0th location the operating system kills your program with runtime assignment error because the 0th location is in the operating systems address space and operating system doesn't allow access to its address space by user program.
17. What are near, far and huge pointers? How many bytes do they occupy?
Near, far, and huge pointers are different types of pointers used to reconcile the different addressing models of the Intel 8086/8088 class processors, running in a 16-bit operating system such as Windows 3.x, as well as any of the newer incarnations running in real mode or virtual 8086 mode.
A near pointer can address something within one 64Kb memory segment, containing only an offset, and it takes two bytes. The segment is implied by context, and is usually the data segment, selected for the addressing model.
A far pointer can address anything in the 1Mb memory1, containing both a segment and an offset, and it takes four bytes.
A huge pointer is a normalised far pointer, which means its offset part is always between 00H and 0FH.
In 32-bit mode a pointer can address anything in 4Gb memory, containing a flat 32-bit offset, and it takes four bytes. (In this mode segments have no significance.) It only works, however, when there is support for it, such as the WIN32 extension of Windows 3.x.
A near pointer can address something within one 64Kb memory segment, containing only an offset, and it takes two bytes. The segment is implied by context, and is usually the data segment, selected for the addressing model.
A far pointer can address anything in the 1Mb memory1, containing both a segment and an offset, and it takes four bytes.
A huge pointer is a normalised far pointer, which means its offset part is always between 00H and 0FH.
In 32-bit mode a pointer can address anything in 4Gb memory, containing a flat 32-bit offset, and it takes four bytes. (In this mode segments have no significance.) It only works, however, when there is support for it, such as the WIN32 extension of Windows 3.x.
No comments:
Post a Comment