Sei sulla pagina 1di 17

C++ : Understanding pointers

Linux Tutorials and more...


Home Donate Free eBooks Linux Admin Notes LPI Certification Linux Commands Linux
Links Contact About
Ads by Google Free 3D Cursor Desktop 3D Mouse Pointers 3D Desktop Theme Wallpaper Theme
Ads by Google C++ Programmers C++ Programming C++ Project C++ Examples C Programming Code

Subscribe to admin android apache bash C++ commands database debian fedora filesystem game hardware KDE LPI multimedia
Linuxconfig.org network OpenCV perl program regexp script search security tutorial virtualization vmware VPN
feed

C++ : Understanding pointers


Last Updated on Friday, 31 December 2010 06:05

Author: Lubos Rendek


Date: 10.09.2009 Article Index
1. Introduction
Update: 04.03.2010 - Section 7.2
2. What is a Pointer?
was created to clarify relation
3. Retrieving a Variable's Memory Address
between Pointers and Arrays in
4. Assigning a Variable's Memory Address to a Pointer
C++ 5. Accessing the Value at the Memory Address held by a Pointer
1. Introduction 6. Manipulating Data with Pointers
7. Pointers and Arrays in C++ Language
This article is intended to all 7.1. Array of pointers
programing enthusiasts on all levels 7.2. Pointers vs. Arrays
who do wish to understand pointers 8. Why do we need pointers?
in C++ language.  All code 9. Where do we use pointers?
presented here is not a compiler 9.1. Allocating a memory from the "Free Store"
specific and all examples will be 9.2. Data type class pointers
written in plain ANSI C++. Debate 9.3. Passing to a function by reference using pointers
about pointers can stretch for miles, 10. Memory leaks associated with pointers
and you would need to go really far 10.1. Memory leak caused by pointer reassignment
to master it all. If you really want to 10.2. Memory leak caused by misuse of local variables
run that far, this article gives you a 11. Conclusion
clear understanding of fundamental 12. Useful links:
13. Appendix
concepts about pointers and
13.1. Write, compile and execute C++ “hello world”
prepares you for that journey.
13.2. C++ functions basics
However, those who are new to
13.3. C++ classes basics
C++ programming make sure that 
you are able to write and run your
own C++ “hello world” program, and
also it is recommended that you
have a basic understanding of C++
functions and classes. If you need
to refresh your knowledge about
how to compile and run C++
program, use functions and classes,
please read an appendix at the end
of this document before you
continue reading this article.

2. What is a Pointer?

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

Pointer is a variable that stores a


memory address. OK, that is simple
Ads by Google ! But, what is a memory address
  C++ STL then? Every variable is located
  Turbo C++
under unique location within a
  C++ Download
computer's memory and this unique
  Borland C++
  C C++ Courses location has its own unique address,
the memory address. Normally, variables hold values such as 5 or “hello” and these values are stored under
specific location within computer memory. However, pointer is a different beast, because it holds the
Linux eBooks memory address as its value and has an ability to “point” ( hence pointer ) to certain value within a memory,
by use of its associated memory address.
FREE Download
3. Retrieving a Variable's Memory Address
OK, enough talking and let's get down to the pointer business. To retrieve a variable's memory address, we
need to use address-of operator &.

#include <iostream>
int main()
{
using namespace std;
// Declare an integer variable and initialize it with 99
A guide to unsigned short int myInt = 99;
programming Linux // Print out value of myInt
kernel modules cout << myInt << endl;
// Use address-of operator & to print out
// a memory address of myInt
cout << &myInt << endl;

return 0;
}

OUTPUT:

Introduction to Linux 99
- A Hands on Guide 0xbff26312

The first line of the output contains an integer value 99 and on the second line, there is a memory address
of myInt printed out. Please note that your output will be different.
 

4. Assigning a Variable's Memory Address to a Pointer


Before we can assign a memory address to a pointer, we need to declare one. Declaring a pointer in C++ is
as simple as to declare any other variable with one single difference. Asterix symbol " * " needs to be add
A Newbie's Getting and located after variable type and before a variable name. One rule has to be followed when assigning
Started Guide to memory address to a pointer: pointer type has to match with variable type it will point to. One exception is a
Linux pointer to void, which can handle different types of variables it will point to. To declare a pointer pMark of
type unsigned short int a following syntax is to be used:

