Ans: "++" has higher precedence than "*", hence in case of *p++, pointer would be incremented and return the value of new pointer value. In case of ++*p, the value which is pointed by p will be incremented.
2. What is NULL pointer? How is it different from uninitialized pointer?
Ans: NULL pointer is a pointer which is not pointing any thing in the memory. NULL is defined as (void*)0.
Uninitialized pointer is pointing some memory location but the pointer values are not assigned.
3. What is a NULL pointer assignment error?
Ans: When we initialize any pointer to NULL means that it doesn't point anything. Now If we are trying to access or put some value in it then the error NULL pointer assignment occurs usually "Segmentation Fault".
EX: int *p;
*p = 12;
4. What is the difference between arr and &arr?
Ans: arr is pointer to the first element of the array and &arr is pointer to the entire array.
5. Is the cast to malloc() required at all?
Ans: malloc() returns pointer to void. That is, pointer to the first memory location of the allocated memory block. The pointer type is unknown at this point, hence typecasting is done later. void pointer is generic pointer anyway.
Ex: int *myarray;
myarray = (int*)malloc(numberOfElements * sizeof(int));
6. What does malloc(), calloc(), realloc(), and free() do?
Ans: malloc() is a memory manager, used to allocate memory block.
Syntax: void* malloc(size_t size); //size_t is unsigned integer.
This allocates a block of size bytes of memory, return a pointer to the beginning of the block. The content of newly allocated memory is not initialized, hence garbage values would be there.
calloc() is also used to allocate space for array in memory.
Syntax: void * calloc(size_t num, size_t size);
This allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all bits to zero. Which is equivalent to p = malloc(m*n); memset(p, 0, m*n);
realloc() is used to reallocate memory block.
Syntax: void * realloc(void * ptr, size_t size);
The size of the memory block pointed to by the ptr parameter is changed to the size bytes, expanding or reducing the amount of memory available in the block. If the new size is more than old size, then newly allocated data would have garbage values. Otherwise, data would be remain. It is same as malloc()
free() is deallocate space in memory.
Syntax: void free(void * ptr);
A block of memory previously allocated by malloc, calloc, realloc is deallocated, making it available again. This function leaves the value of ptr unchanged, hence it still points to the same location, and not to the NULL pointer.
It is better to set the pointer to NULL after freed it to prevent accidental usage. You can't use dynamically-allocated memory after free it.
There was no standard to find out how big the allocated memory block is using the pointer.
7. What is the difference between const char *p, char * const p and const char * const p?
Ans: const char *p:- This is a pointer to constant char. One cannot change the value pointed by p, but can change the pointer p itself. char const *p is also same.
Ex: *p = "A"; // is illegal.
p = "Hello"; // is legal.
char * const p:- This is a constant pointer to (not-const)char. One cannot change the pointer p, but can change the value pointed by p.
Ex: *p = "A"; // is illegal.
p = "Hello"; // is legal.
const char * const p:- This is a constant pointer to constant char. One cannot change either pointer or value pointed by p.
Ex: *p = "A"; // is illegal.
p = "Hello"; // is illegal.
8. What is a void pointer? Why can't we perform arithmetic on a void * pointer?
Ans: A void data type is used when no other data type is appropriate. A void pointer is a pointer that may point any kind of object. It is used when a pointer must be specified but its type is unknown. The compiler doesn't know the size of the objects in case of void * pointer. Before performing arithmetic, convert the pointer either to char * or to the pointer type you are trying to manipulate.
9. What do Segmentation Fault, access violation, core dump and bus error mean?
Ans: Segmentation Fault, core dump, bus error kind of errors usually mean that the program tried to access memory it doest't have.
Probable causes are overflow of local arrays, improper use of NULL pointers, corruption of the malloc() data structures.
SIGSEGV indicates an invalid access to valid memory address. SIGBUS indicates an access to invalid memory address.
Segmentation fault occurs a process tries to access memory that it is not allowed to, such as the memory at address 0(where NULL points). It is easy to get to a segment fault which dereference NULL.
Ex: char *p;
p = NULL;
++(*p);
10. What is the difference between an array of pointers and a pointer to an array?
Ans: int *p[10]; // array of 10 pointers
int (*p)[10]; // pointer to a 10 elements array
11. What is memory leak?
Ans: Some programs fail to release memory that in no longer being used back inti the pool of available memory. The result is that the used memory is not cleared for use by other programs and thus diminishes the operating capacity of the computer.
Ex: while(malloc(50)); // Here 50 bytes of memory block will be allocated in infinite times. It will return NULL sooner or later due to lack of memory.
12. What is dangling pointer?
Ans: Dangling pointer arise when an object is deleted or deallocated, without modifying the value of pointer, so the pointer still points to the memory location of the deallocated memory. Dereferencing such a pointer usually produces garbage.
Ex: {
char *dp = NULL;
{
char c;
dp = &c;
} // c falls out of the scope
} // dp is now a dangling pointer
The solution to this problem would be to assign NULL to dp immediately before the inner block is exited.
Ex: char *ch = (char*)malloc(sizeof(char));
free(dp); //dp is now became a dangling pointer
dp = NULL; //dp is no longer dangling
Using reference counters which keep track of how many pointers are pointing to this memory location can prevent such issues. The reference counts are incremented when a new pointer starts to point to the memory location and decremented when they no longer need to point to that memory. When the reference count reaches zero, the memory can be safely freed. Also, once freed, the corresponding pointer must be set to NULL.
13. What is equivalent to a[i], and a[i][j]?
Ans: a[i] == i[a] == *(a+i) == *(i+a)
a[i][j] == *(*(a+i)+j)
14. What operations are valid on pointers?
Ans: (px <> py), (px >= py), (px <= py), (px != py), (px == py), px = px + n,
px = px - n, px - py.
Other than these, all are invalid (multiplication, division, addition of two pointers).
15. What are near, far and huge pointers?
Ans: A near pointer is 2 bytes long whereas a far and a huge pointers are 4 bytes long. near, far, and huge pointers exist only under DOS. Under Windows and Linux every pointer is 4 bytes long.
A far pointer and huge pointer are always treated as 32-bit pointer and contain both segment and an offset. But only difference is huge pointer is normalized pointer whereas far pointer is not.
Ex: char far *a = 0x00000120; char far *b = 0x00120000;
char huge *a = 0x00000120; char huge *b = 0x00120000;
In case of far pointer, all 32 bits are in consideration, hence both address are different, but not. In case of huge pointer, all 32 bits are converted into 20-bits towards normalization. Hence both address are referring same location as bellow.
00000 00120 // segment address left shifted by 4bits.
0120 0000 // offset address
00120 00120 // normalized 20-bit address.
16. What is 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 does'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 does't have any relationship with the null pointer.
The null string is just another name for an empty string ""
17. What is the difference between malloc() and calloc()?
Ans: void * malloc(size_t size);
void * calloc(size_t num, size_t size);
Functionality wise both are same that they allocate memory from a storage pool which is heap. Both are memory managers not memory allocators. Memory allocation is done by OS by brk() and sbrk().
Some of differences between malloc() and calloc() are:
a) malloc() takes one argument, and calloc() takes two arguments.
b) calloc() initializes all bits to zero, but malloc() does not.
c) A call to calloc() is equalent to a call to malloc followed by one memset(). calloc() is equivalent to
p = malloc(m*n);
memset(p, 0, m*n);
d) Since calloc() is single call to allocate and initialize, it is faster compare to malloc() and memset().
18. Why sizeof() an operator and not a function?
Ans: sizeof() is a compile time operator. To calculate size of an object, we need the type information. This type information is available only at compile time. The compiler replaces all sizeof calls or operations with the actual size_t value.
A function would be pointless since the discovery of types and their sizes is done at compile time. Now in cases where the size of something needs to be known at run time!!!
19. What is an opaque pointer?
Ans: A pointer is said to be opaque if the definition of the type to which it points to is not included in the current translation unit. A translation unit is the result of merging an implementation file with all its headers and header files.
20. What are the common causes of pointer bugs?
Ans: Uninitialized pointers- One of the easiest way to create a pointer bug is to try to reference the value of a pointer even though the pointer is uninitiated and doest not yet to point to a valid address. Ex: int *p; *p = 12;
In this case, p is points to random memory location.
So, make sure you initialize all pointers to a valid address before deference them.
Invalid pointer references- Occurs when a pointer's value is referenced even though the pointer doesn't point to a valid block.
Ex: p = q; // Here q is uninitialized, then the pointer p will then become uninitialized as well and any reference to *p is an invalid pointer reference.
Zero Pointer Reference- Occurs whenever a pointer pointing to zero is used in a statement that attempts to reference a block.
Ex: p = 0; *p = 12; // There is no block pointed by p.
No comments:
Post a Comment