INTRODUCTION TO POINTERS
What is a pointer?
A pointer is a variable that holds
a memory address, usually the location of another variable in memory.
The pointers are
one of the most useful and strongest features of C++. The correct understanding
and use of pointers is critical to successful programming in C++.
There are three reasons
that support this:
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 dynamic memory allocation which
means they are used to assign memory to variables, dynamically.
Third, pointers increase the efficiency of most of the programs in
C++.
Pointers are one of the strongest and
also one of the most dangerous and vulnerable features of C++. 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 with creating problems in a
program.
The ideas behind pointers are not complicated. Here’s the first key concept:
Every byte in computer’s memory has an address. Addresses are
numbers, just as your house numbers. Moreover, the address numbers start at 0
and go higher from there (1 2 3 4 and so on).
For example, if you have 640 KB memory i.e. 640 x 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
address of a memory location and also facilitate C++’s dynamic memory
allocation routines, we must understand the way C++ organises memory for its
programs. After compiling a program, C++ creates four logically distinct
regions of memory that are used for four distinct specific functions.
The above figure shows the conceptual memory map of a C++ program.
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.
The next region is the memory area where the global
variables of the program are stored. Life time of global variables is the
entire execution of the program, i.e. the global variables remain in memory as
long as the program is running.
The third region known as stack, is used for great many
things while the program executes. The stack is used for holding the return
addresses at function calls, arguments passed to the functions, and local
variables for the 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.
The Heap memory area is a region of free
memory from which chunks of memory are allocated via C++’s dynamic memory
allocation functions.
Do not stop here. To read more topics on C++ click here.
If you have any doubts, queries or suggestions for our website, comment on this post.
If you want to contribute articles on any topic related to C++
or coding in general, contact us on coding.nkcoder@gmail.com.
We will publish the article on our website in your name.
Related content :
Dynamic & static allocation of memory
Declaration & initialisation of pointers


Post a Comment