#include <iostream>

int main()
{
using namespace std;

// Declare and initialize a pointer.


unsigned short int * pPointer = 0;
// Declare an integer variable and initialize it with 35698
unsigned short int twoInt = 35698;
Linux from Scratch - // Declare an integer variable and initialize it with 77

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

unsigned short int oneInt = 77;


Create Your Own
// Use address-of operator & to assign a memory address of twoInt to a pointer
Linux System - Free
pPointer = &twoInt;
eBook
// Pointer pPointer now holds a memory address of twoInt

// Print out associated memory addresses and its values


cout << "pPointer's memory address:\t\t" << &pPointer << endl;
cout << "Integer's oneInt memory address:\t" << &oneInt << "\tInteger value: \t" << oneInt <<
endl;
cout << "Integer's twoInt memory address:\t" << &twoInt << "\tInteger value: \t" << twoInt <<
endl;
cout << "pPointer is pointing to memory address:\t" << pPointer << " \tInteger value:\t" <<
*pPointer << endl;

return 0;
}
Linux: The Hacking
Solution (v.3.0)
OUTPUT:

pPointer's memory address: 0xbff43314


Integer's oneInt memory address: 0xbff43318 Integer value: 77
Integer's twoInt memory address: 0xbff4331a Integer value: 35698
pPointer is pointing to memory address: 0xbff4331a Integer value: 35698

The GNU/Linux
Advanced
Administration

The diagram above is a high level visual abstraction of how are variables stored within a computer memory.
Pointer pPointer starts at memory address 0xbff43314 and takes 4 bytes. Pointer pPointer holds as a value
a memory address of a short int twoInt ( 2 bytes ) which is 0xbff4331a. This address is stored as a binary
data within a pointer's memory space allocation. Therefore, dereferencing a pointer with a memory address
0xbff4331a will indirectly access a value of twoInt which is in this case a positive integer 36698.
 

5. Accessing the Value at the Memory Address held by a Pointer


As you could see in the previous example a pointer pMark truly holds a value memory address of an
A Complete oneInt. Process accessing a variable's value by a pointer is called indirection, since the value of variable is
Beginner's Manual accessed indirectly. Value of oneInt can be now indirectly accessed with a use of pPointer pointer. To do
for Ubuntu 10.04
that we need to dereference a pointer with dereference operator “ * “ which needs to be placed before a
(Lucid Lynx)
pointer variable name:

#include <iostream>

int main()

{
using namespace std;
// Declare an integer variable and initialize it with 99
unsigned short int myInt = 99;
// Declare and initialize a pointer
unsigned short int * pMark = 0;
// Print out a value of myInt
Advanced Bash- cout << myInt << endl;
Scripting Guide // Use address-of operator & to assign a memory address of myInt to a pointer
pMark = &myInt;

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

// Dereference a pMark pointer with dereference operator * to access a value of myInt


cout << *pMark << endl;

return 0;
}

Partner Linux OUTPUT:


Sites: TuxMachines
Monsterb 99
LinuxBloggers 99
AdamsInfo
LinuxScrew
All For Linux 6. Manipulating Data with Pointers
Ads by Google Same as accessing the value at the memory address held by pointer by indirection, the indirection can also
  Visual C++
be used to manipulate variable's value. Assigning a value to a dereferenced pointer will indirectly change a
  Pointer
  C++ Compiler
value of a variable the pointer is pointing to. The following example illustrates simple manipulation of data
  C++ Program with pointers:
  C++ Classes
#include <iostream>

