Friday, 23 August 2013

How many types of pointers in C

1.         NULL pointer?
A.        A null pointer has a value reserved for indicating that the pointer does not refer to a valid object. Null pointers  are routinely used to represent  conditions such as the end of a list of unknown length or the failure to perform some action

Null pointer in most programming languages means "no value", while null value in relational database means "unknown value".

            Example:       int *p=0;        or        int *p=NuLL;


2.         Based pointer?
A.        A based pointer is a pointer whose value is an offset from the value of another pointer. This can be used to store and load blocks of data, assigning  the address of the beginning of the block to the base pointer.

3.         wild branch?
A.        Wild pointers are pointers that have not been initialized (that is, a wild pointer has not had any address assigned to it) and may make a program crash or behave oddly. In the Pascal or C programming languages,     pointers that are not specifically initialized may point to unpredictable addresses in memory.
           
            The following example code shows a wild pointer:
                       
            int func(void)
            {
                char *p1 = malloc(sizeof(char)); /* (undefined) value of some place on the heap */
                char *p2;       /* wild (uninitialized) pointer */
                *p1 = 'a';      /* This is OK, assuming malloc() has not returned NULL. */
                *p2 = 'b';      /* This invokes undefined behavior */
            }         

4.         autorelative pointer?
A.       The term autorelative pointer may refer to a pointer whose value is interpreted as an offset from the address of the pointer itself; thus, if a data structure,
M, has an autorelative pointer member, p, that points to some portion of M itself, then M may be relocated in memory without having to update the value of p.                       
The cited patent also uses the term self-relative pointer to mean the same thing. However, the meaning of that term has been used in other ways:
It is often used to mean an offset from the address of a structure rather than from the address of the pointer itself.
It has been used to mean a pointer containing its own address, which can be useful for reconstructing in any arbitrary region of memory a collection of data structures that point to each other.

5.         Void Pointers?
A.        Void pointer is generic pointer that can point to any data type.It can hold the base address of another pointer data variable.
                       
            syntax void *vpt;


            int *ptr=&a;


6.         DANGLING POINTER?     
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 (*NIX) or general protection faults (Windows). If the overwritten data is bookkeeping data used by the system's memory allocator, the corruption can cause system instabilities.

            

1 comments:

  1. Nice article. Thanks for sharing such useful information. Is size of void pointers is fixed or varies according to size of data type.
    "https://www.thecprogramminglanguages.com/2019/01/pointers-in-c.html"

    ReplyDelete