C Language : pointers

Pointers

A pointer is a variable that holds a memory address, usually the location of another variable in memory. The pointers are one of C’s most useful and strongest features. The correct understanding and use of pointers are critical to successful programming in “C” language. Three reasons that support this are:

First, pointers provide the means through which the memory location of a variable can be directly accessed and hence can be manipulated in the way as required.

Second, pointers support C’s dynamic allocation routines.

Third, pointers can improve the efficiency of certain routines.

Pointers are one of the strongest and also one of the most dangerous and vulnerable features of C language. For instance, uninitialized, or wild pointers can cause your system to crash. Perhaps worse, it is easy to use pointers incorrectly, causing bugs that are very difficult to find. So, it becomes very much necessary to understand and grasp this important concept in order to exploit its power and use without creating problems in a program.

The ideas behind pointers are not complicated. Here’s the first key concept: Every byte in the computer’s memory has an address. Addresses are numbers, just like your house number. Moreover, the address numbers start at 0 and go up from there-1, 2, 3, and so on. For example, if you have 640 KB memory i.e., 640*1024 bytes i.e., 655360 bytes of memory, then the first byte will be having address 0, second as 1, third as 2, and so on. The highest address will be 655359. A variable storing a memory address is called a pointer as it points to a specific memory location whose address it is storing under its name.

C MEMORY MAP

Since pointers have a close relation with memory as they store the address of a memory location and also they facilitate C’s dynamic memory allocation routines, we must understand the way C organizes memory for its program. After compiling a program, “C” creates four logically distinct regions of memory that are used for four distinct specific functions. The following figure shows the conceptual memory map of a “C” program.

Program Code

One region in the memory holds the compiled code of the program. Every instruction and every function of the program starts at a particular address.

Global Variables

The next region is the memory area where the global variables of the program are stored. Global variables remain in the memory as long as the program continues.

Stack

The third region known as the stack, is used for a great many things while your program executes. The stack is used for holding the return addresses at function calls, arguments passed to the functions and local variables for functions. The local variables remain in memory as long as the function continues and after that, they are erased from the memory. The stack also stores the current state of the CPU.

Heap

The heap memory area is a region of free memory from which chunks of memory are allocated via C’s dynamic memory allocation functions.

Number Of Visitors

0125596