int main()
Popular Linux
Tutorials {
using namespace std;
Bash scripting // Declare an integer variable and initialize it with 99
Tutorial unsigned short int myInt = 99;
// Declare and initialize a pointer
Linux unsigned short int * pMark = 0;
Commands // Print out a value of myInt
cout << myInt << endl;
HowTo
// Use address-of operator & to assign memory address of myInt to a pointer
configure NFS pMark = &myInt;
// Dereference a pMark pointer with dereference operator * and set new value
How to mount
*pMark = 11;
partition with
// show indirectly a value of pMark and directly the value of myInt
ntfs file system
cout << "*pMark:\t" << *pMark << "\nmyInt:\t" << myInt << endl;
and read write
access return 0;
}
Howto mount
USB drive in
Linux OUTPUT:
HowTo mount 99
cdrom in linux *pMark: 11
myInt: 11
Linux lvm -
Logical Volume
Manager Dereferencing a pMark pointer and assignment of new value to 11 will indirectly alter a value of myInt also
to 11. It is important to bring up again that pointer holds only a variable's memory address not its value. To
Linux Software
Raid 1 Setup access a value of the variable to which a pointer is pointing to the pointer must be derefereced by
dereference operator “ * “. Note “ * “ is not a multiplication, by the context of your C++ code your compiler
Linux WD EARS will differentiate if your intention is to use multiplication or dereference operator.
Advanced Hard
Drive Format 7. Pointers and Arrays in C++ Language
In C++ , an array name is a constant pointer to its first element. The relationship between c++ pointers and
C++ :
arrays are closely related and its use is almost interchangeable. Consider the following example:
Understanding
pointers #include <iostream>

How to crack a
int main()
wireless WEP
{
key using AIR

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

using namespace std;


Crack // Declare an array with 10 elements
int Marks [10]= {1,2,3,4,5,6,7,8,9,0};
Vim Tutorial
// Print out the memory address of an array name
Perl cout << Marks << endl;
Programming // Print out the memory address of a first element of an array
cout << &Marks[0] << endl;
Tutorial
// Print out value of the first element by dereferencing a array name
Get Started with cout << *Marks << endl;
Android return 0;
application }

development
using Linux and OUTPUT:
Android SDK
0xbf83d3fc
Introduction to
0xbf83d3fc
Computer Vision
1
with the
OpenCV Library
on Linux As you can see an array name is indeed a pointer to its first element and therefore, is perfectly legal to
access the array elements by a const pointer. Hence, dereferencing an array name will access a value of
Linux DNS
the first element of a given array. The next example will demonstrate how to access other elements of an
server BIND
array by conts pointers.
configuration

logrotate #include <iostream>

SSH login int main()


without {
password using namespace std;

grep egrep // Declare an array with 10 elements


fgrep rgrep int Marks [10]= {1,2,3, 4,5,6,7,8,9,0};
// Create a constant pointer to Marks array
How to dual const int *pMarks = Marks;
boot Windows // Access a 6th element of an array by pMarks pointer
XP and Ubuntu cout << *(pMarks + 5) << endl;
Linux // Access a 6th element by dereferencing array name
cout << *(Marks + 5) << endl;
// Access a 6th element of an array

Search: cout << Marks[5] << endl;

return 0;
}
search...
Web
linuxconfig.org OUTPUT:

6
6

Poll 6

The 6th element of an array can be referenced with a pointer expression *(Marks + 5). In addition it is
Do you care
possible to declare another constant pointer and assign it a value of a memory address of the first element
about your
of an array . This pointer will then behave the same way as the originally declared Marks array. The “ + “
privacy when
sign tells the compiler to move 5 objects that are integers from the start of the array. If the object, in an
using a
FACEBOOK? integer, as it is in this our example, ( integer is usually 4 bytes ) this will cause the pointer to point to a
Yes, I do and memory address 20 bytes behind the address reserved by the first array element and thus pointing to 6th.
stopped using The following example demonstrates this idea in detail:
Facebook.
#include <iostream>
No, I do not !
int main()
I have nothing
{
to hide.

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

using namespace std;


I'm a Linux // Declare an array with 10 elements
user, I do not int Marks [10]= {1,2,3,4,5,6,7,8,9,0};
have time for // Create a constant pointer to Marks array
Facebook. const int *pMarks = Marks;
No comment
! Facebook for (int i=0, bytes=0; i < 10; ++i, bytes+=4)
sucks ! {
Vote   cout << "Element " << i << ": " << pMarks << " + ";
Results cout << bytes << " bytes" << " = " << (pMarks + i) << endl;
}
return 0;
}

OUTPUT:

