C

Topic: Pointers

What is :- pointer , NULL pointer , dangling pointer , far pointer , near pointer , huge pointer , generic pointer , smart pointer ?

A null pointer has a reserved value, often but not necessarily the value zero, indicating that it refers to no object. In most C programming environments malloc returns a null pointer if it is unable to allocate the memory region requested, which notifies the caller that there is insufficient memory available. Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, a silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults.

{char *cp = NULL;{
char c;cp = &c;
} /* c falls out of scope */ /* cp is now a dangling pointer */
}

Solution to dangling pointer:
char *cp = malloc ( A_CONST );/* ... */

free ( cp ); /* cp now becomes a dangling pointer */c
p = NULL; /* cp is no longer dangling */

far pointer is a pointer which includes a segment selector, making it possible to point to addresses outside of the current segment. Far pointers have a size of 4 bytes. They store both the segment and the offset of the address the pointer is referencing. A far pointer has an address range of 0 - 1M bytes. A far pointer can be incremented and decremented using arithmetic operators. When a far pointer is incremented or decremented ONLY the offset of the pointer is actually incremented or decremented. The segment is never incremented by the arithmetic operators. Near pointers have a size of 2 bytes. They only store the offset of the address the pointer is referencing.

 An address consisting of only an offset has a range of 0 - 64K bytes. A near pointer can be incremented and decremented using arithmetic operators (+, -, ++, and --) through the entire address range. Any attempt to increment a near pointer that has a value of 64K (0xffff) will result in a value of 0.HUGE pointers have a size of 4 bytes. They store both the segment and the offset of the address the pointer is referencing. A huge pointer has an address range of 0 - 1M bytes. A huge pointer can be incremented and decremented using arithmetic operators. The only difference between a far pointer and a huge pointer is that a huge pointer is normalized by the compiler. A normalized pointer is one that has as much of the address as possible in the segment, meaning that the offset is never larger than 15. A huge pointer is normalized only when pointer arithmetic is performed on it. It is not normalized when an assignment is made. 

Generic Pointers

When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.

This is very useful when you want a pointer to point to data of different types at different times.

Smart pointers are objects which store pointers to dynamically allocated (heap) objects. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners

Browse random answers:

What does the error "Null Pointer Assignment" mean and what causes this error?
What are the advantages of using pointers in a program? 
What is the process of writing the null pointer?
Differentiate between ordinary variable and pointer in C.
How does normalization of huge pointer works?
What are the different types of pointers used in C language?
What is the difference between null pointer and wild pointer?
What is the function of dangling pointer?
What are pointers? Why are they used?
How pointer variables are initialized?
In C, why is the void pointer useful? When would you use it?
What is a void pointer?
What is a pointer variable?
What is a pointer value and address?
Difference between ordinary variable and pointer in C? 
What is NULL pointer?
What is a pointer?
What are the uses of a pointer?
When should a far pointer be used?
What are the pointer declarations used in C?
How to use realloc() to dynamically increase size of an already allocated array?
How many levels of pointers can you have?
Declare an array of three function pointers where each function receives two integers andreturns float.
What is :- pointer , NULL pointer , dangling pointer , far pointer , near pointer , huge pointer , generic pointer , smart pointer ?
Explain the variable assignment in the declaration

int *(*p[10])(char *, char *);
What is the difference between near pointer and far pointer?
What is dangling pointer in c? 
What are the pointer declarations used in C?
Differentiate between a constant pointer and pointer to a constant?
Discuss on pointer arithmetic?
What is the invalid pointer arithmetic?
What are the advantages of using array of pointers to string instead of an array of strings?
What is a huge pointer?
What is a normalized pointer, how do we normalize a pointer?
what are pointers integer?
What is pointer to a pointer?
Can a Structure contain a Pointer to itself?
Declare an array of three function pointers where each function receives two integers and returns float.
 Can you add pointers together? Why would you? 
What is a huge pointer?
What is “this”s pointer?
What is generic pointer in C?
What will be the output of the program ? #include<stdio.h>#include<string.h>int main(){    int i, n;    char *x="Alice";    n = strlen(x);    *x = x[n];    for(i=0; i<=n; i++)    {        printf("%s ", x);        x++;    }    printf("\n", x);    return 0;}
Point out the compile time error in the program given below.#include<stdio.h>int main(){    int *x;    *x=100;    return 0;}
What are the differences between malloc() and calloc()? 
What does it mean when a pointer is used in an if statement? 
When would you use a pointer to a function? 
Why should we assign NULL to the elements (pointer) after freeing them? 
How I can add two numbers in c language without using Arithmetic operators?
WRITE A C PROGRAMME TO FIND OUT THE AREA OF A CIRCLE USING POINTER.
How reliable are floating-point comparisons? 
What is a void pointer? 
Why should we assign NULL to the elements (pointer) after freeing them ?
How to operate pointers in any pragram & how to develop our logic while implementing pointer 
WRITE A C PROGRAMME TO PRINT THE FEBONACCI SERIES USING POINTER?AS:0,1,1,2,3,5,8,13,21........N 
Explain between a long pointer and a char pointer , which one consumes more memory?
Linked Lists -- Can you tell me how to check whether a linked list is circular?
The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?
The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?
What are FP_SEG And FP_OF ?