Element 0: 0xbfa5ce0c + 0 bytes = 0xbfa5ce0c


Element 1: 0xbfa5ce0c + 4 bytes = 0xbfa5ce10
Element 2: 0xbfa5ce0c + 8 bytes = 0xbfa5ce14
Element 3: 0xbfa5ce0c + 12 bytes = 0xbfa5ce18
Element 4: 0xbfa5ce0c + 16 bytes = 0xbfa5ce1c
Element 5: 0xbfa5ce0c + 20 bytes = 0xbfa5ce20
Element 6: 0xbfa5ce0c + 24 bytes = 0xbfa5ce24
Element 7: 0xbfa5ce0c + 28 bytes = 0xbfa5ce28
Element 8: 0xbfa5ce0c + 32 bytes = 0xbfa5ce2c
Element 9: 0xbfa5ce0c + 36 bytes = 0xbfa5ce30

Think over the following pointer arithmetic operation:


Since we know that this particular array contains integers and integers are 4 bytes long, memory reserved
for the entire array is 40 bytes. Memory address 0xbfa5ce0c is where the first element of the array resides.
Adding 20 bytes ( 0x16 ) to an address of the first element will return address of the 6th element:
0xbfa5ce0c + 0x16 ( 20 bytes ) = 0xbfa5ce20

7.1. Array of pointers
In C++, it is also possible to declare an array of pointers. Such a data structure could be used to create an
array of strings ( string array ). In C++ a string is actually a pointer to its first character, thus a string array
would be essentially an array of pointers to a first character of a string in each array's element.
 

#include <iostream>
int main()
{
using namespace std;

const char *linuxDistro[6] =


{ "Debian", "Ubuntu", "OpenSuse", "Fedora", "Linux Mint", "Mandriva"};

for ( int i=0; i < 6; i++)


cout << linuxDistro[i] << endl;

return 0;
}

OUTPUT:

Debian
Ubuntu
OpenSuse
Fedora
Linux Mint
Mandriva

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

7.2. Pointers vs. Arrays


This section was added to clarify relation between Pointers and Arrays in C++ and it may be skipped by a
regular reader. The previous statement that pointers and arrays are very similar and almost interchangeable
is a good definition when the effort is made to explain on how pointers and arrays in C++ actually work. As
a results, the understanding of these similarities broadens the programmer's knowledge of C++.
There is lots of discussion on how and to what extent are pointers and arrays related in C++. To answer this
question we need to ask a Bjarne Stroustrup, the creator of C++ language. Bjarne Stroustrup wrote in his
book The C++ Programming Language - Third Edition:
' 'In C++, pointers and arrays are closely related. The name of an array can be used as a pointer to
its initial element. Taking a pointer to the element one beyond the end of an array is guaranteed to
work. This is important for many algorithms. However, since such a pointer does not in fact point to
an element of the array, it may not be used for reading and writing. The result of taking the address
of the element before the initial element is undefined and should be avoided. On some machine
architectures, arrays are often allocated on machine addressing boundaries, so "one before the
initial element" simply doesn't make sense' '

8. Why do we need pointers?


So far, you have been exposed to some theory and syntax behind C++ pointers. You learned how to assign
a memory address of a variable to a pointer. Assigning an address to a pointer is useful for an explanation
on how pointers work. However, in the real life you would under no circumstances do that. The correct
question at this point would be: Why do we need pointers in C++ language if we can access and manipulate
variables by just using their declaration name? The ability of C++ to access memory directly by pointers
makes C++ language favorable over some other languages such as Visual Basic, C# or Java. Accessing
variables directly by pointers rather than through their memory location results in increased efficiency and
flexibility of written code. However, as it can be expected, increased efficiency takes its cost, because using
any low-level tool such as a pointer means intensified difficulty in their implementation. The most common
use of pointers includes:

Data management on the free store

Accessing class member data and functions

Passing variables by reference to functions

9. Where do we use pointers?


In this section, we will explore a couple different ways of using pointers in C++ language.

9.1. Allocating a memory from the "Free Store"


Variables declared and used locally inside a function definition are destroyed, once a return value is passed
back to a calling statement. This approach of passing by value to a function has an advantage that
programmer does not have to a free a memory allocated by function's local variables and variables passed
to a function cannot be changes. Disadvantage comes when declared variables within a function scope are
needed to be used by other functions without creating an overhead be copying these variables via function's
return value.

One solution to this problem is to create global variables. However, this always leads to decreased code
readability, efficiency and bugs, therefore, declaring global variables should be avoided whenever possible.
Declared global variables within a global namespace are holding an allocated memory during the entire
program runtime and very often are needed only for a short time.

Imagine that a programmer will be able to dynamically allocate a memory during a program runtime . This
memory allocation can also be used anywhere within a code and freed any time is no longer needed. This is
where the so called “Free Store” comes in. Free store is a memory available for a programmer to be
dynamically allocated and de-allocated during the program execution.

Allocating a memory with new operator

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

new operator is to be used when allocating a memory in the free store is needed. Return value of a new
operator is a pointer. Therefore, it should be assigned to only a pointer. Here is an example:

#include <iostream>
int main()
{
using namespace std;
// Delcare a pointer
unsigned short * pPointer;
// allocate a memory with a new operator on the free store
// and assign a memory address to a pointer named pPointer
pPointer = new unsigned short;
// assign an integer value to a pointer
*pPointer = 31;
// Print out the value of pPointer
cout << *pPointer << endl;

return 0;
}

OUTPUT:

31

The free store memory allocated during the program runtime was released only when the actual program
ended. For this short program it is ok, however not de-allocating a free memory back to the system is not a
good programming practice and more importantly your code will normally consist of more than 11 lines.

De-allocating a memory with delete operator


To return an allocated memory back to the free store a delete operator is to be used:

#include <iostream>
int main()
{
using namespace std;
// Delcare a pointer
unsigned short * pPointer;
// allocate a memory with a new operator on the free store
// and assign a memory address to a pointer named pPointer
pPointer = new unsigned short;
// assign an integer value to a pointer
*pPointer = 31;
// de-allocating memory back to the free store
delete pPointer;
// Print out a value of pPointer
cout << *pPointer << endl;

return 0;
}

OUTPUT:

delete operator frees a memory allocated by a new operator and returns it back to the free store. The
declaration of the pointer is still valid so pPointer can be reassigned. The remove operator just removed
associated memory address with the pointer.
NOTE:It is important to mention that pointers should be always initialized even after delete operator. Not to

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

do so can create so called a stray pointer which can lead to unpredictable results because a stray pointer
may still point to an old position in the memory and compiler can use that memory address to store other
data. Program can function normally. However, a time bomb to crash the program is set and the time is
running.

9.2. Data type class pointers


Declaring a type class data pointers are not different from declaring a pointers to any other data type.
Consider o following example where simple class Heater will be constructed to demonstrate this concept.

#include <iostream>

using namespace std;


class Heater
{
// Declare public functions for class heater
public:
// constrator to initialise temperature
Heater( int itsTemperature);
// destructor, takes no action
~Heater();
// accessor function to retrieve temperature
int getTemperature() const;
// declare private data members
private:
int temperature;
};
// definition of constractor
Heater::Heater(int itsTemperature)
{
temperature = itsTemperature;
}
// definition of destructor
Heater::~Heater()
{
// no action taken here
}
// Definition of an accesor function which returns a value of a private Heater class member
temperature
int Heater::getTemperature() const
{
return temperature;
}

int main()
{
// define a Heater class pointer object and initialize temperature to 8 by use of constructor
Heater * modelXYZ = new Heater(8);
// Access an accessor function getTemperature() to retrieve temperature via an object modelXVZ
cout << "Temperature of heater modelXYZ is :" << modelXYZ->getTemperature() << endl;
// Free an allocated memory back to a free store
delete modelXYZ;

return 0;
}

OUTPUT:

Temperature of heater modelXYZ is :8

The largest part of the code above is devoted to a class definition. The interesting segment for us from
pointers perspective is in the main function. Memory from a free store is allocated to a new pointer object
modelXYZ of class Heater. On the next line of this code, an accessor function getTemperature() is called by

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

referencing a pointer object with an arrow operator. Lastly, a memory set aside from a free store is freed. It
is important to mention that (.) operator which is generally used to access class member functions cannot
be used when an object is declared as a pointer, when a class object is declared as a pointer an arrow ( ->
) operator must be used instead.

9.3. Passing to a function by reference using pointers


In addition to passing by value and reference, passing arguments to a function can be also done by using
pointers. Consider a following example:

#include <iostream>

using namespace std;

// FUNCTION PROTOTYPE
// passing to a function by reference using pointers
void addone( int * a, int * b);

int main()
{
// define integers a and b
int a = 1;
int b = 4;

cout << "a = " << a << "\tb = " << b << endl;
// addone() function call
cout << "addone() function call\n";
addone(&a,&b);
// after addone() function call value a=2 and b=5
cout << "a = " << a << "\tb = " << b << endl;

return 0;
}

// addone() fuction header


void addone( int * a, int * b)
{
// addone() function definition
// this function adds 1 to each value
++*a;
++*b;
}

OUTPUT:

a = 1 b = 4
addone() function call
a = 2 b = 5

Function prototype of addone function includes pointers as accepted variable types. addone() function adds
1 to each variable which a memory address had been passed during the function call, from the main
function. It is imperative to know that using a reference to pointers as passing arguments allow the involved
values to be changed also outside of the addone() function scope. This is a complete opposite to a situation
where a programmer is passing to a function by value. Please see an appendix at the end of this article, for
listed examples on how to pass to a function by value and by reference.

10. Memory leaks associated with pointers


As it was already mentioned above, using pointers and new operator gives to a programmer great power.
Great power very often comes with a great responsibility. False manipulation with a free store memory can
result in memory leaks. Next we will illustrate some instances of memory leaks related to C++ pointers.

10.1. Memory leak caused by pointer reassignment

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

Memory leak takes place when a memory allocated from the free store is no longer needed and is not
released by delete operator. One way this can occur is by reassigning a pointer before a delete operator
had a opportunity to do its job:

#include <iostream>
int main()
{
using namespace std;
// Delcare a pointer
unsigned short * pPointer;
// allocate a memory with a new operator on the free store
// and assign a memory address to a pointer named pPointer
pPointer = new unsigned short;
// assign an integer value to a pointer
*pPointer = 31;
// print out the value of pPointer and its associated memory address
cout << "*pPointer Value: " << *pPointer << "\tMemory Address: " << pPointer << endl;
// reassign a pointer to a new memory address from a free store
pPointer = new unsigned short;
// assign an integer value to a pointer
*pPointer = 15;
// print out a value of pPointer and its corresponding memory address
cout << "*pPointer Value: " << *pPointer << "\tMemory Address: " << pPointer << endl;
// de-allocating memory back to the free store
delete pPointer;

return 0;
}

OUTPUT:

*pPointer Value: 31 Memory Address: 0x90e9008


*pPointer Value: 15 Memory Address: 0x90e9018

Analysing an output from a memory leak example:


1. pPointer holds a memory address ( 0x90e9008 ) which points to an integer value 15.
2. pPointer was reassigned with a new memory address by allocating a new memory from a free store.
3. The original memory address which pointed to a value 31 is lost, and therefore, it cannot be released.
4. pPointer now holds a memory address ( 0x90e9018 ) which points to an integer of value 15.
5. Allocated memory with an address 0x90e9018 is de-allocated.
6. Allocated memory with an address 0x90e9008 is released due to the program termination.
10.2. Memory leak caused by misuse of local variables
When a function returns a value or executes its last statement all variables declared within a function
definition are destroyed and no longer accessible from the stack segment. This can cause a problem what
memory from a free store is allocated to a pointer declared locally within a function definition, and it is not
freed with delete operator before it goes out of the scope. The next example will sketch how it may occur:

#include <iostream>
// function prototype
void localPointer();

int main()
{
// function call from the main function
localPointer();

return 0;
}

// function header
void localPointer()

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

{
// function definition
// variable declaration pPointer of type pointer
unsigned short int * pPointer;
// allocate a new memory from the free store and assign its memory address to a pointer pPointer
pPointer = new unsigned short int;
// initialise pPointer
*pPointer = 38;
// print out a value to which pPointer is pointing to
std::cout << "Value of *pPointer : " << *pPointer << std::endl;
// de-allocation statement "delete pPointer;" is missing here

// function end
}

OUTPUT:

Value of *pPointer : 38

When the function localPointer() exits, all variables decalred within a function scope are removed from the
existence. pPointer is also a variable declared locally and therefore, a memory address associated with
assigned memory from the free store will be lost and therefore, impossible de-allocate. The program will run
normally in this simple example. However, every time the function localPointer() is called, a new memory will
be set aside and not freed, which will diminish program's efficiency and cause program crash. More
examples of memory leaks could be found on codersource.net.

11. Conclusion
As it was already mentioned earlier, pointers give a great power to any software engineer who masters
them. This article tried to introduce a pointers in simple way by using examples, so reader can be on a
good way to achieve that goal. Accessing a memory directly by pointers is one of the best features of C++
language and for that reason is often preferable language also for embedded devices programming. Great
caution needs to be taken when working with pointers as great power can swiftly turn into a disaster.

12. Useful links:
Here are some great links to enhance understanding of C++ pointers.

cplusplus.com: C++ Language Tutorial : Pointers

codersource.net: C++ Pointers

augustcouncil.com: Pointers

13. Appendix
13.1. Write, compile and execute C++ “hello world”
#include <iostream>

int main()
{
using namespace std;
cout << "Hello world\n";

return 0;
}

COMPILE:

g++ hello-world.cc -o hello-world

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

EXECUTE:

./hello-world

OUTPUT:

Hello World

13.2. C++ functions basics


Please note that the following program is just for revision purposes only. It should not serve as a full guide
to C++ functions. Please visit following links for better understanding on how to use C++ functions:

cplusplus.com: C++ Language Tutorial : Functions (I)

codersource.net : C++ Functions

devarticles.com: Beginner's Guide to Functions in C++

#include <iostream>

using namespace std;

// FUNCTION PROTOTYPES

// passing to a function by value


int add( int a, int b);
// passing to a function by reference using pointers
void addone( int * a, int * b);
// passing to a function by reference using reference
void swap( int &a, int &b);

int main()

{
// define integers a and b
int a = 1;
int b = 4;

// add() function call return value will be printed out


cout << "add() function\n";
cout << add(a,b) << endl;
cout << "a = " << a << "\tb = " << b << endl;
// addone() function call
cout << "addone() function\n";
addone(&a,&b);
// after addone() function call value a=2 and b=5
cout << "a = " << a << "\tb = " << b << endl;
// swap() function call
cout << "swap() function\n";
swap(a,b);
// after swap() function call value a=5 and b=2
cout << "a = " << a << "\tb = " << b << endl;

return 0;
}
// add() fuction header
int add( int a, int b)
{
// add() function definition
// this function returns sum of two integers
return a + b;

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

// addone() fuction header


void addone( int * a, int * b)
{
// addone() function definition
// this function adds 1 to each value
++*a;
++*b;
}

// swap() function header


void swap( int &a, int &b)
{
// swap() function definition
// this function swaps values
int tmp;

tmp = a;
a = b;
b = tmp;
}

OUTPUT:

add() function
5
a = 1 b = 4
addone() function
a = 2 b = 5
swap() function
a = 5 b = 2

13.3. C++ classes basics


Please note that the following program is just for revision purposes only. It should not serve as a full guide
to C++ classes. Please visit following links for better understanding on how to use C++ classes:

cplusplus.com: C++ Language Tutorial : Classes (I)

codersource.net : C++ Tutorial - class

en.wikipedia.org: C++ classes

#include <iostream>

using namespace std;


class Heater
{
// declare public functions for class heater
public:
// constrator to initialize a private data member temperature
Heater( int itsTemperature);
// destructor, takes no action
~Heater();
// accessor function to retrieve a private date member temperature
int getTemperature() const;
// declare private data members
private:
int temperature;
};
// definition of constractor
Heater::Heater(int itsTemperature)

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

{
temperature = itsTemperature;
}
// definition of destructor
Heater::~Heater()
{
// no action taken
}
// definition of accesor function which returns value of a private Heater class member
temperature
int Heater::getTemperature() const
{
return temperature;
}

int main()
{
// define a Heater class object and initialise temperature to 8 by calling a constructor
Heater modelXYZ(8);
// access accessor function getTemperature() to retrieve temperature via object modelXVZ
cout << "Temperature of heater modelXYZ is :" << modelXYZ.getTemperature() << endl;
return 0;

OUTPUT:

Temperature of heater modelXYZ is :8

Share this linux post:

Add New Comment

Post as …

Showing 0 comments

Popular
Sort by Popular now
now   Subscribe by email   Subscribe by RSS

Linux eBooks FREE Download

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

The GNU/Linux Advanced Administration


The GNU/Linux systems have reached an important level of maturity, allowing to
integrate them in almost any kind of work environment, from a desktop PC to the
sever facilities of a big company.
In this ebook "The GNU/Linux Operating System", the main contents are related with
system administration. You will learn how to install and configure several computer
services, and how to optimize and synchronize the resources using GNU/Linux.

The topics covered in this 500+ page eBook include Linux network, server and data
administration, Linux kernel, security, clustering, configuration, tuning, optimization,
migration and coexistence with non-Linux systems. A must read for any serious Linux
system admin.

A Newbies Getting Started Guide to Linux


Learn the basics of the Linux operating systems. Get to know what it is all about,
and familiarize yourself with the practical side. Basically, if youre a complete
Linux newbie and looking for a quick and easy guide to get you started this is it.
Youve probably heard about Linux, the free, open-source operating system thats been
pushing up against Microsoft. Its way cheaper, faster, safer, and has a far bigger active
community than Windows, so why arent you on it? Dont worry, Makeuseof.com
understands. Like many things, venturing off into a completely unknown world can seem
rather scary, and also be pretty difficult in the beginning. Its while adapting to the
unknown, that one needs a guiding, and caring hand. This guide will tell you all you need
to know in 20 illustrated pages, helping you to take your first steps. Let your curiosity
take you hostage and start discovering Linux today, with this manual as your guide! Dont
let Makeuseof.com keep you any longer, and download the Newbies Initiation to Linux.
With this free guide you will also receive daily updates on new cool websites and programs
in your email for free courtesy of MakeUseOf.

Linux from Scratch


Linux from Scratch describes the process of creating your own Linux system from
scratch from an already installed Linux distribution, using nothing but the source
code of software that you need.
This 318 page eBook provides readers with the background and instruction to design and
build custom Linux systems. This eBook highlights the Linux from Scratch project and the
benefits of using this system. Users can dictate all aspects of their system, including
directory layout, script setup, and security. The resulting system will be compiled
completely from the source code, and the user will be able to specify where, why, and how
programs are installed. This eBook allows readers to fully customize Linux systems to their
own needs and allows users more control over their system.

A Complete Beginners Manual for Ubuntu 10.04 (Lucid


Lynx)
Getting Started with Ubuntu 10.04 (Lucid Lynx) is a comprehensive beginners
guide for the Ubuntu operating system; it features comprehensive guides, How
Tos and information on anything you need to know after first installing Ubuntu.
Designed to be as user-friendly and easy to follow as possible, it should provide the first
point of reference to any Ubuntu newcomer with lots of information. The manual has step
by step instructions and includes lots of screenshots to show you how to do tasks. It also
includes a Troubleshooting section to help you solve common Ubuntu problems quickly.
Download this 160+ page manual today.

Securing & Optimizing Linux: The Hacking Solution


(v.3.0)
A comprehensive collection of Linux security products and explanations in the
most simple and structured manner on how to safely and easily configure and run
many popular Linux-based applications and services.
This book is intended for a technical audience and system administrators who manage
Linux servers, but it also includes material for home users and others. It discusses how to
install and setup a Linux server with all the necessary security and optimization for a high
performance Linux specific machine. It can also be applied with some minor changes to
other Linux variants without difficulty.

back to top

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]


C++ : Understanding pointers

Copyright © 2011 Linuxconfig.org. All Rights Reserved.

file:///C|/Users/Badshah/Desktop/c-understanding-pointers.htm[1/30/2011 2:58:26 AM]

Potrebbero piacerti anche